import gql from "graphql-tag";

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

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
      close_date
      updated_at
      requires_reimport
    }
    jobs_aggregate {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;

export const SEARCH_JOBS_PAGINATED = gql`
  query SEARCH_JOBS_PAGINATED(
    $offset: Int
    $limit: Int
    $search: String
    $startDate: date
    $endDate: date
    $closeDateIsNull: Boolean
  ) {
    search_jobs(
      offset: $offset
      limit: $limit
      args: { enddate: $endDate, search: $search, startdate: $startDate }
      where: { close_date: { _is_null: $closeDateIsNull } }
    ) {
      ownr_fn
      ownr_ln
      v_vin
      v_model_yr
      v_model
      v_makedesc
      id
      ins_co_nm
      clm_no
      updated_at
      close_date
      requires_reimport
    }
    search_jobs_aggregate(
      args: { enddate: $endDate, search: $search, startdate: $startDate }
      where: { close_date: { _is_null: $closeDateIsNull } }
    ) {
      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
      group_verified
      v_age
      v_type
      loss_date
      close_date
      updated_at
      requires_reimport
      v_mileage
      joblines(order_by: { line_no: asc }) {
        id
        line_no
        act_price
        db_price
        line_desc
        line_ind
        oem_partno
        part_qty
        part_type
        unq_seq
        price_diff
        price_diff_pc
        ignore
        db_ref
      }
    }
  }
`;

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
      close_date
      requires_reimport
      joblines {
        id
        act_price
        db_price
        line_desc
        line_ind
        oem_partno
        part_qty
        part_type
        unq_seq
      }
    }
  }
`;

export const QUERY_CLOSE_DATE_BY_CLM_NO = gql`
  query QUERY_CLOSE_DATE_BY_CLM_NO($clm_no: String!) {
    jobs(where: { clm_no: { _eq: $clm_no } }) {
      id
      close_date
    }
  }
`;

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
        close_date
        v_age
        group
        group_verified
        requires_reimport
        v_mileage
        joblines(order_by: { line_no: asc }) {
          id
          act_price
          db_price
          line_desc
          line_ind
          oem_partno
          part_qty
          part_type
          unq_seq
        }
      }
    }
  }
`;

export const DELETE_JOB = gql`
  mutation DELETE_JOB($jobId: uuid!) {
    delete_jobs_by_pk(id: $jobId) {
      id
    }
  }
`;
