import { gql } from "@apollo/client";
import dayjs from "../../utils/day.js";
import componentList from "./componentList.js";

const createDashboardQuery = (items) => {
  const componentBasedAdditions =
    Array.isArray(items) &&
    items
      .map((item) => (componentList[item.i] && componentList[item.i].gqlFragment) || "")
      .filter(Boolean)
      .join("");
  return gql`
    query QUERY_DASHBOARD_DETAILS { ${componentBasedAdditions || ""}
      monthly_sales: jobs(where: {_and: [
        { voided: {_eq: false}},
        {date_invoiced: {_gte: "${dayjs()
          .startOf("month")
          .startOf("day")
          .toISOString()}"}}, {date_invoiced: {_lte: "${dayjs().endOf("month").endOf("day").toISOString()}"}}]}) {
      id
        ro_number
        date_invoiced
        job_totals
        rate_la1
        rate_la2
        rate_la3
        rate_la4
        rate_laa
        rate_lab
        rate_lad
        rate_lae
        rate_laf
        rate_lag
        rate_lam
        rate_lar
        rate_las
        rate_lau
        rate_ma2s
        rate_ma2t
        rate_ma3s
        rate_mabl
        rate_macs
        rate_mahw
        rate_mapa
        rate_mash
        rate_matd
        joblines(where: { removed: { _eq: false } }) {
          id
          mod_lbr_ty
          mod_lb_hrs
          act_price
          part_qty
          part_type
        }
        }
        production_jobs: jobs(where: { inproduction: { _eq: true } }) {
          id
          ro_number
          ins_co_nm
          job_totals
          joblines(where: { removed: { _eq: false } }) {
          id
          mod_lbr_ty
          mod_lb_hrs
          act_price
          part_qty
          part_type
        }
        labhrs: joblines_aggregate(where: { mod_lbr_ty: { _neq: "LAR" }, removed: { _eq: false } }) {
          aggregate {
            sum {
              mod_lb_hrs
            }
          }
        }
        larhrs: joblines_aggregate(where: { mod_lbr_ty: { _eq: "LAR" }, removed: { _eq: false } }) {
          aggregate {
            sum {
              mod_lb_hrs
            }
          }
        }
        }
      }`;
};

export default createDashboardQuery;
