-- Migration 086: Agent Scan Results persistence
-- Stores scan results so they survive page reloads and can be reviewed later

CREATE TABLE IF NOT EXISTS compliance_agent_scans (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id UUID NOT NULL DEFAULT '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e',
    user_id TEXT NOT NULL DEFAULT 'default',
    url TEXT NOT NULL,
    scan_type TEXT NOT NULL,  -- 'quick', 'scan', 'consent_test'
    analysis_mode TEXT NOT NULL DEFAULT 'post_launch',  -- 'pre_launch', 'post_launch'
    classification TEXT,
    risk_level TEXT,
    risk_score FLOAT DEFAULT 0,
    escalation_level TEXT,
    responsible_role TEXT,
    services JSONB DEFAULT '[]',
    findings JSONB DEFAULT '[]',
    corrections JSONB DEFAULT '[]',
    consent_test JSONB,
    text_references JSONB DEFAULT '[]',
    mandatory_checks JSONB DEFAULT '[]',
    summary_html TEXT,
    pages_scanned INT DEFAULT 0,
    pages_list JSONB DEFAULT '[]',
    email_sent BOOLEAN DEFAULT FALSE,
    email_recipient TEXT,
    created_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_agent_scans_tenant ON compliance_agent_scans(tenant_id);
CREATE INDEX IF NOT EXISTS idx_agent_scans_url ON compliance_agent_scans(url);
CREATE INDEX IF NOT EXISTS idx_agent_scans_created ON compliance_agent_scans(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_agent_scans_type ON compliance_agent_scans(scan_type);
