import { DBFFile } from "dbffile";
import { envFieldLineDescriptors } from "../util/ems-interface/fielddescriptors/env-field-descriptor";
import { deleteEmsFileIfExists, generatePpcFilePath } from "../util/ems-util";
import { PpcJob } from "./ppc-handler";

const GenerateEnvFile = async (job: PpcJob): Promise<boolean> => {
  const records = [
    {
      EST_SYSTEM: "C",
      RO_ID: job.ro_number,
      ESTFILE_ID: job.ciecaid,
      STATUS: false,
      INCL_ADMIN: true,
      INCL_VEH: true,
      INCL_EST: true,
      INCL_PROFL: true,
      INCL_TOTAL: true,
      INCL_VENDR: false,
    },
  ];

  await deleteEmsFileIfExists(generatePpcFilePath(`${job.ciecaid}.ENV`));

  const dbf = await DBFFile.create(
    generatePpcFilePath(`${job.ciecaid}.ENV`),
    envFieldLineDescriptors,
  );

  await dbf.appendRecords(records);
  console.log(`${records.length} LIN file records added.`);
  return true;
};

export default GenerateEnvFile;
