import gql from "graphql-tag";

export const INSERT_NEW_JOB = gql`
  mutation INSERT_JOB($job: [jobs_insert_input!]!) {
    insert_jobs(objects: $job) {
      returning {
        id
      }
    }
  }
`;

// on_conflict: {
//   constraint: jobs_clm_no_bodyshopid_key
//   update_columns: [
//     ins_co_nm
//     clm_no
//     clm_total
//     ownr_ln
//     ownr_fn
//     v_vin
//     v_make_desc
//     v_model_desc
//     v_type
//   ]
// }

export const QUERY_ALL_JOBS_PAGINATED = gql`
  query QUERY_ALL_JOBS_PAGINATED(
    $offset: Int
    $limit: Int
    $order: [jobs_order_by!]
  ) {
    jobs(offset: $offset, limit: $limit, order_by: $order) {
      ownr_fn
      ownr_ln
      v_vin
      v_model_yr
      v_model
      v_makedesc
      id
      ins_co_nm
      clm_no
      clm_total
      ro_number
      updated_at
    }
    jobs_aggregate {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;

export const QUERY_JOB_BY_PK = gql`
  query QUERY_JOB_BY_PK($jobId: uuid!) {
    jobs_by_pk(id: $jobId) {
      ownr_fn
      ownr_ln
      v_vin
      v_model_yr
      v_model
      v_makedesc
      id
      ins_co_nm
      clm_no
      clm_total
      ro_number
      updated_at
      group
      v_age
      joblines(order_by: { unq_seq: asc }) {
        id
        act_price
        db_price
        line_desc
        line_ind
        oem_partno
        part_qty
        part_type
        unq_seq
      }
    }
  }
`;

export const QUERY_JOB_BY_CLM_NO = gql`
  query QUERY_JOB_BY_CLM_NO($clm_no: String!) {
    jobs(where: { clm_no: { _eq: $clm_no } }) {
      id
      joblines {
        id
        act_price
        db_price
        line_desc
        line_ind
        oem_partno
        part_qty
        part_type
        unq_seq
      }
    }
  }
`;

export const UPDATE_JOB = gql`
  mutation UPDATE_JOB($jobId: uuid!, $job: jobs_set_input!) {
    update_jobs(where: { id: { _eq: $jobId } }, _set: $job) {
      returning {
        ownr_fn
        ownr_ln
        v_vin
        v_model_yr
        v_model
        v_makedesc
        id
        ins_co_nm
        clm_no
        clm_total
        ro_number
        updated_at
        joblines(order_by: { unq_seq: asc }) {
          id
          act_price
          db_price
          line_desc
          line_ind
          oem_partno
          part_qty
          part_type
          unq_seq
        }
      }
    }
  }
`;
