import { DBFFile } from "dbffile";
import _ from "lodash";
import errorTypeCheck from "../../util/errorTypeCheck";
import { DecodedPfmLine } from "../decoder/decode-pfm.interface";
import { pfmFieldLineDescriptors } from "../util/ems-interface/fielddescriptors/pfm-field-descriptors";
import {
  deleteEmsFileIfExists,
  generateEmsOutFilePath,
} from "../util/ems-util";
import uppercaseObjectKeys from "../util/uppercaseObjectKeys";
import { EmsPartsOrder } from "./ems-parts-order-interfaces";

const EmsPartsOrderGeneratePfmFile = async (
  partsOrder: EmsPartsOrder,
): Promise<boolean> => {
  try {
    let records;
    if (partsOrder.job.materials && !_.isEmpty(partsOrder.job.materials)) {
      records = Object.keys(partsOrder.job.materials).map((key) => {
        const record: DecodedPfmLine = partsOrder.job.materials[key];
        return uppercaseObjectKeys(record);
      });
    } else {
      //Older records may not have materials, especially for ImEX.
      records = [
        {
          MATL_TYPE: "MAPA",
          CAL_CODE: null,
          CAL_DESC: null,
          CAL_MAXDLR: 0,
          CAL_PRIP: 0,
          CAL_SECP: 0,
          MAT_CALP: 0,
          CAL_PRETHR: 0,
          CAL_PSTTHR: 0,
          CAL_THRAMT: 0,
          CAL_LBRMIN: 0,
          CAL_LBRMAX: 0,
          CAL_LBRRTE: partsOrder.job.rate_mapa,
          CAL_OPCODE: null,
          TAX_IND: true,
          MAT_TAXP: null,
          MAT_ADJP: null,
          MAT_TX_TY1: null,
          MAT_TX_IN1: null,
          MAT_TX_TY2: null,
          MAT_TX_IN2: null,
          MAT_TX_TY3: null,
          MAT_TX_IN3: null,
          MAT_TX_TY4: null,
          MAT_TX_IN4: null,
          MAT_TX_TY5: null,
          MAT_TX_IN5: null,
        },
        {
          MATL_TYPE: "MASH",
          CAL_CODE: null,
          CAL_DESC: null,
          CAL_MAXDLR: 0,
          CAL_PRIP: 0,
          CAL_SECP: 0,
          MAT_CALP: 0,
          CAL_PRETHR: 0,
          CAL_PSTTHR: 0,
          CAL_THRAMT: 0,
          CAL_LBRMIN: 0,
          CAL_LBRMAX: 0,
          CAL_LBRRTE: partsOrder.job.rate_mash,
          CAL_OPCODE: null,
          TAX_IND: true,
          MAT_TAXP: null,
          MAT_ADJP: null,
          MAT_TX_TY1: null,
          MAT_TX_IN1: null,
          MAT_TX_TY2: null,
          MAT_TX_IN2: null,
          MAT_TX_TY3: null,
          MAT_TX_IN3: null,
          MAT_TX_TY4: null,
          MAT_TX_IN4: null,
          MAT_TX_TY5: null,
          MAT_TX_IN5: null,
        },
      ];
    }

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

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

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

export default EmsPartsOrderGeneratePfmFile;
