-- 040: Local cache of FCC Form 499 filer database
-- Source: https://apps.fcc.gov/cgb/form499/499results.cfm?XML=TRUE
-- Updated daily by scraper. Used for FRN → entity name lookup
-- so compliance wizard doesn't need to hit the FCC site.

CREATE TABLE IF NOT EXISTS fcc_499_filers (
    id              SERIAL PRIMARY KEY,
    filer_id        TEXT UNIQUE,             -- USAC 499 Filer ID
    frn             TEXT,                    -- FCC Registration Number (10-digit)
    legal_name      TEXT NOT NULL,
    trade_name      TEXT,
    state           TEXT,                    -- 2-letter state code
    service_type    TEXT,                    -- Primary service classification
    holding_company TEXT,
    status          TEXT,                    -- Active / Inactive
    last_scraped_at TIMESTAMPTZ DEFAULT NOW(),
    created_at      TIMESTAMPTZ DEFAULT NOW(),
    updated_at      TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_fcc_499_filers_frn ON fcc_499_filers(frn) WHERE frn IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_fcc_499_filers_name ON fcc_499_filers USING gin(to_tsvector('english', legal_name));
