{BEGIN,"-- Helpful indexes for fast lookups
CREATE INDEX IF NOT EXISTS idx_system_settings_key ON public.system_settings (setting_key)","CREATE INDEX IF NOT EXISTS idx_org_settings_org_key ON public.organization_settings (organization_id, setting_key)","-- Seed global (system) settings if they don't already exist
DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM public.system_settings WHERE setting_key = 'messaging_global') THEN
    INSERT INTO public.system_settings (id, setting_key, setting_value, description)
    VALUES (
      gen_random_uuid(),
      'messaging_global',
      jsonb_build_object(
        'whatsapp_cloud', jsonb_build_object(
          'enabled', true,
          'business_phone_id', NULL,
          'from_phone_number', NULL,
          'access_token_secret', 'WHATSAPP_CLOUD_ACCESS_TOKEN'
        ),
        'twilio', jsonb_build_object(
          'enabled', false,
          'account_sid_secret', 'TWILIO_ACCOUNT_SID',
          'auth_token_secret', 'TWILIO_AUTH_TOKEN',
          'from_phone_number', NULL
        )
      ),
      'Global messaging providers configuration managed by system admin'
    );
  END IF;

  IF NOT EXISTS (SELECT 1 FROM public.system_settings WHERE setting_key = 'tts_global') THEN
    INSERT INTO public.system_settings (id, setting_key, setting_value, description)
    VALUES (
      gen_random_uuid(),
      'tts_global',
      jsonb_build_object(
        'free_tts', jsonb_build_object(
          'enabled', true,
          'default_voice', 'auto'
        ),
        'elevenlabs', jsonb_build_object(
          'enabled', false,
          'api_key_secret', 'ELEVENLABS_API_KEY',
          'default_voice_id', 'TX3LPaxmHKxFdv7VOQHJ',
          'model', 'eleven_turbo_v2_5'
        ),
        'default_provider', 'free_tts'
      ),
      'Global TTS providers configuration managed by system admin'
    );
  END IF;
END $$","-- Seed per-organization defaults so the system admin can toggle allowed providers per company
DO $$
DECLARE
  org RECORD;
BEGIN
  FOR org IN SELECT id FROM public.organizations LOOP
    -- Allowed messaging providers for this organization
    IF NOT EXISTS (
      SELECT 1 FROM public.organization_settings 
      WHERE organization_id = org.id AND setting_key = 'allowed_messaging'
    ) THEN
      INSERT INTO public.organization_settings (id, organization_id, setting_key, setting_value)
      VALUES (
        gen_random_uuid(),
        org.id,
        'allowed_messaging',
        jsonb_build_object(
          'whatsapp_cloud', true,
          'twilio', false
        )
      );
    END IF;

    -- Allowed TTS providers for this organization (default to free_tts)
    IF NOT EXISTS (
      SELECT 1 FROM public.organization_settings 
      WHERE organization_id = org.id AND setting_key = 'allowed_tts'
    ) THEN
      INSERT INTO public.organization_settings (id, organization_id, setting_key, setting_value)
      VALUES (
        gen_random_uuid(),
        org.id,
        'allowed_tts',
        jsonb_build_object(
          'elevenlabs', false,
          'free_tts', true,
          'default_provider', 'free_tts'
        )
      );
    END IF;
  END LOOP;
END $$",COMMIT}