-- 022_fcc_rmd.sql
-- FCC Robocall Mitigation Database (RMD) registry cache.
-- Populated by scripts/workers/fcc_rmd_scraper.py — run manually.

BEGIN;

CREATE TABLE IF NOT EXISTS fcc_rmd (
    id                          SERIAL PRIMARY KEY,
    -- RMD identifier (e.g. "RMD0001410")
    rmd_number                  TEXT UNIQUE NOT NULL,
    frn                         TEXT,
    business_name               TEXT,
    business_address            TEXT,
    foreign_voice_provider      BOOLEAN DEFAULT FALSE,
    country                     TEXT,
    other_frns                  TEXT,
    other_dba_names             TEXT,
    previous_dba_names          TEXT,
    -- Robocall mitigation contact (from CSV)
    contact_name                TEXT,
    contact_title               TEXT,
    contact_department          TEXT,
    contact_business_address    TEXT,
    contact_country             TEXT,
    contact_telephone_number    TEXT,
    contact_phone_extension     TEXT,
    -- Contact email — scraped from individual record page (not in CSV)
    contact_email               TEXT,
    contact_email_scraped_at    TIMESTAMPTZ,
    -- Implementation
    implementation              TEXT,
    voice_service_provider      BOOLEAN DEFAULT FALSE,
    gateway_provider            BOOLEAN DEFAULT FALSE,
    intermediate_provider       BOOLEAN DEFAULT FALSE,
    -- Dates
    last_updated                DATE,
    last_recertified            DATE,
    -- Direct link to the RMD filing page
    filing_url                  TEXT,
    -- sys_id extracted from filing_url — used as Playwright scrape target
    servicenow_sys_id           TEXT,
    -- Audit
    csv_imported_at             TIMESTAMPTZ DEFAULT now(),
    updated_at                  TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_fcc_rmd_number      ON fcc_rmd(rmd_number);
CREATE INDEX IF NOT EXISTS idx_fcc_rmd_frn         ON fcc_rmd(frn);
CREATE INDEX IF NOT EXISTS idx_fcc_rmd_email       ON fcc_rmd(contact_email);
CREATE INDEX IF NOT EXISTS idx_fcc_rmd_business    ON fcc_rmd(business_name);
CREATE INDEX IF NOT EXISTS idx_fcc_rmd_no_email    ON fcc_rmd(rmd_number) WHERE contact_email IS NULL;

COMMIT;
