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

const EmsPartsOrderGenerateLinFile = async (
  partsOrder: EmsPartsOrder,
): Promise<boolean> => {
  try {
    const records = partsOrder.parts_order_lines.map((partsOrderLine) => ({
      LINE_NO: partsOrderLine.jobline?.line_no,
      LINE_IND: partsOrderLine.jobline?.line_ind,
      LINE_REF: partsOrderLine.jobline?.line_ref,
      TRAN_CODE: partsOrderLine.jobline?.tran_code ?? "1",
      DB_REF: partsOrderLine.jobline?.db_ref,
      UNQ_SEQ: partsOrderLine.jobline?.unq_seq,
      PART_DES_J: false,
      LINE_DESC: partsOrderLine.jobline?.line_desc,
      PART_TYPE:
        partsOrderLine.priceChange === true
          ? partsOrderLine.part_type
          : partsOrderLine.jobline?.part_type,
      GLASS_FLAG: partsOrderLine.jobline?.glass_flag,
      OEM_PARTNO: partsOrderLine.jobline?.oem_partno,
      PRICE_INC: partsOrderLine.jobline?.price_inc,
      ALT_PART_I: partsOrderLine.jobline?.alt_part_i,
      TAX_PART: partsOrderLine.jobline?.tax_part,
      DB_PRICE: partsOrderLine.jobline?.db_price,
      ACT_PRICE:
        partsOrderLine.priceChange === true
          ? partsOrderLine.act_price
          : partsOrderLine.jobline?.act_price,
      PRICE_J: partsOrderLine.jobline?.price_j,
      CERT_PART: partsOrderLine.jobline?.cert_part,
      PART_QTY: partsOrderLine.jobline?.part_qty,
      ALT_CO_ID: partsOrderLine.jobline?.alt_co_id,
      ALT_PARTNO: partsOrderLine.jobline?.alt_partno,
      ALT_OVERRD: partsOrderLine.jobline?.alt_overrd,
      ALT_PARTM: partsOrderLine.jobline?.alt_partm,
      PRT_DSMK_P: partsOrderLine.jobline?.prt_dsmk_p,
      PRT_DSMK_M: partsOrderLine.jobline?.prt_dsmk_m,
      MOD_LBR_TY: partsOrderLine.jobline?.mod_lbr_ty,
      DB_HRS: partsOrderLine.jobline?.db_hrs,
      MOD_LB_HRS: partsOrderLine.jobline?.mod_lb_hrs,
      LBR_INC: partsOrderLine.jobline?.lbr_inc,
      LBR_OP: partsOrderLine.jobline?.lbr_op,
      LBR_HRS_J: partsOrderLine.jobline?.lbr_hrs_j,
      LBR_TYP_J: partsOrderLine.jobline?.lbr_typ_j,
      LBR_OP_J: partsOrderLine.jobline?.lbr_op_j,
      PAINT_STG: partsOrderLine.jobline?.paint_stg,
      PAINT_TONE: partsOrderLine.jobline?.paint_tone,
      LBR_TAX: partsOrderLine.jobline?.lbr_tax,
      LBR_AMT: partsOrderLine.jobline?.lbr_amt,
      MISC_AMT: partsOrderLine.jobline?.misc_amt,
      MISC_SUBLT: partsOrderLine.jobline?.misc_sublt,
      MISC_TAX: partsOrderLine.jobline?.misc_tax,
      BETT_TYPE: partsOrderLine.jobline?.bett_type,
      BETT_PCTG: partsOrderLine.jobline?.bett_pctg,
      BETT_AMT: partsOrderLine.jobline?.bett_amt,
      BETT_TAX: partsOrderLine.jobline?.bett_tax,
    }));

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

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

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

export default EmsPartsOrderGenerateLinFile;
