-- =============================================================================
-- SEED 012 — Plan Features (vínculo plano ↔ feature com limites)
-- =============================================================================
-- Idempotente: ON CONFLICT (plan_id, feature_id) DO UPDATE
--
-- Legenda:
--   enabled=true  → feature ativa no plano
--   enabled=false → feature aparece como "PRO" / upsell na UI
--   limits jsonb  → limites quantitativos (sessions_per_month, etc.)
--
-- IMPORTANTE: therapist_free e clinic_free incluem features básicas
-- necessárias para o sistema funcionar (agenda, pacientes, etc.).
-- O data_dump de 11/03 estava incompleto para esses planos.
-- =============================================================================

BEGIN;

-- Limpa plan_features existentes para reinserir de forma limpa
DELETE FROM public.plan_features;

-- ════════════════════════════════════════════════════════════════
-- CLINIC PRO  (a74bc2d4) — todas as features habilitadas
-- ════════════════════════════════════════════════════════════════

INSERT INTO public.plan_features (plan_id, feature_id, enabled, limits) VALUES
  -- Agenda
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '5e539124-630f-4c2a-a9de-7999317085e6', true, NULL),           -- agenda.view
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'a74fef14-c9d9-4884-ba45-f81c60e0783a', true, NULL),           -- agenda.manage
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '9b36c65d-b3b3-4bed-b6d5-f7ee8c087c80', true, '{"max_patients": 9999, "max_therapists": 999}'), -- clinic_calendar
  -- Pacientes
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'a56482a1-0787-49da-90a7-e1857488734a', true, NULL),           -- patients
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '57f631a1-9ebe-480b-a2cb-144ad32ff5f0', true, NULL),           -- patients.view
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '4e5bc50b-e339-42fe-9d91-61e8555f83e7', true, NULL),           -- patients.manage
  -- Agendamento online
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '53a48c3b-0617-4618-adf8-f3a255c51ee4', true, '{"sessions_per_month": 9999}'), -- online_scheduling
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '5739aa27-b089-4b15-b149-31b13d768825', true, NULL),           -- online_scheduling.manage
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '0bfe0b1c-8c3d-4c0c-af29-2ddc24f31bc7', true, NULL),           -- online_scheduling.public
  -- Relatórios
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'b3efa25d-60a4-4974-8153-6ec098b3d477', true, NULL),           -- reports_basic
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'bf133ad1-da8e-4ea9-bd66-21901cb50075', true, NULL),           -- reports_advanced
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'a830e45b-3bb4-4b17-812d-fe83777a2377', true, NULL),           -- advanced_reports
  -- Lembretes
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'f5d66212-fd73-4472-a306-07928e5deaec', true, '{"reminders_per_month": 9999}'), -- reminders
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '8cc81988-d02a-4542-9cb2-ce2ed7c18d60', true, NULL),           -- sms_reminder
  -- Clínica
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '336aeeba-b18e-4e68-8303-d42ba09f4b20', true, NULL),           -- secretary
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '30c9cdd5-7c8c-44d9-8c0b-614165bb9496', true, NULL),           -- shared_reception
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '74fc1321-4d17-49c3-b72e-db3a7f4be451', true, NULL),           -- rooms
  -- Intake
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'c109ad27-0edf-4774-91a7-94dac4faab49', true, NULL),           -- intake_public
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', '90e92108-8124-40ee-88a0-f0ecafb57d76', true, NULL),           -- intakes_pro
  -- PRO exclusivo
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'f393178c-284d-422f-b096-8793f85428d5', true, NULL),           -- custom_branding
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'd6f54674-ea8b-484b-af0e-99127a510da2', true, NULL),           -- api_access
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'a5593d96-dd95-46bb-bef0-bd379b56ad50', true, NULL),           -- audit_log
  -- Documentos
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'b1a2c3d4-1111-4aaa-bbbb-000000000001', true, '{"max_storage_mb": 5000}'), -- documents.upload
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'b1a2c3d4-1111-4aaa-bbbb-000000000002', true, NULL),           -- documents.templates
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'b1a2c3d4-1111-4aaa-bbbb-000000000003', true, NULL),           -- documents.signatures
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'b1a2c3d4-1111-4aaa-bbbb-000000000004', true, NULL),           -- documents.share_links
  ('a74bc2d4-88c6-4cc6-b004-ef2bcb1b5145', 'b1a2c3d4-1111-4aaa-bbbb-000000000005', true, NULL);           -- documents.patient_portal


-- ════════════════════════════════════════════════════════════════
-- CLINIC FREE (01a5867f) — features básicas para funcionar
-- ════════════════════════════════════════════════════════════════

INSERT INTO public.plan_features (plan_id, feature_id, enabled, limits) VALUES
  -- Agenda
  ('01a5867f-0705-4714-ac97-a23470949157', '5e539124-630f-4c2a-a9de-7999317085e6', true, NULL),           -- agenda.view
  ('01a5867f-0705-4714-ac97-a23470949157', 'a74fef14-c9d9-4884-ba45-f81c60e0783a', true, NULL),           -- agenda.manage
  ('01a5867f-0705-4714-ac97-a23470949157', '9b36c65d-b3b3-4bed-b6d5-f7ee8c087c80', true, '{"max_patients": 30, "max_therapists": 5}'), -- clinic_calendar
  -- Pacientes
  ('01a5867f-0705-4714-ac97-a23470949157', 'a56482a1-0787-49da-90a7-e1857488734a', true, NULL),           -- patients
  ('01a5867f-0705-4714-ac97-a23470949157', '57f631a1-9ebe-480b-a2cb-144ad32ff5f0', true, NULL),           -- patients.view
  ('01a5867f-0705-4714-ac97-a23470949157', '4e5bc50b-e339-42fe-9d91-61e8555f83e7', true, NULL),           -- patients.manage
  -- Agendamento online (com limites)
  ('01a5867f-0705-4714-ac97-a23470949157', '53a48c3b-0617-4618-adf8-f3a255c51ee4', true, '{"sessions_per_month": 40}'), -- online_scheduling
  ('01a5867f-0705-4714-ac97-a23470949157', '5739aa27-b089-4b15-b149-31b13d768825', true, NULL),           -- online_scheduling.manage
  ('01a5867f-0705-4714-ac97-a23470949157', '0bfe0b1c-8c3d-4c0c-af29-2ddc24f31bc7', true, NULL),           -- online_scheduling.public
  -- Relatórios (básico sim, avançado não)
  ('01a5867f-0705-4714-ac97-a23470949157', 'b3efa25d-60a4-4974-8153-6ec098b3d477', true, NULL),           -- reports_basic
  ('01a5867f-0705-4714-ac97-a23470949157', 'bf133ad1-da8e-4ea9-bd66-21901cb50075', false, NULL),          -- reports_advanced (PRO)
  ('01a5867f-0705-4714-ac97-a23470949157', 'a830e45b-3bb4-4b17-812d-fe83777a2377', false, NULL),          -- advanced_reports (PRO)
  -- Lembretes (com limites)
  ('01a5867f-0705-4714-ac97-a23470949157', 'f5d66212-fd73-4472-a306-07928e5deaec', true, '{"reminders_per_month": 50}'), -- reminders
  ('01a5867f-0705-4714-ac97-a23470949157', '8cc81988-d02a-4542-9cb2-ce2ed7c18d60', false, NULL),          -- sms_reminder (PRO)
  -- Intake
  ('01a5867f-0705-4714-ac97-a23470949157', 'c109ad27-0edf-4774-91a7-94dac4faab49', true, NULL),           -- intake_public
  ('01a5867f-0705-4714-ac97-a23470949157', '90e92108-8124-40ee-88a0-f0ecafb57d76', false, NULL),          -- intakes_pro (PRO)
  -- PRO-only (desabilitado)
  ('01a5867f-0705-4714-ac97-a23470949157', '336aeeba-b18e-4e68-8303-d42ba09f4b20', false, NULL),          -- secretary (PRO)
  ('01a5867f-0705-4714-ac97-a23470949157', '30c9cdd5-7c8c-44d9-8c0b-614165bb9496', false, NULL),          -- shared_reception (PRO)
  ('01a5867f-0705-4714-ac97-a23470949157', '74fc1321-4d17-49c3-b72e-db3a7f4be451', false, NULL),          -- rooms (PRO)
  ('01a5867f-0705-4714-ac97-a23470949157', 'f393178c-284d-422f-b096-8793f85428d5', false, NULL),          -- custom_branding (PRO)
  ('01a5867f-0705-4714-ac97-a23470949157', 'd6f54674-ea8b-484b-af0e-99127a510da2', false, NULL),          -- api_access (PRO)
  ('01a5867f-0705-4714-ac97-a23470949157', 'a5593d96-dd95-46bb-bef0-bd379b56ad50', false, NULL),          -- audit_log (PRO)
  -- Documentos
  ('01a5867f-0705-4714-ac97-a23470949157', 'b1a2c3d4-1111-4aaa-bbbb-000000000001', true, '{"max_storage_mb": 500}'),  -- documents.upload (FREE)
  ('01a5867f-0705-4714-ac97-a23470949157', 'b1a2c3d4-1111-4aaa-bbbb-000000000002', false, NULL),          -- documents.templates (PRO)
  ('01a5867f-0705-4714-ac97-a23470949157', 'b1a2c3d4-1111-4aaa-bbbb-000000000003', false, NULL),          -- documents.signatures (PRO)
  ('01a5867f-0705-4714-ac97-a23470949157', 'b1a2c3d4-1111-4aaa-bbbb-000000000004', false, NULL),          -- documents.share_links (PRO)
  ('01a5867f-0705-4714-ac97-a23470949157', 'b1a2c3d4-1111-4aaa-bbbb-000000000005', false, NULL);          -- documents.patient_portal (PRO)


-- ════════════════════════════════════════════════════════════════
-- THERAPIST PRO (82067ba7) — todas as features habilitadas
-- ════════════════════════════════════════════════════════════════

INSERT INTO public.plan_features (plan_id, feature_id, enabled, limits) VALUES
  -- Agenda
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '5e539124-630f-4c2a-a9de-7999317085e6', true, NULL),           -- agenda.view
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'a74fef14-c9d9-4884-ba45-f81c60e0783a', true, NULL),           -- agenda.manage
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '9b36c65d-b3b3-4bed-b6d5-f7ee8c087c80', true, NULL),           -- clinic_calendar
  -- Pacientes
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '57f631a1-9ebe-480b-a2cb-144ad32ff5f0', true, NULL),           -- patients.view
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '4e5bc50b-e339-42fe-9d91-61e8555f83e7', true, NULL),           -- patients.manage
  -- Agendamento online
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '53a48c3b-0617-4618-adf8-f3a255c51ee4', true, '{"sessions_per_month": 9999}'), -- online_scheduling
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '5739aa27-b089-4b15-b149-31b13d768825', true, NULL),           -- online_scheduling.manage
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '0bfe0b1c-8c3d-4c0c-af29-2ddc24f31bc7', true, NULL),           -- online_scheduling.public
  -- Relatórios
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'b3efa25d-60a4-4974-8153-6ec098b3d477', true, NULL),           -- reports_basic
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'bf133ad1-da8e-4ea9-bd66-21901cb50075', true, NULL),           -- reports_advanced
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'a830e45b-3bb4-4b17-812d-fe83777a2377', true, NULL),           -- advanced_reports
  -- Lembretes
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'f5d66212-fd73-4472-a306-07928e5deaec', true, '{"reminders_per_month": 9999}'), -- reminders
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '8cc81988-d02a-4542-9cb2-ce2ed7c18d60', true, NULL),           -- sms_reminder
  -- Clínica
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '336aeeba-b18e-4e68-8303-d42ba09f4b20', true, NULL),           -- secretary
  -- Intake
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'c109ad27-0edf-4774-91a7-94dac4faab49', true, NULL),           -- intake_public
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', '90e92108-8124-40ee-88a0-f0ecafb57d76', true, NULL),           -- intakes_pro
  -- PRO exclusivo
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'f393178c-284d-422f-b096-8793f85428d5', true, NULL),           -- custom_branding
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'd6f54674-ea8b-484b-af0e-99127a510da2', true, NULL),           -- api_access
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'a5593d96-dd95-46bb-bef0-bd379b56ad50', true, NULL),           -- audit_log
  -- Documentos
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'b1a2c3d4-1111-4aaa-bbbb-000000000001', true, '{"max_storage_mb": 5000}'), -- documents.upload
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'b1a2c3d4-1111-4aaa-bbbb-000000000002', true, NULL),           -- documents.templates
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'b1a2c3d4-1111-4aaa-bbbb-000000000003', true, NULL),           -- documents.signatures
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'b1a2c3d4-1111-4aaa-bbbb-000000000004', true, NULL),           -- documents.share_links
  ('82067ba7-16f0-4803-b36f-4c4e8919d4b4', 'b1a2c3d4-1111-4aaa-bbbb-000000000005', true, NULL);           -- documents.patient_portal


-- ════════════════════════════════════════════════════════════════
-- THERAPIST FREE (c56fe2a8) — features básicas para funcionar
-- ════════════════════════════════════════════════════════════════

INSERT INTO public.plan_features (plan_id, feature_id, enabled, limits) VALUES
  -- Agenda
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', '5e539124-630f-4c2a-a9de-7999317085e6', true, NULL),           -- agenda.view
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'a74fef14-c9d9-4884-ba45-f81c60e0783a', true, NULL),           -- agenda.manage
  -- Pacientes
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', '57f631a1-9ebe-480b-a2cb-144ad32ff5f0', true, NULL),           -- patients.view
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', '4e5bc50b-e339-42fe-9d91-61e8555f83e7', true, NULL),           -- patients.manage
  -- Agendamento online (com limites)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', '53a48c3b-0617-4618-adf8-f3a255c51ee4', true, '{"sessions_per_month": 40}'), -- online_scheduling
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', '5739aa27-b089-4b15-b149-31b13d768825', true, NULL),           -- online_scheduling.manage
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', '0bfe0b1c-8c3d-4c0c-af29-2ddc24f31bc7', true, NULL),           -- online_scheduling.public
  -- Relatórios (básico sim, avançado não)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'b3efa25d-60a4-4974-8153-6ec098b3d477', true, NULL),           -- reports_basic
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'bf133ad1-da8e-4ea9-bd66-21901cb50075', false, NULL),          -- reports_advanced (PRO)
  -- Lembretes (com limites)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'f5d66212-fd73-4472-a306-07928e5deaec', true, '{"reminders_per_month": 50}'), -- reminders
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', '8cc81988-d02a-4542-9cb2-ce2ed7c18d60', false, NULL),          -- sms_reminder (PRO)
  -- Intake
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'c109ad27-0edf-4774-91a7-94dac4faab49', true, NULL),           -- intake_public
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', '90e92108-8124-40ee-88a0-f0ecafb57d76', false, NULL),          -- intakes_pro (PRO)
  -- PRO-only (desabilitado)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'f393178c-284d-422f-b096-8793f85428d5', false, NULL),          -- custom_branding (PRO)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'd6f54674-ea8b-484b-af0e-99127a510da2', false, NULL),          -- api_access (PRO)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'a5593d96-dd95-46bb-bef0-bd379b56ad50', false, NULL),          -- audit_log (PRO)
  -- Documentos
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'b1a2c3d4-1111-4aaa-bbbb-000000000001', true, '{"max_storage_mb": 500}'),  -- documents.upload (FREE)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'b1a2c3d4-1111-4aaa-bbbb-000000000002', false, NULL),          -- documents.templates (PRO)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'b1a2c3d4-1111-4aaa-bbbb-000000000003', false, NULL),          -- documents.signatures (PRO)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'b1a2c3d4-1111-4aaa-bbbb-000000000004', false, NULL),          -- documents.share_links (PRO)
  ('c56fe2a8-2c17-4048-adc7-ff7fbd89461a', 'b1a2c3d4-1111-4aaa-bbbb-000000000005', false, NULL);          -- documents.patient_portal (PRO)


-- ════════════════════════════════════════════════════════════════
-- SUPERVISOR FREE (8c4895a3) — acesso + convite
-- ════════════════════════════════════════════════════════════════

INSERT INTO public.plan_features (plan_id, feature_id, enabled, limits) VALUES
  ('8c4895a3-e12d-48de-a078-efb8a4ea2eb2', '9ab8bdbb-838b-4946-aa5d-fd9cfdd257b3', true, NULL),           -- supervisor.access
  ('8c4895a3-e12d-48de-a078-efb8a4ea2eb2', '1167b54a-0e93-43a2-94d7-c12e64eb56de', true, NULL);           -- supervisor.invite


-- ════════════════════════════════════════════════════════════════
-- SUPERVISOR PRO (ca28e46c) — acesso + convite + sessões + relatórios
-- ════════════════════════════════════════════════════════════════

INSERT INTO public.plan_features (plan_id, feature_id, enabled, limits) VALUES
  ('ca28e46c-0687-45d5-9406-0a0f56a5b625', '9ab8bdbb-838b-4946-aa5d-fd9cfdd257b3', true, NULL),           -- supervisor.access
  ('ca28e46c-0687-45d5-9406-0a0f56a5b625', '1167b54a-0e93-43a2-94d7-c12e64eb56de', true, NULL),           -- supervisor.invite
  ('ca28e46c-0687-45d5-9406-0a0f56a5b625', '761e4495-b46a-4791-9519-86ffe48dc47f', true, NULL),           -- supervisor.sessions
  ('ca28e46c-0687-45d5-9406-0a0f56a5b625', '7e82ee01-44f6-4b3f-9861-840c58e13f58', true, NULL);           -- supervisor.reports


-- ════════════════════════════════════════════════════════════════
-- PATIENT FREE (984c1f29) — sem features de plano (acesso via portal)
-- ════════════════════════════════════════════════════════════════
-- Pacientes acessam via portal_user, não precisam de features de plano.
-- O acesso é controlado pelo role 'portal_user' no profile.


DO $$
BEGIN
  RAISE NOTICE 'seed_012_plan_features: plan_features inseridos para todos os 7 planos.';
END;
$$;

COMMIT;
