-- =============================================================================
-- SEED 001 — Usuários fictícios para teste
-- =============================================================================
-- Execute APÓS migration_001.sql
--
-- Usuários criados:
--   paciente@agenciapsi.com.br   senha: Teste@123  → patient
--   terapeuta@agenciapsi.com.br  senha: Teste@123  → therapist
--   clinica1@agenciapsi.com.br   senha: Teste@123  → clinic_coworking
--   clinica2@agenciapsi.com.br   senha: Teste@123  → clinic_reception
--   clinica3@agenciapsi.com.br   senha: Teste@123  → clinic_full
--   saas@agenciapsi.com.br       senha: Teste@123  → saas_admin
-- =============================================================================


-- ============================================================
-- Limpeza de seeds anteriores
-- ============================================================

ALTER TABLE public.patient_groups DISABLE TRIGGER ALL;

DELETE FROM public.tenant_members
WHERE user_id IN (
  SELECT id FROM auth.users
  WHERE email IN (
    'paciente@agenciapsi.com.br',
    'terapeuta@agenciapsi.com.br',
    'clinica1@agenciapsi.com.br',
    'clinica2@agenciapsi.com.br',
    'clinica3@agenciapsi.com.br',
    'saas@agenciapsi.com.br'
  )
);

DELETE FROM public.tenants WHERE id IN (
  'bbbbbbbb-0002-0002-0002-000000000002',
  'bbbbbbbb-0003-0003-0003-000000000003',
  'bbbbbbbb-0004-0004-0004-000000000004',
  'bbbbbbbb-0005-0005-0005-000000000005'
);

DELETE FROM auth.users WHERE email IN (
  'paciente@agenciapsi.com.br',
  'terapeuta@agenciapsi.com.br',
  'clinica1@agenciapsi.com.br',
  'clinica2@agenciapsi.com.br',
  'clinica3@agenciapsi.com.br',
  'saas@agenciapsi.com.br'
);

ALTER TABLE public.patient_groups ENABLE TRIGGER ALL;


-- ============================================================
-- 1. Usuários no auth.users
-- ============================================================

INSERT INTO auth.users (
  id, email, encrypted_password, email_confirmed_at,
  created_at, updated_at, raw_user_meta_data, role, aud
)
VALUES
  (
    'aaaaaaaa-0001-0001-0001-000000000001',
    'paciente@agenciapsi.com.br',
    crypt('Teste@123', gen_salt('bf')),
    now(), now(), now(),
    '{"name": "Ana Paciente"}'::jsonb,
    'authenticated', 'authenticated'
  ),
  (
    'aaaaaaaa-0002-0002-0002-000000000002',
    'terapeuta@agenciapsi.com.br',
    crypt('Teste@123', gen_salt('bf')),
    now(), now(), now(),
    '{"name": "Bruno Terapeuta"}'::jsonb,
    'authenticated', 'authenticated'
  ),
  (
    'aaaaaaaa-0003-0003-0003-000000000003',
    'clinica1@agenciapsi.com.br',
    crypt('Teste@123', gen_salt('bf')),
    now(), now(), now(),
    '{"name": "Clinica Espaco Psi"}'::jsonb,
    'authenticated', 'authenticated'
  ),
  (
    'aaaaaaaa-0004-0004-0004-000000000004',
    'clinica2@agenciapsi.com.br',
    crypt('Teste@123', gen_salt('bf')),
    now(), now(), now(),
    '{"name": "Clinica Mente Sa"}'::jsonb,
    'authenticated', 'authenticated'
  ),
  (
    'aaaaaaaa-0005-0005-0005-000000000005',
    'clinica3@agenciapsi.com.br',
    crypt('Teste@123', gen_salt('bf')),
    now(), now(), now(),
    '{"name": "Clinica Bem Estar"}'::jsonb,
    'authenticated', 'authenticated'
  ),
  (
    'aaaaaaaa-0006-0006-0006-000000000006',
    'saas@agenciapsi.com.br',
    crypt('Teste@123', gen_salt('bf')),
    now(), now(), now(),
    '{"name": "Admin Plataforma"}'::jsonb,
    'authenticated', 'authenticated'
  );


-- ============================================================
-- 2. Profiles
-- ============================================================

INSERT INTO public.profiles (id, role, account_type, full_name)
VALUES
  ('aaaaaaaa-0001-0001-0001-000000000001', 'portal_user', 'patient',   'Ana Paciente'),
  ('aaaaaaaa-0002-0002-0002-000000000002', 'portal_user', 'therapist', 'Bruno Terapeuta'),
  ('aaaaaaaa-0003-0003-0003-000000000003', 'portal_user', 'clinic',    'Clinica Espaco Psi'),
  ('aaaaaaaa-0004-0004-0004-000000000004', 'portal_user', 'clinic',    'Clinica Mente Sa'),
  ('aaaaaaaa-0005-0005-0005-000000000005', 'portal_user', 'clinic',    'Clinica Bem Estar'),
  ('aaaaaaaa-0006-0006-0006-000000000006', 'saas_admin',  'free',      'Admin Plataforma')
ON CONFLICT (id) DO UPDATE SET
  role         = EXCLUDED.role,
  account_type = EXCLUDED.account_type,
  full_name    = EXCLUDED.full_name;


-- ============================================================
-- 3. SaaS Admin
-- ============================================================

INSERT INTO public.saas_admins (user_id, created_at)
VALUES ('aaaaaaaa-0006-0006-0006-000000000006', now())
ON CONFLICT (user_id) DO NOTHING;


-- ============================================================
-- 4. Tenant do terapeuta
-- ============================================================

INSERT INTO public.tenants (id, name, kind, created_at)
VALUES ('bbbbbbbb-0002-0002-0002-000000000002', 'Bruno Terapeuta', 'therapist', now())
ON CONFLICT (id) DO NOTHING;

INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
VALUES ('bbbbbbbb-0002-0002-0002-000000000002', 'aaaaaaaa-0002-0002-0002-000000000002', 'tenant_admin', 'active', now())
ON CONFLICT (tenant_id, user_id) DO NOTHING;

DO $$ BEGIN
  PERFORM public.seed_determined_commitments('bbbbbbbb-0002-0002-0002-000000000002');
END; $$;


-- ============================================================
-- 5. Tenant Clinica 1 — Coworking
-- ============================================================

INSERT INTO public.tenants (id, name, kind, created_at)
VALUES ('bbbbbbbb-0003-0003-0003-000000000003', 'Clinica Espaco Psi', 'clinic_coworking', now())
ON CONFLICT (id) DO NOTHING;

INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
VALUES ('bbbbbbbb-0003-0003-0003-000000000003', 'aaaaaaaa-0003-0003-0003-000000000003', 'tenant_admin', 'active', now())
ON CONFLICT (tenant_id, user_id) DO NOTHING;

DO $$ BEGIN
  PERFORM public.seed_determined_commitments('bbbbbbbb-0003-0003-0003-000000000003');
END; $$;


-- ============================================================
-- 6. Tenant Clinica 2 — Recepcao
-- ============================================================

INSERT INTO public.tenants (id, name, kind, created_at)
VALUES ('bbbbbbbb-0004-0004-0004-000000000004', 'Clinica Mente Sa', 'clinic_reception', now())
ON CONFLICT (id) DO NOTHING;

INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
VALUES ('bbbbbbbb-0004-0004-0004-000000000004', 'aaaaaaaa-0004-0004-0004-000000000004', 'tenant_admin', 'active', now())
ON CONFLICT (tenant_id, user_id) DO NOTHING;

DO $$ BEGIN
  PERFORM public.seed_determined_commitments('bbbbbbbb-0004-0004-0004-000000000004');
END; $$;


-- ============================================================
-- 7. Tenant Clinica 3 — Full
-- ============================================================

INSERT INTO public.tenants (id, name, kind, created_at)
VALUES ('bbbbbbbb-0005-0005-0005-000000000005', 'Clinica Bem Estar', 'clinic_full', now())
ON CONFLICT (id) DO NOTHING;

INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
VALUES ('bbbbbbbb-0005-0005-0005-000000000005', 'aaaaaaaa-0005-0005-0005-000000000005', 'tenant_admin', 'active', now())
ON CONFLICT (tenant_id, user_id) DO NOTHING;

DO $$ BEGIN
  PERFORM public.seed_determined_commitments('bbbbbbbb-0005-0005-0005-000000000005');
END; $$;


-- ============================================================
-- 8. Subscriptions ativas
-- ============================================================

-- Paciente → patient_free
INSERT INTO public.subscriptions (
  user_id, plan_id, plan_key, status, interval,
  current_period_start, current_period_end, source, started_at, activated_at
)
SELECT
  'aaaaaaaa-0001-0001-0001-000000000001',
  p.id, p.key, 'active', 'month',
  now(), now() + interval '30 days', 'seed', now(), now()
FROM public.plans p WHERE p.key = 'patient_free';

-- Terapeuta → therapist_free
INSERT INTO public.subscriptions (
  user_id, plan_id, plan_key, status, interval,
  current_period_start, current_period_end, source, started_at, activated_at
)
SELECT
  'aaaaaaaa-0002-0002-0002-000000000002',
  p.id, p.key, 'active', 'month',
  now(), now() + interval '30 days', 'seed', now(), now()
FROM public.plans p WHERE p.key = 'therapist_free';

-- Clinica 1 → clinic_free
INSERT INTO public.subscriptions (
  tenant_id, plan_id, plan_key, status, interval,
  current_period_start, current_period_end, source, started_at, activated_at
)
SELECT
  'bbbbbbbb-0003-0003-0003-000000000003',
  p.id, p.key, 'active', 'month',
  now(), now() + interval '30 days', 'seed', now(), now()
FROM public.plans p WHERE p.key = 'clinic_free';

-- Clinica 2 → clinic_free
INSERT INTO public.subscriptions (
  tenant_id, plan_id, plan_key, status, interval,
  current_period_start, current_period_end, source, started_at, activated_at
)
SELECT
  'bbbbbbbb-0004-0004-0004-000000000004',
  p.id, p.key, 'active', 'month',
  now(), now() + interval '30 days', 'seed', now(), now()
FROM public.plans p WHERE p.key = 'clinic_free';

-- Clinica 3 → clinic_free
INSERT INTO public.subscriptions (
  tenant_id, plan_id, plan_key, status, interval,
  current_period_start, current_period_end, source, started_at, activated_at
)
SELECT
  'bbbbbbbb-0005-0005-0005-000000000005',
  p.id, p.key, 'active', 'month',
  now(), now() + interval '30 days', 'seed', now(), now()
FROM public.plans p WHERE p.key = 'clinic_free';


-- ============================================================
-- 9. Vincula terapeuta à Clinica 3 (exemplo de associacao)
-- ============================================================

INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
VALUES (
  'bbbbbbbb-0005-0005-0005-000000000005',
  'aaaaaaaa-0002-0002-0002-000000000002',
  'therapist', 'active', now()
)
ON CONFLICT (tenant_id, user_id) DO NOTHING;


-- ============================================================
-- Confirmacao
-- ============================================================

DO $$
BEGIN
  RAISE NOTICE '✅ Seed aplicado com sucesso.';
  RAISE NOTICE '  paciente@agenciapsi.com.br   → patient';
  RAISE NOTICE '  terapeuta@agenciapsi.com.br  → therapist';
  RAISE NOTICE '  clinica1@agenciapsi.com.br   → clinic_coworking';
  RAISE NOTICE '  clinica2@agenciapsi.com.br   → clinic_reception';
  RAISE NOTICE '  clinica3@agenciapsi.com.br   → clinic_full';
  RAISE NOTICE '  saas@agenciapsi.com.br       → saas_admin';
  RAISE NOTICE '  Senha: Teste@123';
END;
$$;
