-- =============================================================================
-- SEED 013 — Subscriptions + Determined Commitments
-- =============================================================================
-- Execute APÓS: seed_010 (plans), seed_011 (features), seed_012 (plan_features)
-- E APÓS: seed_001, seed_002, seed_003 (usuários e tenants)
--
-- Cria subscriptions para todos os usuários de teste e chama
-- seed_determined_commitments() para tenants que ainda não têm.
--
-- Mapeamento:
--   paciente@   (user_id)   → patient_free
--   terapeuta@  (user_id)   → therapist_free
--   clinica1@   (tenant_id) → clinic_free    (coworking)
--   clinica2@   (tenant_id) → clinic_free    (recepção)
--   clinica3@   (tenant_id) → clinic_free    (full)
--   supervisor@ (user_id)   → supervisor_free
--   editor@     (user_id)   → therapist_free (acesso via platform_roles)
--   therapist2@ (user_id)   → therapist_free
--   therapist3@ (user_id)   → therapist_free
--   secretary@              → sem plano próprio (usa plano da Clínica 2)
--   saas@                   → saas_admin (sem plano)
-- =============================================================================

BEGIN;

-- Limpa subscriptions de seed anteriores
DELETE FROM public.subscriptions WHERE source = 'seed';

-- ============================================================
-- 1. Subscriptions — user_id scope (therapist, patient, supervisor)
-- ============================================================

-- 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 '1 year',
  '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 '1 year',
  'seed', now(), now()
FROM public.plans p WHERE p.key = 'therapist_free';

-- Supervisor → supervisor_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-0007-0007-0007-000000000007',
  p.id, p.key, 'active', 'month',
  now(), now() + interval '1 year',
  'seed', now(), now()
FROM public.plans p WHERE p.key = 'supervisor_free';

-- Editor → therapist_free (acesso editor vem de platform_roles)
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-0008-0008-0008-000000000008',
  p.id, p.key, 'active', 'month',
  now(), now() + interval '1 year',
  'seed', now(), now()
FROM public.plans p WHERE p.key = 'therapist_free';

-- Terapeuta 2 → 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-0009-0009-0009-000000000009',
  p.id, p.key, 'active', 'month',
  now(), now() + interval '1 year',
  'seed', now(), now()
FROM public.plans p WHERE p.key = 'therapist_free';

-- Terapeuta 3 → 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-0010-0010-0010-000000000010',
  p.id, p.key, 'active', 'month',
  now(), now() + interval '1 year',
  'seed', now(), now()
FROM public.plans p WHERE p.key = 'therapist_free';


-- ============================================================
-- 2. Subscriptions — tenant_id scope (clinic)
-- ============================================================

-- Clínica 1 (Espaço Psi / Coworking) → 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 '1 year',
  'seed', now(), now()
FROM public.plans p WHERE p.key = 'clinic_free';

-- Clínica 2 (Mente sã / Recepção) → 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 '1 year',
  'seed', now(), now()
FROM public.plans p WHERE p.key = 'clinic_free';

-- Clínica 3 (Bem Estar / Full) → 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 '1 year',
  'seed', now(), now()
FROM public.plans p WHERE p.key = 'clinic_free';


-- ============================================================
-- 3. Determined Commitments (idempotente por design da função)
-- ============================================================
-- Chama para todos os tenants. A função usa ON CONFLICT internamente.

DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0002-0002-0002-000000000002'); END $$;  -- Terapeuta
DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0003-0003-0003-000000000003'); END $$;  -- Clínica 1
DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0004-0004-0004-000000000004'); END $$;  -- Clínica 2
DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0005-0005-0005-000000000005'); END $$;  -- Clínica 3
DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0009-0009-0009-000000000009'); END $$;  -- Terapeuta 2
DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0010-0010-0010-000000000010'); END $$;  -- Terapeuta 3


-- ============================================================
-- 4. Confirma
-- ============================================================

DO $$
DECLARE
  v_count integer;
BEGIN
  SELECT count(*) INTO v_count FROM public.subscriptions WHERE source = 'seed';
  RAISE NOTICE 'seed_013_subscriptions: % subscriptions ativas criadas.', v_count;
  SELECT count(*) INTO v_count FROM public.determined_commitments WHERE is_native = true;
  RAISE NOTICE 'seed_013_subscriptions: % determined_commitments nativos existentes.', v_count;
END;
$$;

COMMIT;
