const { DBFFile } = require("dbffile");
const path = require("path");
const _ = require("lodash");
const log = require("electron-log");
const { store } = require("../electron-store");
const { BrowserWindow } = require("electron");
const ipcTypes = require("../../src/ipc.types.commonjs");

//Return the jobline. Modification happens in place.
exports.claimsClerk = ({ jobline, joblines }) => {
  const alerts = rules
    .map((rule) => rule({ jobline, joblines })) //If it should be ignored, skip it.
    .filter((rule) => rule !== null);
  return alerts;
};

const rules = [
  //Disabling per discussion with Norm.
  // ({ jobline, joblines }) => {
  //   //Upgrade 1
  //   if (
  //     jobline.db_ref === "900500" &&
  //     jobline.db_price === 0 &&
  //     jobline.db_hrs === 0 &&
  //     jobline.mod_lb_hrs !== jobline.db_hrs
  //   ) {
  //     return {
  //       key: "Manual Line",
  //       alert: `<div>
  //           Manually entered line detected.
  //           <ul>
  //             <li>This part will NOT count towards your RPS.</li>
  //             <li>You will need to supply an invoice to MPI for this part.</li>
  //             <li>Always ensure part is not in CEG before creating a manual entry line.</li>
  //           </ul>
  //         </div>`
  //     };
  //   }

  //   return null;
  // },
  ({ jobline, joblines }) => {
    //Upgrade 2
    if (
      jobline.db_hrs !== 0 &&
      jobline.mod_lb_hrs !== jobline.db_hrs &&
      jobline.lbr_inc === false &&
      jobline.lbr_hrs_j === true

      // jobline.lbr_op !== "OP9" &&
      // jobline.lbr_op !== "OP14"
    ) {
      return {
        key: "Manual labor Time Change",
        alert: `<div>
            Labor time manually changed from original CEG time.
            <ul>
              <li>This could possibly be denied by MPI.</li>
              <li> Ensure labor time is accurate & justified.</li>
              <li>Add an explanation line if needed.</li>
            </ul>
          </div>`
      };
    }

    return null;
  },
  ({ jobline, joblines }) => {
    //Upgrade 3
    if (jobline.db_ref === "900500" && jobline.mod_lb_hrs !== jobline.db_hrs) {
      return {
        key: "Manual Labor Line",
        alert: `<div>
            Manually entered labor line detected.
            <ul>
              <li>Always ensure the labor operation you manually entered was not available in CEG.</li>
              <li>Make sure there are no overlaps with other labor lines to consider.</li>
            </ul>
          </div>`
      };
    }

    return null;
  },
  ({ jobline, joblines }) => {
    //Upgrade 4
    if (
      jobline.db_ref !== "900500" &&
      jobline.part_type &&
      jobline.oem_partno &&
      jobline.price_j //TODO Requires verification per Norm's email.
    ) {
      if (jobline.act_price < jobline.db_price) {
        //TODO: Verify what should happen here when the two values are the same?
        return {
          key: "Modified Part Price",
          alert: `<div>
              Modified part price detected.
              <ul>
                <li>
                  You will need to supply MPI with an invoice for this part showing the retail price you manually
                  entered.
                </li>
                <li>
                  Your manually entered price is <strong>LOWER</strong> than the database price, consider reverting back to database
                  price.
                </li>
                <li>If you chose to leave the manually entered price, you will need to:</li>
                <li>Supply MPI with an invoice for this part showing the retail price you manually entered</li>
                <li>NOTE: You do not need to show MPI your cost on this part, only retail.</li>
              </ul>
            </div>`
        };
      } else {
        return {
          key: "Modified Part Price",
          alert: `<div>
              Modified part price detected.
              <ul>
                <li>
                  You will need to supply MPI with an invoice for this part showing the retail price you manually
                  entered.
                </li>
                <li>NOTE: You do not need to show MPI your cost on this part, only retail.</li>
              </ul>
            </div>`
        };
      }
    }

    return null;
  },
  ({ jobline, joblines }) => {
    // In this update we want to identify OEM part lines where the part # and price have been changed.
    if (
      (jobline.part_type === "PAA" || jobline.part_type === "PAL") &&
      jobline.alt_partno &&
      jobline.act_price < jobline.db_price //TODO: Verify the equals than case.
    ) {
      //Need to find a 900501 line that is right after it indicating it has the words pricematch
      const lineIndex = joblines.findIndex((line) => line.line_no === jobline.line_no);
      const nextLine = joblines[lineIndex + 1];

      if (!nextLine?.line_desc.includes("price")) {
        return {
          key: "Missing pricematch explanation",
          alert: `<div>In that explanation line (900501) we are looking for the words “pricematch” or “price” & “match”.</div>`
        };
      }
    }

    return null;
  },

  ({ jobline, joblines }) => {
    //Upgrade 5
    //TODO: LIN Files did not seem accurate for this. Perhaps one was created and it doesn't work right.
    if (false) {
      return {
        key: "Modified Part # & Price",
        alert: `<div>
            Modified part # and Price detected
            <ul>
              <li>
                Try searching that part # in CEG to find the correct lines rather than modifying a different part line.
              </li>
              <li>If that part # does not exist in CEG, create a manual entry line and erase the one you modified.</li>
              <li>Since this part is not ins CEG, it will not be considered for RPS.</li>
            </ul>
          </div>`
      };
    }

    return null;
  },
  ({ jobline, joblines }) => {
    //Upgrade 6
    if (jobline.part_type && jobline.part_qty !== 1) {
      return {
        key: "Quantity Changed",
        alert: `<div>
            Quantity manual change detected.
            <ul>
              <li>
                MPI Estimating Standard outline that every part should have 1 line rather than manually changing the
                quantity.
              </li>
              <li>Leaving the quantity modified <strong>MAY</strong> affect your RPS score.</li>
              <li>
                Consider creating manual entry lines for each part which will also disqualify those parts from your RPS
                calculation.
              </li>
            </ul>
          </div>`
      };
    }

    return null;
  }
];
