import { DBFFile } from "dbffile";
import errorTypeCheck from "../../util/errorTypeCheck";
import { ad1FieldLineDescriptors } from "../util/ems-interface/fielddescriptors/ad1-field-descriptors";
import {
  deleteEmsFileIfExists,
  generateEmsOutFilePath,
} from "../util/ems-util";
import { EmsPartsOrder } from "./ems-parts-order-interfaces";

const EmsPartsOrderGenerateAd1File = async (
  partsOrder: EmsPartsOrder,
): Promise<boolean> => {
  try {
    const records = [
      {
        INS_CO_ID: partsOrder.job.ins_co_nm,
        INS_CO_NM: partsOrder.job.ins_co_nm,
        INS_ADDR1: partsOrder.job.ins_addr1,
        INS_ADDR2: partsOrder.job.ins_addr2,
        INS_CITY: partsOrder.job.ins_city,
        INS_ST: partsOrder.job.ins_st,
        INS_ZIP: partsOrder.job.ins_zip,
        INS_CTRY: partsOrder.job.ins_ctry,
        INS_PH1: partsOrder.job.ins_ph1,
        INS_PH1X: partsOrder.job.ins_ph1x,
        INS_PH2: partsOrder.job.ins_ph2,
        INS_PH2X: partsOrder.job.ins_ph2x,
        INS_FAX: partsOrder.job.ins_fax,
        INS_FAXX: partsOrder.job.ins_faxx,
        INS_CT_LN: partsOrder.job.ins_ct_ln,
        INS_CT_FN: partsOrder.job.ins_ct_fn,
        INS_TITLE: partsOrder.job.ins_title,
        INS_CT_PH: partsOrder.job.ins_ct_ph,
        INS_CT_PHX: partsOrder.job.ins_ct_phx,
        INS_EA: partsOrder.job.ins_ea,
        INS_MEMO: partsOrder.job.ins_memo,
        POLICY_NO: partsOrder.job.policy_no,
        DED_AMT: partsOrder.job.ded_amt,
        DED_STATUS: partsOrder.job.ded_status,
        ASGN_NO: partsOrder.job.asgn_no,
        ASGN_DATE: partsOrder.job.asgn_date
          ? new Date(partsOrder.job.asgn_date)
          : null,
        ASGN_TYPE: partsOrder.job.asgn_type,
        CLM_NO: partsOrder.job.clm_no,
        CLM_OFC_ID: partsOrder.job.clm_ofc_id,
        CLM_OFC_NM: partsOrder.job.clm_ofc_nm,
        CLM_ADDR1: partsOrder.job.clm_addr1,
        CLM_ADDR2: partsOrder.job.clm_addr2,
        CLM_CITY: partsOrder.job.clm_city,
        CLM_ST: partsOrder.job.clm_st,
        CLM_ZIP: partsOrder.job.clm_zip,
        CLM_CTRY: partsOrder.job.clm_ctry,
        CLM_PH1: partsOrder.job.clm_ph1,
        CLM_PH1X: partsOrder.job.clm_ph1x,
        CLM_PH2: partsOrder.job.clm_ph2,
        CLM_PH2X: partsOrder.job.clm_ph2x,
        CLM_FAX: partsOrder.job.clm_fax,
        CLM_FAXX: partsOrder.job.clm_faxx,
        CLM_CT_LN: partsOrder.job.clm_ct_ln,
        CLM_CT_FN: partsOrder.job.clm_ct_fn,
        CLM_TITLE: partsOrder.job.clm_title,
        CLM_CT_PH: partsOrder.job.clm_ct_ph,
        CLM_CT_PHX: partsOrder.job.clm_ct_phx,
        CLM_EA: partsOrder.job.clm_ea,
        PAYEE_NMS: partsOrder.job.payee_nms,
        PAY_TYPE: partsOrder.job.pay_type,
        PAY_DATE: partsOrder.job.pay_date,
        PAY_CHKNM: null, // Explicitly set to null as in original code
        PAY_AMT: null, // Explicitly set to null as in original code
        PAY_MEMO: partsOrder.job.pay_memo,
        AGT_CO_ID: partsOrder.job.agt_co_id,
        AGT_CO_NM: partsOrder.job.agt_co_nm,
        AGT_ADDR1: partsOrder.job.agt_addr1,
        AGT_ADDR2: partsOrder.job.agt_addr2,
        AGT_CITY: partsOrder.job.agt_city,
        AGT_ST: partsOrder.job.agt_st,
        AGT_ZIP: partsOrder.job.agt_zip,
        AGT_CTRY: partsOrder.job.agt_ctry,
        AGT_PH1: partsOrder.job.agt_ph1,
        AGT_PH1X: partsOrder.job.agt_ph1x,
        AGT_PH2: partsOrder.job.agt_ph2,
        AGT_PH2X: partsOrder.job.agt_ph2x,
        AGT_FAX: partsOrder.job.agt_fax,
        AGT_FAXX: partsOrder.job.agt_faxx,
        AGT_CT_LN: partsOrder.job.agt_ct_ln,
        AGT_CT_FN: partsOrder.job.agt_ct_fn,
        AGT_CT_PH: partsOrder.job.agt_ct_ph,
        AGT_CT_PHX: partsOrder.job.agt_ct_phx,
        AGT_EA: partsOrder.job.agt_ea,
        AGT_LIC_NO: partsOrder.job.agt_lic_no,
        LOSS_DATE: partsOrder.job.loss_date
          ? new Date(partsOrder.job.loss_date)
          : null,
        LOSS_CAT: null, // Explicitly set to null as in original code
        LOSS_TYPE: null, // Explicitly set to null as in original code
        LOSS_DESC: partsOrder.job.loss_desc,
        THEFT_IND: null, // Explicitly set to null as in original code
        CAT_NO: partsOrder.job.cat_no,
        TLOS_IND: null, // Explicitly set to null as in original code
        LOSS_MEMO: partsOrder.job.loss_memo,
        CUST_PR: partsOrder.job.cust_pr,
        INSD_LN: partsOrder.job.insd_ln,
        INSD_FN: partsOrder.job.insd_fn,
        INSD_TITLE: partsOrder.job.insd_title,
        INSD_CO_NM: partsOrder.job.insd_co_nm,
        INSD_ADDR1: partsOrder.job.insd_addr1,
        INSD_ADDR2: partsOrder.job.insd_addr2,
        INSD_CITY: partsOrder.job.insd_city,
        INSD_ST: partsOrder.job.insd_st,
        INSD_ZIP: partsOrder.job.insd_zip,
        INSD_CTRY: partsOrder.job.insd_ctry,
        INSD_PH1: partsOrder.job.insd_ph1,
        INSD_PH1X: partsOrder.job.insd_ph1x,
        INSD_PH2: partsOrder.job.insd_ph2,
        INSD_PH2X: partsOrder.job.insd_ph2x,
        INSD_FAX: partsOrder.job.insd_fax,
        INSD_FAXX: partsOrder.job.insd_faxx,
        INSD_EA: partsOrder.job.insd_ea,
        OWNR_LN: partsOrder.job.ownr_ln,
        OWNR_FN: partsOrder.job.ownr_fn,
        OWNR_TITLE: partsOrder.job.ownr_title,
        OWNR_CO_NM: partsOrder.job.ownr_co_nm,
        OWNR_ADDR1: partsOrder.job.ownr_addr1,
        OWNR_ADDR2: partsOrder.job.ownr_addr2,
        OWNR_CITY: partsOrder.job.ownr_city,
        OWNR_ST: partsOrder.job.ownr_st,
        OWNR_ZIP: partsOrder.job.ownr_zip,
        OWNR_CTRY: partsOrder.job.ownr_ctry,
        OWNR_PH1: partsOrder.job.ownr_ph1,
        OWNR_PH1X: partsOrder.job.ownr_ph1x,
        OWNR_PH2: partsOrder.job.ownr_ph2,
        OWNR_PH2X: partsOrder.job.ownr_ph2x,
        OWNR_FAX: partsOrder.job.ownr_fax,
        OWNR_FAXX: partsOrder.job.ownr_faxx,
        OWNR_EA: partsOrder.job.ownr_ea,
      },
    ];

    await deleteEmsFileIfExists(
      generateEmsOutFilePath(`${partsOrder.job.ciecaid}.AD1`),
    );

    const dbf: DBFFile = await DBFFile.create(
      generateEmsOutFilePath(`${partsOrder.job.ciecaid}.AD1`),
      ad1FieldLineDescriptors,
    );

    await dbf.appendRecords(records);
    console.log(`${records.length} AD1 file records added.`);
    return true;
  } catch (error) {
    console.error("Error generating AD1 file:", errorTypeCheck(error));
    return false;
  }
};

export default EmsPartsOrderGenerateAd1File;
