import logging
from sqlalchemy.orm import Session
from .models import AgencyTariffCode

logger = logging.getLogger(__name__)

AGENCY_TARIFF_CODES_DATA = [
    {"id": 1, "tariff_flag_code": "EP1", "agency_code": "EPA", "requirement_level": "M", "program_code": "ODS", "definition": "Ozone Depleting Substances specific data may be required"},
    {"id": 2, "tariff_flag_code": "EP2", "agency_code": "EPA", "requirement_level": "R", "program_code": "ODS", "definition": "Ozone Depleting Substances specific data is required"},
    {"id": 3, "tariff_flag_code": "EP3", "agency_code": "EPA", "requirement_level": "M", "program_code": "VNE", "definition": "Vehicle and Engines specific data may be required"},
    {"id": 4, "tariff_flag_code": "EP4", "agency_code": "EPA", "requirement_level": "R", "program_code": "VNE", "definition": "Vehicle and Engines specific data is required"},
    {"id": 5, "tariff_flag_code": "EP5", "agency_code": "EPA", "requirement_level": "M", "program_code": "PS1", "definition": "Pesticides specific data may be required"},
    {"id": 6, "tariff_flag_code": "EP5", "agency_code": "EPA", "requirement_level": "M", "program_code": "PS2", "definition": "Pesticides specific data may be required"},
    {"id": 7, "tariff_flag_code": "EP5", "agency_code": "EPA", "requirement_level": "M", "program_code": "PS3", "definition": "Pesticides specific data may be required"},
    {"id": 8, "tariff_flag_code": "EP6", "agency_code": "EPA", "requirement_level": "R", "program_code": "PS1", "definition": "Pesticides specific data is required"},
    {"id": 9, "tariff_flag_code": "EP6", "agency_code": "EPA", "requirement_level": "R", "program_code": "PS2", "definition": "Pesticides specific data is required"},
    {"id": 10, "tariff_flag_code": "EP6", "agency_code": "EPA", "requirement_level": "R", "program_code": "PS3", "definition": "Pesticides specific data is required"},
    {"id": 11, "tariff_flag_code": "EP7", "agency_code": "EPA", "requirement_level": "M", "program_code": "TS1", "definition": "Toxic Substances Control Act specific data may be required"},
    {"id": 12, "tariff_flag_code": "EP7", "agency_code": "EPA", "requirement_level": "M", "program_code": "TS2", "definition": "Toxic Substances Control Act specific data may be required"},
    {"id": 13, "tariff_flag_code": "EP8", "agency_code": "EPA", "requirement_level": "R", "program_code": "TS1", "definition": "Toxic Substances Control Act specific data is required"},
    {"id": 14, "tariff_flag_code": "EP8", "agency_code": "EPA", "requirement_level": "R", "program_code": "TS2", "definition": "Toxic Substances Control Act specific data is required"},
    {"id": 15, "tariff_flag_code": "FS3", "agency_code": "FSI", "requirement_level": "M", "program_code": "FSI", "definition": "FSIS data may be required. Applicable to all FSIS programs"},
    {"id": 16, "tariff_flag_code": "FS4", "agency_code": "FSI", "requirement_level": "R", "program_code": "FSI", "definition": "FSIS data is required. Applicable to all FSIS programs"},
    {"id": 17, "tariff_flag_code": "NM1", "agency_code": "NMF", "requirement_level": "M", "program_code": "370", "definition": "370 specific data may be required"},
    {"id": 18, "tariff_flag_code": "NM2", "agency_code": "NMF", "requirement_level": "R", "program_code": "370", "definition": "370 specific data is required"},
    {"id": 19, "tariff_flag_code": "NM3", "agency_code": "NMF", "requirement_level": "M", "program_code": "AMR", "definition": "Antarctic Marine Living Resources specific data may be required"},
    {"id": 20, "tariff_flag_code": "NM4", "agency_code": "NMF", "requirement_level": "R", "program_code": "AMR", "definition": "Antarctic Marine Living Resources is required"},
    {"id": 21, "tariff_flag_code": "NM5", "agency_code": "NMF", "requirement_level": "M", "program_code": "HMS", "definition": "Highly Migratory Species specific data may be required"},
    {"id": 22, "tariff_flag_code": "NM6", "agency_code": "NMF", "requirement_level": "R", "program_code": "HMS", "definition": "Highly Migratory Species specific data is required"},
    {"id": 23, "tariff_flag_code": "DT1", "agency_code": "NHT", "requirement_level": "M", "program_code": "MV", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data may be required"},
    {"id": 24, "tariff_flag_code": "DT1", "agency_code": "NHT", "requirement_level": "M", "program_code": "REI", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data may be required"},
    {"id": 25, "tariff_flag_code": "DT1", "agency_code": "NHT", "requirement_level": "M", "program_code": "TPE", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data may be required"},
    {"id": 26, "tariff_flag_code": "DT1", "agency_code": "NHT", "requirement_level": "M", "program_code": "OEI", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data may be required"},
    {"id": 27, "tariff_flag_code": "DT1", "agency_code": "NHT", "requirement_level": "M", "program_code": "OFF", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data may be required"},
    {"id": 28, "tariff_flag_code": "DT2", "agency_code": "NHT", "requirement_level": "R", "program_code": "MVS", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data is required"},
    {"id": 29, "tariff_flag_code": "DT2", "agency_code": "NHT", "requirement_level": "R", "program_code": "REI", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data is required"},
    {"id": 30, "tariff_flag_code": "DT2", "agency_code": "NHT", "requirement_level": "R", "program_code": "TPE", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data is required"},
    {"id": 31, "tariff_flag_code": "DT2", "agency_code": "NHT", "requirement_level": "R", "program_code": "OEI", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data is required"},
    {"id": 32, "tariff_flag_code": "DT2", "agency_code": "NHT", "requirement_level": "R", "program_code": "OFF", "definition": "DOT/National Highway Traffic Safety Administration HS-7 data is required"},
    {"id": 33, "tariff_flag_code": "AL1", "agency_code": "APH", "requirement_level": "M", "program_code": "APL", "definition": "Lacey Act specific data may be required"},
    {"id": 34, "tariff_flag_code": "AL2", "agency_code": "APH", "requirement_level": "R", "program_code": "APL", "definition": "Lacey Act specific data is required"},
    {"id": 35, "tariff_flag_code": "FD1", "agency_code": "FDA", "requirement_level": "M", "program_code": "BIO", "definition": "FDA data may be required 801(a)"},
    {"id": 36, "tariff_flag_code": "FD1", "agency_code": "FDA", "requirement_level": "M", "program_code": "COS", "definition": "FDA data may be required 801(a)"},
    {"id": 37, "tariff_flag_code": "FD1", "agency_code": "FDA", "requirement_level": "M", "program_code": "DEV", "definition": "FDA data may be required 801(a)"},
    {"id": 38, "tariff_flag_code": "FD1", "agency_code": "FDA", "requirement_level": "M", "program_code": "DRU", "definition": "FDA data may be required 801(a)"},
    {"id": 39, "tariff_flag_code": "FD1", "agency_code": "FDA", "requirement_level": "M", "program_code": "FOO", "definition": "FDA data may be required 801(a)"},
    {"id": 40, "tariff_flag_code": "FD1", "agency_code": "FDA", "requirement_level": "M", "program_code": "RAD", "definition": "FDA data may be required 801(a)"},
    {"id": 41, "tariff_flag_code": "FD1", "agency_code": "FDA", "requirement_level": "M", "program_code": "TOB", "definition": "FDA data may be required 801(a)"},
    {"id": 42, "tariff_flag_code": "FD1", "agency_code": "FDA", "requirement_level": "M", "program_code": "VME", "definition": "FDA data may be required 801(a)"},
    {"id": 43, "tariff_flag_code": "FD2", "agency_code": "FDA", "requirement_level": "R", "program_code": "BIO", "definition": "FDA data Required 801(a)"},
    {"id": 44, "tariff_flag_code": "FD2", "agency_code": "FDA", "requirement_level": "R", "program_code": "COS", "definition": "FDA data Required 801(a)"},
    {"id": 45, "tariff_flag_code": "FD2", "agency_code": "FDA", "requirement_level": "R", "program_code": "DEV", "definition": "FDA data Required 801(a)"},
    {"id": 46, "tariff_flag_code": "FD2", "agency_code": "FDA", "requirement_level": "R", "program_code": "DRU", "definition": "FDA data Required 801(a)"},
    {"id": 47, "tariff_flag_code": "FD2", "agency_code": "FDA", "requirement_level": "R", "program_code": "FOO", "definition": "FDA data Required 801(a)"},
    {"id": 48, "tariff_flag_code": "FD2", "agency_code": "FDA", "requirement_level": "R", "program_code": "RAD", "definition": "FDA data Required 801(a)"},
    {"id": 49, "tariff_flag_code": "FD2", "agency_code": "FDA", "requirement_level": "R", "program_code": "TOB", "definition": "FDA data Required 801(a)"},
    {"id": 50, "tariff_flag_code": "FD2", "agency_code": "FDA", "requirement_level": "R", "program_code": "VME", "definition": "FDA data Required 801(a)"},
    {"id": 51, "tariff_flag_code": "FD3", "agency_code": "FDA", "requirement_level": "M", "program_code": "BIO", "definition": "FDA Prior Notice Data may be required 801(m)"},
    {"id": 52, "tariff_flag_code": "FD3", "agency_code": "FDA", "requirement_level": "M", "program_code": "COS", "definition": "FDA Prior Notice Data may be required 801(m)"},
    {"id": 53, "tariff_flag_code": "FD3", "agency_code": "FDA", "requirement_level": "M", "program_code": "DEV", "definition": "FDA Prior Notice Data may be required 801(m)"},
    {"id": 54, "tariff_flag_code": "FD3", "agency_code": "FDA", "requirement_level": "M", "program_code": "DRU", "definition": "FDA Prior Notice Data may be required 801(m)"},
    {"id": 55, "tariff_flag_code": "FD3", "agency_code": "FDA", "requirement_level": "M", "program_code": "FOO", "definition": "FDA Prior Notice Data may be required 801(m)"},
    {"id": 56, "tariff_flag_code": "FD3", "agency_code": "FDA", "requirement_level": "M", "program_code": "RAD", "definition": "FDA Prior Notice Data may be required 801(m)"},
    {"id": 57, "tariff_flag_code": "FD3", "agency_code": "FDA", "requirement_level": "M", "program_code": "TOB", "definition": "FDA Prior Notice Data may be required 801(m)"},
    {"id": 58, "tariff_flag_code": "FD3", "agency_code": "FDA", "requirement_level": "M", "program_code": "VME", "definition": "FDA Prior Notice Data may be required 801(m)"},
    {"id": 59, "tariff_flag_code": "FD4", "agency_code": "FDA", "requirement_level": "R", "program_code": "BIO", "definition": "FDA Prior Notice Data is required 801(m)"},
    {"id": 60, "tariff_flag_code": "FD4", "agency_code": "FDA", "requirement_level": "R", "program_code": "COS", "definition": "FDA Prior Notice Data is required 801(m)"},
    {"id": 61, "tariff_flag_code": "FD4", "agency_code": "FDA", "requirement_level": "R", "program_code": "DEV", "definition": "FDA Prior Notice Data is required 801(m)"},
    {"id": 62, "tariff_flag_code": "FD4", "agency_code": "FDA", "requirement_level": "R", "program_code": "DRU", "definition": "FDA Prior Notice Data is required 801(m)"},
    {"id": 63, "tariff_flag_code": "FD4", "agency_code": "FDA", "requirement_level": "R", "program_code": "FOO", "definition": "FDA Prior Notice Data is required 801(m)"},
    {"id": 64, "tariff_flag_code": "FD4", "agency_code": "FDA", "requirement_level": "R", "program_code": "RAD", "definition": "FDA Prior Notice Data is required 801(m)"},
    {"id": 65, "tariff_flag_code": "FD4", "agency_code": "FDA", "requirement_level": "R", "program_code": "TOB", "definition": "FDA Prior Notice Data is required 801(m)"},
    {"id": 66, "tariff_flag_code": "FD4", "agency_code": "FDA", "requirement_level": "R", "program_code": "VME", "definition": "FDA Prior Notice Data is required 801(m)"},
    {"id": 67, "tariff_flag_code": "AM1", "agency_code": "AMS", "requirement_level": "M", "program_code": "EG", "definition": "USDA/Agricultural Marketing Service Data related to egg products may be required"},
    {"id": 68, "tariff_flag_code": "AM2", "agency_code": "AMS", "requirement_level": "R", "program_code": "EG", "definition": "USDA/Agricultural Marketing Service Data Related to shell eggs is required"},
    {"id": 69, "tariff_flag_code": "AM3", "agency_code": "AMS", "requirement_level": "M", "program_code": "MO", "definition": "USDA/Agricultural Marketing Service Data Related to marketing orders may be required"},
    {"id": 70, "tariff_flag_code": "AM4", "agency_code": "AMS", "requirement_level": "R", "program_code": "MO", "definition": "USDA/Agricultural Marketing Service Data Related to marketing orders is required"},
    {"id": 71, "tariff_flag_code": "AM6", "agency_code": "AMS", "requirement_level": "R", "program_code": "PN", "definition": "USDA/Agriculture Marketing Service Data related to peanuts is required"},
    {"id": 72, "tariff_flag_code": "TB1", "agency_code": "TTB", "requirement_level": "M", "program_code": "BER", "definition": "TTB data may be required. Applicable to all TTB programs."},
    {"id": 73, "tariff_flag_code": "TB1", "agency_code": "TTB", "requirement_level": "M", "program_code": "WIN", "definition": "TTB data may be required. Applicable to all TTB programs."},
    {"id": 74, "tariff_flag_code": "TB1", "agency_code": "TTB", "requirement_level": "M", "program_code": "DSP", "definition": "TTB data may be required. Applicable to all TTB programs."},
    {"id": 75, "tariff_flag_code": "TB1", "agency_code": "TTB", "requirement_level": "M", "program_code": "TOB", "definition": "TTB data may be required. Applicable to all TTB programs."},
    {"id": 76, "tariff_flag_code": "TB2", "agency_code": "TTB", "requirement_level": "R", "program_code": "BER", "definition": "TTB data is required. Applicable to all TTB programs."},
    {"id": 77, "tariff_flag_code": "TB2", "agency_code": "TTB", "requirement_level": "R", "program_code": "WIN", "definition": "TTB data is required. Applicable to all TTB programs."},
    {"id": 78, "tariff_flag_code": "TB2", "agency_code": "TTB", "requirement_level": "R", "program_code": "DSP", "definition": "TTB data is required. Applicable to all TTB programs."},
    {"id": 79, "tariff_flag_code": "TB2", "agency_code": "TTB", "requirement_level": "R", "program_code": "TOB", "definition": "TTB data is required. Applicable to all TTB programs."},
    {"id": 80, "tariff_flag_code": "AQ1", "agency_code": "APH", "requirement_level": "M", "program_code": "AAC", "definition": "APHIS data may be required"},
    {"id": 81, "tariff_flag_code": "AQ1", "agency_code": "APH", "requirement_level": "M", "program_code": "APQ", "definition": "APHIS data may be required"},
    {"id": 82, "tariff_flag_code": "AQ1", "agency_code": "APH", "requirement_level": "M", "program_code": "AVS", "definition": "APHIS data may be required"},
    {"id": 83, "tariff_flag_code": "AQ1", "agency_code": "APH", "requirement_level": "M", "program_code": "ABS", "definition": "APHIS data may be required"},
    {"id": 84, "tariff_flag_code": "AQ2", "agency_code": "APH", "requirement_level": "R", "program_code": "AAC", "definition": "APHIS data is required"},
    {"id": 85, "tariff_flag_code": "AQ2", "agency_code": "APH", "requirement_level": "R", "program_code": "APQ", "definition": "APHIS data is required"},
    {"id": 86, "tariff_flag_code": "AQ2", "agency_code": "APH", "requirement_level": "R", "program_code": "AVS", "definition": "APHIS data is required"},
    {"id": 87, "tariff_flag_code": "AQ2", "agency_code": "APH", "requirement_level": "R", "program_code": "ABS", "definition": "APHIS data is required"},
    {"id": 88, "tariff_flag_code": "OM1", "agency_code": "OMC", "requirement_level": "M", "program_code": "OMC", "definition": "Office of Marine Conservation data may be required"},
    {"id": 89, "tariff_flag_code": "OM2", "agency_code": "OMC", "requirement_level": "R", "program_code": "OMC", "definition": "Office of Marine Conservation data is required"},
    {"id": 90, "tariff_flag_code": "FW2", "agency_code": "FWS", "requirement_level": "R", "program_code": "FWS", "definition": "U.S. Fish and Wildlife Service data is required"},
    {"id": 91, "tariff_flag_code": "DE1", "agency_code": "DEA", "requirement_level": "M", "program_code": "DEA", "definition": "U.S. Drug Enforcement Administration data may be required"},
]

def seed_agency_tariff_codes(db: Session):
    """Seed Agency Tariff Codes catalog data"""
    logger.info("Seeding Agency Tariff Codes...")
    for item in AGENCY_TARIFF_CODES_DATA:
        db_item = db.query(AgencyTariffCode).filter(AgencyTariffCode.id == item["id"]).first()
        if not db_item:
            logger.info(f"Adding agency tariff code: {item['id']}")
            new_item = AgencyTariffCode(**item)
            db.add(new_item)
        else:
            # Update fields if changed
            db_item.tariff_flag_code = item["tariff_flag_code"]
            db_item.agency_code = item["agency_code"]
            db_item.requirement_level = item["requirement_level"]
            db_item.program_code = item["program_code"]
            db_item.definition = item["definition"]
    db.commit()
    logger.info("Agency Tariff Codes seeding completed.")
