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 } }, 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
    }
    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
      date
      id
      jobid
      employeeid
      memo
      flat_rate
      clockon
      clockoff
      rate
      employee {
        id
        first_name
        last_name
        employee_number
      }
      productivehrs
    }
  }
`;

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
        part_qty
        db_price
        act_price
        line_desc
        line_no
        oem_partno
        notes
        location
        status
        removed
        convertedtolbr
        convertedtolbr_data
      }
    }
  }
`;

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
    }
    jobs_by_pk(id: $id) {
      id
      status
      ious {
        id
        ro_number
      }
    }
  }
`;
// 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
      }
    }
  }
`;
