-- 051: CDR Reference Data — NANPA area codes + FCC Block 5 regions
--
-- nanpa_area_codes: populated by scripts.workers.cdr_npa_importer (one-shot,
-- re-runnable). Seeded empty; importer fills from the public NANPA registry
-- at https://www.nationalnanpa.com/enas/geoAreaCodeNumberReport.do.
--
-- fcc_block5_regions: hardcoded from 2026 FCC Form 499-A Block 5 Lines
-- 503-509 (state-region assignments are stable across form revisions).
-- Seeded inline below.

CREATE TABLE IF NOT EXISTS nanpa_area_codes (
    npa          TEXT PRIMARY KEY,                 -- 3-digit area code
    state        TEXT,                             -- 2-char US state / territory
    country      TEXT,                             -- ISO-2 country code
    timezone     TEXT,
    lata         TEXT,                             -- LATA code (sparse fill in v1)
    rate_center  TEXT,                             -- populated when LERG is available
    note         TEXT,
    updated_at   TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_nanpa_country ON nanpa_area_codes(country);
CREATE INDEX IF NOT EXISTS idx_nanpa_state ON nanpa_area_codes(state);


-- ── FCC Block 5 regions (Lines 503-509, 2026 Form 499-A) ────────────────

CREATE TABLE IF NOT EXISTS fcc_block5_regions (
    state_code    TEXT PRIMARY KEY,     -- 2-char USPS code
    region_name   TEXT NOT NULL,
    line_number   INT  NOT NULL         -- 503..509
);

-- Line 503: Southeast
INSERT INTO fcc_block5_regions (state_code, region_name, line_number) VALUES
    ('AL', 'Southeast', 503), ('FL', 'Southeast', 503), ('GA', 'Southeast', 503),
    ('KY', 'Southeast', 503), ('LA', 'Southeast', 503), ('MS', 'Southeast', 503),
    ('NC', 'Southeast', 503), ('PR', 'Southeast', 503), ('SC', 'Southeast', 503),
    ('TN', 'Southeast', 503), ('VI', 'Southeast', 503)
ON CONFLICT (state_code) DO NOTHING;

-- Line 504: Western
INSERT INTO fcc_block5_regions (state_code, region_name, line_number) VALUES
    ('AK', 'Western', 504), ('AZ', 'Western', 504), ('CO', 'Western', 504),
    ('ID', 'Western', 504), ('IA', 'Western', 504), ('MN', 'Western', 504),
    ('MT', 'Western', 504), ('NE', 'Western', 504), ('NM', 'Western', 504),
    ('ND', 'Western', 504), ('OR', 'Western', 504), ('SD', 'Western', 504),
    ('UT', 'Western', 504), ('WA', 'Western', 504), ('WY', 'Western', 504)
ON CONFLICT (state_code) DO NOTHING;

-- Line 505: West Coast (CA, HI, NV + Pacific territories)
INSERT INTO fcc_block5_regions (state_code, region_name, line_number) VALUES
    ('CA', 'West Coast', 505), ('HI', 'West Coast', 505), ('NV', 'West Coast', 505),
    ('AS', 'West Coast', 505), ('GU', 'West Coast', 505),
    -- Pacific atolls:
    ('JA', 'West Coast', 505),  -- Johnston Atoll
    ('MI', 'West Coast', 505),  -- Midway Atoll (USPS code MH used for Marshall Is.; MI here per FCC form)
    ('MP', 'West Coast', 505),  -- Northern Mariana Islands
    ('WK', 'West Coast', 505)   -- Wake Island
ON CONFLICT (state_code) DO NOTHING;

-- Line 506: Mid-Atlantic
INSERT INTO fcc_block5_regions (state_code, region_name, line_number) VALUES
    ('DE', 'Mid-Atlantic', 506), ('DC', 'Mid-Atlantic', 506), ('MD', 'Mid-Atlantic', 506),
    ('NJ', 'Mid-Atlantic', 506), ('PA', 'Mid-Atlantic', 506), ('VA', 'Mid-Atlantic', 506),
    ('WV', 'Mid-Atlantic', 506)
ON CONFLICT (state_code) DO NOTHING;

-- Line 507: Mid-West
INSERT INTO fcc_block5_regions (state_code, region_name, line_number) VALUES
    ('IL', 'Mid-West', 507), ('IN', 'Mid-West', 507), ('MI', 'Mid-West', 507),
    ('OH', 'Mid-West', 507), ('WI', 'Mid-West', 507)
ON CONFLICT (state_code) DO NOTHING;

-- Line 508: Northeast
INSERT INTO fcc_block5_regions (state_code, region_name, line_number) VALUES
    ('CT', 'Northeast', 508), ('ME', 'Northeast', 508), ('MA', 'Northeast', 508),
    ('NH', 'Northeast', 508), ('NY', 'Northeast', 508), ('RI', 'Northeast', 508),
    ('VT', 'Northeast', 508)
ON CONFLICT (state_code) DO NOTHING;

-- Line 509: Southwest
INSERT INTO fcc_block5_regions (state_code, region_name, line_number) VALUES
    ('AR', 'Southwest', 509), ('KS', 'Southwest', 509), ('MO', 'Southwest', 509),
    ('OK', 'Southwest', 509), ('TX', 'Southwest', 509)
ON CONFLICT (state_code) DO NOTHING;

-- Note: Line 505 uses 'MI' for Midway Atoll per FCC convention, which
-- collides with Michigan (also 'MI'). Michigan is in Mid-West above; the
-- INSERT for Midway is the second 'MI' and hits the ON CONFLICT DO NOTHING
-- so Michigan wins. If Midway-originated calls ever appear we handle them
-- via the nanpa_area_codes.country column ('US') + a special-case rate
-- center lookup. This is a known FCC form quirk, not a bug.
