import { gql } from "apollo-boost";

export const QUERY_ALL_ACTIVE_JOBS = gql`
  query QUERY_ALL_ACTIVE_JOBS($statuses: [String!]!) {
    jobs(where: { status: { _in: $statuses } }) {
      ownr_fn
      ownr_ln
      ownr_ph1
      ownr_ea
      owner {
        id
        allow_text_message
        preferred_contact
      }
      vehicle {
        id
        v_model_yr
        v_make_desc
        v_model_desc
        v_color
        plate_no
      }
      actual_completion
      actual_delivery
      actual_in
      est_number
      id
      ins_co_nm
      ins_ct_fn
      ins_ct_ln
      ins_ph1
      ins_ea
      est_co_nm
      est_ph1
      est_ea
      est_ct_fn
      est_ct_ln
      clm_no
      clm_total
      owner_owing
      ro_number
      scheduled_completion
      scheduled_in
      scheduled_delivery
      status
      updated_at
      ded_amt
      vehicle {
        id
        plate_no
        v_vin
        v_model_yr
        v_model_desc
        v_make_desc
        v_color
      }
    }
  }
`;

export const SUBSCRIPTION_JOBS_IN_PRODUCTION = gql`
  subscription SUBSCRIPTION_JOBS_IN_PRODUCTION {
    job_status(
      where: { isproductionstatus: { _eq: true } }
      order_by: { order: asc }
    ) {
      name
      order
      isproductionstatus
      id
      jobs {
        id
        scheduled_completion
        actual_in
        est_number
        ro_number
        vehicle {
          id
          v_model_yr
          v_model_desc
          v_make_desc
          v_vin
        }
        owner {
          id
          first_name
          last_name
        }
      }
    }
  }
`;

export const GET_JOB_BY_PK = gql`
  query GET_JOB_BY_PK($id: uuid!) {
    jobs_by_pk(id: $id) {
      updated_at
      service_car
      csr
      loss_desc
      kmin
      kmout
      referral_source
      unit_number
      po_number
      special_coverage_policy
      scheduled_delivery
      converted
      est_number
      ro_number
      clm_total
      vehicle {
        id
        plate_no
        v_vin
        v_model_yr
        v_model_desc
        v_make_desc
        v_color
      }
      ins_co_id
      policy_no
      loss_date
      clm_no
      area_of_damage
      ins_co_nm
      ins_addr1
      ins_city
      ins_ct_ln
      ins_ct_fn
      ins_ea
      ins_ph1
      est_co_nm
      est_ct_fn
      est_ct_ln
      pay_date
      est_ph1
      est_ea
      selling_dealer
      servicing_dealer
      selling_dealer_contact
      servicing_dealer_contact
      regie_number
      scheduled_completion
      id
      ded_amt
      ded_status
      depreciation_taxes
      federal_tax_payable
      other_amount_payable
      towing_payable
      storage_payable
      adjustment_bottom_line
      ownr_fn
      ownr_ln
      owner {
        id
        ownr_fn
        ownr_ln
      }
      labor_rate_desc
      rate_lab
      rate_lad
      rate_lae
      rate_lar
      rate_las
      rate_laf
      rate_lam
      rate_lag
      rate_atp
      rate_lau
      rate_la1
      rate_la2
      rate_la3
      rate_la4
      rate_mapa
      rate_mash
      rate_mahw
      rate_ma2s
      rate_ma3s
      rate_mabl
      rate_macs
      rate_laa
      rate_matd
      actual_in
      scheduled_completion
      scheduled_in
      actual_completion
      scheduled_delivery
      actual_delivery
      date_estimated
      date_open
      date_scheduled
      date_invoiced
      date_closed
      date_exported
      status
      joblines {
        id
        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
      }
      appointments_aggregate {
        aggregate {
          count
        }
      }
    }
  }
`;

export const QUERY_JOB_CARD_DETAILS = gql`
  query QUERY_JOB_CARD_DETAILS($id: uuid!) {
    jobs_by_pk(id: $id) {
      ownr_fn
      ownr_ln
      ownr_ph1
      ownr_ea
      owner {
        id
        allow_text_message
        preferred_contact
      }
      vehicle {
        id
        v_model_yr
        v_make_desc
        v_model_desc
        v_color
        plate_no
      }
      actual_completion
      actual_delivery
      actual_in
      est_number
      id
      ins_co_nm
      ins_ct_fn
      ins_ct_ln
      ins_ph1
      ins_ea
      est_co_nm
      est_ph1
      est_ea
      est_ct_fn
      est_ct_ln
      clm_no
      status
      area_of_damage
      ro_number
      scheduled_completion
      scheduled_in
      scheduled_delivery
      notes {
        id
        text
        critical
        private
        created_at
      }

      updated_at
      clm_total
      ded_amt
      documents(limit: 3, order_by: { created_at: desc }) {
        id
        thumb_url
      }
      vehicle {
        id
        plate_no
        v_vin
        v_model_yr
        v_model_desc
        v_make_desc
        v_color
      }
    }
  }
`;

export const UPDATE_JOB = gql`
  mutation UPDATE_JOB($jobId: uuid!, $job: jobs_set_input!) {
    update_jobs(where: { id: { _eq: $jobId } }, _set: $job) {
      returning {
        id
        est_ph1
        est_ea
      }
    }
  }
`;

export const CONVERT_JOB_TO_RO = gql`
  mutation CONVERT_JOB_TO_RO($jobId: uuid!) {
    update_jobs(where: { id: { _eq: $jobId } }, _set: { converted: true }) {
      returning {
        id
        ro_number
        converted
      }
    }
  }
`;

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

export const UPDATE_JOB_STATUS = gql`
  mutation UPDATE_JOB_STATUS($jobId: uuid!, $status: String!) {
    update_jobs(where: { id: { _eq: $jobId } }, _set: { status: $status }) {
      returning {
        id
      }
    }
  }
`;

export const ACTIVE_JOBS_FOR_AUTOCOMPLETE = gql`
  query ACTIVE_JOBS_FOR_AUTOCOMPLETE($statuses: [String!]!) {
    jobs(where: { status: { _in: $statuses } }) {
      id
      ownr_fn
      ownr_ln
      ro_number
      est_number
      vehicle {
        id
        v_make_desc
        v_model_desc
        v_model_yr
      }
    }
  }
`;
