-- 077: Log every FCC compliance check for analytics and follow-up.
-- Tracks who runs checks, what results they see, and whether they convert.

CREATE TABLE IF NOT EXISTS compliance_check_log (
    id              SERIAL PRIMARY KEY,
    frn             TEXT NOT NULL,
    entity_name     TEXT,
    ip_address      TEXT,
    user_agent      TEXT,
    referrer        TEXT,
    -- Results summary
    total_checks    INTEGER DEFAULT 0,
    issues_found    INTEGER DEFAULT 0,
    severity        TEXT,          -- critical, major, minor, clean
    check_slugs     TEXT[],        -- which services were flagged
    -- Timing
    response_ms     INTEGER,       -- how long the API call took
    -- Conversion tracking
    clicked_order   BOOLEAN DEFAULT FALSE,
    order_number    TEXT,          -- if they eventually ordered
    -- Metadata
    created_at      TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_check_log_frn ON compliance_check_log(frn);
CREATE INDEX IF NOT EXISTS idx_check_log_created ON compliance_check_log(created_at);
