-- FMCSA Motor Carrier Census Data
CREATE TABLE IF NOT EXISTS fmcsa_carriers (
    id SERIAL PRIMARY KEY,
    dot_number TEXT NOT NULL UNIQUE,
    legal_name TEXT NOT NULL,
    dba_name TEXT,
    carrier_operation TEXT,       -- C=carrier, B=broker, etc.
    authorized_for_hire BOOLEAN DEFAULT FALSE,
    private_property BOOLEAN DEFAULT FALSE,
    private_passenger_business BOOLEAN DEFAULT FALSE,
    exempt_for_hire BOOLEAN DEFAULT FALSE,
    hm_flag BOOLEAN DEFAULT FALSE,
    
    -- Contact
    email_address TEXT,
    telephone TEXT,
    
    -- Physical address
    phy_street TEXT,
    phy_city TEXT,
    phy_state TEXT,
    phy_zip TEXT,
    phy_country TEXT,
    
    -- Mailing address
    mailing_street TEXT,
    mailing_city TEXT,
    mailing_state TEXT,
    mailing_zip TEXT,
    mailing_country TEXT,
    
    -- Fleet info
    nbr_power_unit INTEGER,
    driver_total INTEGER,
    mcs150_mileage INTEGER,
    mcs150_mileage_year INTEGER,
    recent_mileage INTEGER,
    recent_mileage_year INTEGER,
    
    -- Compliance dates
    mcs150_date TEXT,              -- raw date string from FMCSA
    mcs150_parsed DATE,            -- parsed for querying
    add_date TEXT,                 -- registration date
    
    -- State info
    oic_state TEXT,                -- state of principal office
    
    -- Metadata
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_fmcsa_dot ON fmcsa_carriers(dot_number);
CREATE INDEX IF NOT EXISTS idx_fmcsa_email ON fmcsa_carriers(email_address) WHERE email_address IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_fmcsa_state ON fmcsa_carriers(phy_state);
CREATE INDEX IF NOT EXISTS idx_fmcsa_mcs150 ON fmcsa_carriers(mcs150_parsed);
CREATE INDEX IF NOT EXISTS idx_fmcsa_hire ON fmcsa_carriers(authorized_for_hire) WHERE authorized_for_hire = TRUE;
