-- ============================================================
-- Migration: company_coverage
-- Timestamp: 20260322220000
-- ============================================================

-- 1. company_coverage_queue table
CREATE TABLE IF NOT EXISTS public.company_coverage_queue (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  company_name TEXT NOT NULL,
  source_article_id TEXT,
  source_article_title TEXT,
  source_article_slug TEXT,
  source_type TEXT DEFAULT 'native', -- 'native' or 'imported'
  ai_draft_title TEXT,
  ai_draft_content TEXT,
  ai_draft_excerpt TEXT,
  status TEXT NOT NULL DEFAULT 'queued', -- 'queued', 'approved', 'published', 'dismissed'
  reminder_sent BOOLEAN DEFAULT FALSE,
  reminder_sent_at TIMESTAMPTZ,
  reviewed_by UUID REFERENCES public.user_profiles(id) ON DELETE SET NULL,
  reviewed_at TIMESTAMPTZ,
  published_article_id UUID,
  created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_company_coverage_status ON public.company_coverage_queue(status);
CREATE INDEX IF NOT EXISTS idx_company_coverage_company ON public.company_coverage_queue(company_name);
CREATE INDEX IF NOT EXISTS idx_company_coverage_created ON public.company_coverage_queue(created_at DESC);

-- 2. autopilot_settings table (single-row config)
CREATE TABLE IF NOT EXISTS public.autopilot_settings (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  autopilot_enabled BOOLEAN NOT NULL DEFAULT FALSE,
  reminder_email TEXT NOT NULL DEFAULT 'ryan.salazar@relevantmediaproperties.com',
  scan_interval_hours INTEGER NOT NULL DEFAULT 24,
  last_scan_at TIMESTAMPTZ,
  last_scan_articles_processed INTEGER DEFAULT 0,
  last_scan_companies_found INTEGER DEFAULT 0,
  updated_by UUID REFERENCES public.user_profiles(id) ON DELETE SET NULL,
  updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

-- Insert default settings row
INSERT INTO public.autopilot_settings (autopilot_enabled, reminder_email)
VALUES (FALSE, 'ryan.salazar@relevantmediaproperties.com')
ON CONFLICT DO NOTHING;

-- 3. Enable RLS
ALTER TABLE public.company_coverage_queue ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.autopilot_settings ENABLE ROW LEVEL SECURITY;

-- 4. RLS Policies for company_coverage_queue
DROP POLICY IF EXISTS "admin_manage_company_coverage" ON public.company_coverage_queue;
CREATE POLICY "admin_manage_company_coverage"
ON public.company_coverage_queue
FOR ALL
TO authenticated
USING (true)
WITH CHECK (true);

-- 5. RLS Policies for autopilot_settings
DROP POLICY IF EXISTS "admin_manage_autopilot_settings" ON public.autopilot_settings;
CREATE POLICY "admin_manage_autopilot_settings"
ON public.autopilot_settings
FOR ALL
TO authenticated
USING (true)
WITH CHECK (true);

-- 6. Trigger for updated_at on company_coverage_queue
DROP TRIGGER IF EXISTS update_company_coverage_updated_at ON public.company_coverage_queue;
CREATE TRIGGER update_company_coverage_updated_at
  BEFORE UPDATE ON public.company_coverage_queue
  FOR EACH ROW EXECUTE FUNCTION public.update_updated_at();

-- 7. Trigger for updated_at on autopilot_settings
DROP TRIGGER IF EXISTS update_autopilot_settings_updated_at ON public.autopilot_settings;
CREATE TRIGGER update_autopilot_settings_updated_at
  BEFORE UPDATE ON public.autopilot_settings
  FOR EACH ROW EXECUTE FUNCTION public.update_updated_at();
