import { gql } from "@apollo/client";

export const GET_ALL_JOBLINES_BY_PK = gql`
  query GET_ALL_JOBLINES_BY_PK($id: uuid!) {
    joblines(where: { jobid: { _eq: $id }, removed: { _eq: false } }, order_by: { line_no: asc }) {
      id
      line_no
      unq_seq
      line_ind
      line_desc
      part_type
      oem_partno
      db_price
      act_price
      part_qty
      mod_lbr_ty
      db_hrs
      mod_lb_hrs
      lbr_op
      lbr_amt
      op_code_desc
      status
      notes
      location
      tax_part
      manual_line
    }
  }
`;

export const GET_LINE_TICKET_BY_PK = gql`
  query GET_LINE_TICKET_BY_PK($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      lbr_adjustments
      converted
      status
      employee_body
      employee_body_rel {
        id
        first_name
        last_name
      }
      employee_csr
      employee_csr_rel {
        id
        first_name
        last_name
      }
      employee_prep
      employee_prep_rel {
        id
        first_name
        last_name
      }
      employee_refinish
      employee_refinish_rel {
        id
        first_name
        last_name
      }
    }
    joblines(where: { jobid: { _eq: $id }, removed: { _eq: false } }) {
      id
      line_desc
      part_type
      oem_partno
      db_price
      act_price
      part_qty
      mod_lbr_ty
      db_hrs
      mod_lb_hrs
      lbr_op
      lbr_amt
      op_code_desc
      convertedtolbr
      convertedtolbr_data
    }
    timetickets(where: { jobid: { _eq: $id } }) {
      actualhrs
      ciecacode
      cost_center
      created_by
      date
      id
      jobid
      employeeid
      memo
      flat_rate
      clockon
      clockoff
      rate
      committed_at
      commited_by
      task_name
      employee {
        id
        first_name
        last_name
        employee_number
      }
      productivehrs
    }
  }
`;

export const GET_JOB_INFO_DRAW_CALCULATIONS = gql`
  query GET_JOB_INFO_DRAW_CALCULATIONS($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      lbr_adjustments
      converted
      rate_lab
      rate_lad
      rate_laa
      rate_la1
      rate_la2
      rate_la3
      rate_la4
      rate_lau
      rate_lar
      rate_lag
      rate_laf
      rate_lam
    }
    timetickets(where: { jobid: { _eq: $id } }) {
      actualhrs
      ciecacode
      cost_center
      date
      id
      jobid
      employeeid
      memo
      flat_rate
      clockon
      clockoff
      rate
      employee {
        id
        first_name
        last_name
        employee_number
      }
      productivehrs
    }
    joblines(where: { jobid: { _eq: $id }, removed: { _eq: false } }) {
      id
      line_desc
      part_type
      oem_partno
      db_price
      act_price
      part_qty
      mod_lbr_ty
      db_hrs
      mod_lb_hrs
      lbr_op
      lbr_amt
      op_code_desc
      convertedtolbr
      convertedtolbr_data
    }
  }
`;
export const UPDATE_JOB_LINE_STATUS = gql`
  mutation UPDATE_JOB_LINE_STATUS($ids: [uuid!]!, $status: String!, $location: String) {
    update_joblines(where: { id: { _in: $ids } }, _set: { status: $status, location: $location }) {
      affected_rows
      returning {
        id
        status
        location
      }
    }
  }
`;

export const INSERT_NEW_JOB_LINE = gql`
  mutation INSERT_NEW_JOB_LINE($lineInput: [joblines_insert_input!]!) {
    insert_joblines(objects: $lineInput) {
      returning {
        id
      }
    }
  }
`;

export const RECEIVE_PARTS_LINE = gql`
  mutation RECEIVE_PARTS_LINE(
    $lineId: uuid!
    $line: joblines_set_input!
    $orderLineId: uuid!
    $orderLine: parts_order_lines_set_input!
  ) {
    update_joblines(where: { id: { _eq: $lineId } }, _set: $line) {
      returning {
        id
        notes
        mod_lbr_ty
        part_qty
        db_price
        act_price
        line_desc
        oem_partno
        notes
        location
        status
        removed
      }
    }
    update_parts_order_lines_by_pk(pk_columns: { id: $orderLineId }, _set: $orderLine) {
      id
      line_desc
      backordered_on
      backordered_eta
      status
    }
  }
`;

export const UPDATE_JOB_LINE_SUBLET = gql`
  mutation UPDATE_JOB_LINE_SUBLET($lineId: uuid!, $line: joblines_set_input!, $now: timestamptz!, $jobId: uuid!) {
    update_jobs_by_pk(pk_columns: { id: $jobId }, _set: { updated_at: $now }) {
      id
      updated_at
    }
    update_joblines(where: { id: { _eq: $lineId } }, _set: $line) {
      returning {
        id
        sublet_completed
        sublet_ignored
      }
    }
  }
`;

export const UPDATE_JOB_LINE = gql`
  mutation UPDATE_JOB_LINE($lineId: uuid!, $line: joblines_set_input!) {
    update_joblines(where: { id: { _eq: $lineId } }, _set: $line) {
      returning {
        id
        notes
        mod_lbr_ty
        mod_lb_hrs
        part_type
        op_code_desc
        prt_dsmk_m
        prt_dsmk_p
        tax_part
        part_qty
        db_price
        act_price
        line_desc
        line_no
        oem_partno
        notes
        location
        status
        removed
        convertedtolbr
        convertedtolbr_data
        assigned_team
        include_in_part_cnt
      }
    }
  }
`;

export const GET_JOB_LINES_TO_ENTER_BILL = gql`
  query GET_JOB_LINES_TO_ENTER_BILL($id: uuid!) {
    joblines(where: { jobid: { _eq: $id } }, order_by: { act_price: desc_nulls_last }) {
      removed
      id
      line_desc
      part_type
      oem_partno
      alt_partno
      db_price
      act_price
      part_qty
      mod_lbr_ty
      db_hrs
      mod_lb_hrs
      lbr_op
      lbr_amt
      op_code_desc
      alt_partno
      assigned_team
    }
    jobs_by_pk(id: $id) {
      id
      status
      ious {
        id
        ro_number
      }
      v_make_desc
    }
  }
`;
// oem_partno: {
//   _neq: "";
// }
// act_price: {
//   _gt: "0";
// }

export const generateJobLinesUpdatesForInvoicing = (joblines) => {
  const updates = joblines.reduce((acc, jl, idx) => {
    return (
      acc +
      `a${idx}:update_joblines(where: {id: {_eq: "${jl.id}"}}, _set: {profitcenter_labor: "${
        jl.profitcenter_labor || ""
      }", profitcenter_part: "${jl.profitcenter_part || ""}"}) {
   returning {
     line_desc
     profitcenter_part
     profitcenter_labor
     id
   }
 }`
    );
  }, "");

  return gql`
        mutation UPDATE_JOBLINES_FOR_INVOICING{
            ${updates}
        }
    `;
};

export const DELETE_JOB_LINE_BY_PK = gql`
  mutation DELETE_JOB_LINE_BY_PK($joblineId: uuid!) {
    update_joblines_by_pk(pk_columns: { id: $joblineId }, _set: { removed: true }) {
      removed
      id
    }
  }
`;

export const UPDATE_JOB_LINES_IOU = gql`
  mutation UPDATE_JOB_LINES_IOU($ids: [uuid!]!) {
    update_joblines(where: { id: { _in: $ids } }, _set: { ioucreated: true }) {
      returning {
        ioucreated
        id
      }
    }
  }
`;

export const UPDATE_LINE_PPC = gql`
  mutation UPDATE_LINE_PPC($id: uuid!, $jobline: joblines_set_input) {
    update_joblines_by_pk(pk_columns: { id: $id }, _set: $jobline) {
      jobid
      id
      act_price_before_ppc
      act_price
    }
  }
`;

export const UPDATE_LINE_BULK_ASSIGN = gql`
  mutation UPDATE_LINE_BULK_ASSIGN($ids: [uuid!]!, $jobline: joblines_set_input) {
    update_joblines_many(updates: { _set: $jobline, where: { id: { _in: $ids } } }) {
      returning {
        id
        assigned_team
      }
    }
  }
`;
