import { platform } from "@electron-toolkit/utils";
import { DBFFile } from "dbffile";
import log from "electron-log/main";
import _ from "lodash";
import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import errorTypeCheck from "../../util/errorTypeCheck";
import { DecodedPft, DecodedPftLine } from "./decode-pft.interface";
import { findFileCaseInsensitive } from "./decoder-utils";

const DecodePft = async (
  extensionlessFilePath: string,
): Promise<DecodedPft> => {
  let dbf: DBFFile | null = null;
  if (platform.isWindows) {
    try {
      dbf = await DBFFile.open(`${extensionlessFilePath}.PFT`);
    } catch (error) {
      log.error("Error opening PFH File.", errorTypeCheck(error));
    }

    if (!dbf) {
      log.error(`Could not find any PFT files at ${extensionlessFilePath}`);
      throw new Error(
        `Could not find any PFT files at ${extensionlessFilePath}`,
      );
    }
  } else {
    const possibleExtensions: string[] = ["pft"];
    const filePath = await findFileCaseInsensitive(
      extensionlessFilePath,
      possibleExtensions,
    );
    try {
      if (!filePath) {
        log.error(`Could not find any PFT files at ${extensionlessFilePath}`);
        throw new Error(
          `Could not find any PFT files at ${extensionlessFilePath}`,
        );
      }
      dbf = await DBFFile.open(filePath);
    } catch (error) {
      log.error("Error opening PFT File.", errorTypeCheck(error));
      throw error;
    }
  }

  const rawDBFRecord = await dbf.readRecords(1);

  //PFT will always have only 1 row.
  //Commented lines have been cross referenced with existing partner fields.

  const rawPftData: DecodedPftLine = deepLowerCaseKeys(
    _.pick(rawDBFRecord[0], [
      //TODO: Add typings for EMS File Formats.
      "TAX_TYPE1", //The below is is taken from a CCC estimate. Will require validation to ensure it is also accurate for Audatex/Mitchell
      "TY1_TIER1",
      "TY1_THRES1",
      "TY1_RATE1",
      "TY1_SUR1",
      "TY1_TIER2",
      "TY1_THRES2",
      "TY1_RATE2",
      "TY1_SUR2",
      "TY1_TIER3",
      "TY1_THRES3",
      "TY1_RATE3",
      "TY1_SUR3",
      "TY1_TIER4",
      "TY1_THRES4",
      "TY1_RATE4",
      "TY1_SUR4",
      "TY1_TIER5",
      "TY1_THRES5",
      "TY1_RATE5",
      "TY1_SUR5",
      "TAX_TYPE2",
      "TY2_TIER1",
      "TY2_THRES1",
      "TY2_RATE1",
      "TY2_SUR1",
      "TY2_TIER2",
      "TY2_THRES2",
      "TY2_RATE2",
      "TY2_SUR2",
      "TY2_TIER3",
      "TY2_THRES3",
      "TY2_RATE3",
      "TY2_SUR3",
      "TY2_TIER4",
      "TY2_THRES4",
      "TY2_RATE4",
      "TY2_SUR4",
      "TY2_TIER5",
      "TY2_THRES5",
      "TY2_RATE5",
      "TY2_SUR5",
      "TAX_TYPE3",
      "TY3_TIER1",
      "TY3_THRES1",
      "TY3_RATE1",
      "TY3_SUR1",
      "TY3_TIER2",
      "TY3_THRES2",
      "TY3_RATE2",
      "TY3_SUR2",
      "TY3_TIER3",
      "TY3_THRES3",
      "TY3_RATE3",
      "TY3_SUR3",
      "TY3_TIER4",
      "TY3_THRES4",
      "TY3_RATE4",
      "TY3_SUR4",
      "TY3_TIER5",
      "TY3_THRES5",
      "TY3_RATE5",
      "TY3_SUR5",
      "TAX_TYPE4",
      "TY4_TIER1",
      "TY4_THRES1",
      "TY4_RATE1",
      "TY4_SUR1",
      "TY4_TIER2",
      "TY4_THRES2",
      "TY4_RATE2",
      "TY4_SUR2",
      "TY4_TIER3",
      "TY4_THRES3",
      "TY4_RATE3",
      "TY4_SUR3",
      "TY4_TIER4",
      "TY4_THRES4",
      "TY4_RATE4",
      "TY4_SUR4",
      "TY4_TIER5",
      "TY4_THRES5",
      "TY4_RATE5",
      "TY4_SUR5",
      "TAX_TYPE5",
      "TY5_TIER1",
      "TY5_THRES1",
      "TY5_RATE1",
      "TY5_SUR1",
      "TY5_TIER2",
      "TY5_THRES2",
      "TY5_RATE2",
      "TY5_SUR2",
      "TY5_TIER3",
      "TY5_THRES3",
      "TY5_RATE3",
      "TY5_SUR3",
      "TY5_TIER4",
      "TY5_THRES4",
      "TY5_RATE4",
      "TY5_SUR4",
      "TY5_TIER5",
      "TY5_THRES5",
      "TY5_RATE5",
      "TY5_SUR5",
      "TAX_TYPE6",
      "TY6_TIER1",
      "TY6_THRES1",
      "TY6_RATE1",
      "TY6_SUR1",
      "TY6_TIER2",
      "TY6_THRES2",
      "TY6_RATE2",
      "TY6_SUR2",
      "TY6_TIER3",
      "TY6_THRES3",
      "TY6_RATE3",
      "TY6_SUR3",
      "TY6_TIER4",
      "TY6_THRES4",
      "TY6_RATE4",
      "TY6_SUR4",
      "TY6_TIER5",
      "TY6_THRES5",
      "TY6_RATE5",
      "TY6_SUR5",
    ]),
  );

  //Apply business logic transfomrations.
  //We don't have an inspection date, we instead have `date_estimated`

  return { cieca_pft: rawPftData };
};
export default DecodePft;
