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

const EmsPartsOrderGeneratePftFile = async (
  partsOrder: EmsPartsOrder,
): Promise<boolean> => {
  try {
    const records = []; //Left blank intentionally as per previous code.

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

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

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

export default EmsPartsOrderGeneratePftFile;
