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

export const QUERY_ALL_ACTIVE_JOBS_PAGINATED = gql`
  query QUERY_ALL_ACTIVE_JOBS_PAGINATED(
    $offset: Int
    $limit: Int
    $order: [jobs_order_by!]
    $statuses: [String!]!
    $isConverted: Boolean
  ) {
    jobs(
      offset: $offset
      limit: $limit
      where: { status: { _in: $statuses }, converted: { _eq: $isConverted } }
      order_by: $order
    ) {
      iouparent
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_ph1
      ownr_ph1_ty
      ownr_ph2
      ownr_ph2_ty
      ownr_ea
      ownerid
      comment
      plate_no
      plate_st
      v_vin
      v_model_yr
      v_model_desc
      v_make_desc
      v_color
      vehicleid
      actual_completion
      actual_delivery
      actual_in
      production_vars
      id
      ins_co_nm
      clm_no
      po_number
      clm_total
      owner_owing
      ro_number
      scheduled_completion
      scheduled_in
      scheduled_delivery
      status
      updated_at
      ded_amt
      suspended
      est_ct_fn
      est_ct_ln
    }
    jobs_aggregate(where: { status: { _in: $statuses } }) {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;

export const QUERY_ALL_ACTIVE_JOBS = gql`
  query QUERY_ALL_ACTIVE_JOBS($statuses: [String!]!, $isConverted: Boolean) {
    jobs(where: { status: { _in: $statuses }, converted: { _eq: $isConverted } }, order_by: { created_at: desc }) {
      iouparent
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      ownr_ea
      ownerid
      comment
      plate_no
      plate_st
      v_vin
      v_model_yr
      v_model_desc
      v_make_desc
      v_color
      vehicleid
      actual_completion
      actual_delivery
      actual_in
      production_vars
      id
      ins_co_nm
      clm_no
      po_number
      clm_total
      owner_owing
      ro_number
      scheduled_completion
      scheduled_in
      scheduled_delivery
      status
      updated_at
      ded_amt
      suspended
      est_ct_fn
      est_ct_ln
    }
  }
`;

export const QUERY_PARTS_QUEUE = gql`
  query QUERY_PARTS_QUEUE($statuses: [String!]!, $offset: Int, $limit: Int) {
    jobs_aggregate(where: { _and: [{ status: { _in: $statuses } }] }) {
      aggregate {
        count(distinct: true)
      }
    }
    jobs(
      where: { _and: [{ status: { _in: $statuses }, converted: { _eq: true } }] }
      offset: $offset
      limit: $limit
      order_by: { ro_number: desc }
    ) {
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      ownr_ea
      plate_no
      plate_st
      v_vin
      v_model_yr
      v_model_desc
      v_make_desc
      v_color
      vehicleid
      scheduled_in
      scheduled_completion
      id
      ins_co_nm
      clm_no
      ro_number
      status
      updated_at
      vehicleid
      ownerid
      queued_for_parts
      comment
      joblines_status {
        count
        part_type
        status
      }
    }
  }
`;

export const QUERY_EXACT_JOB_IN_PRODUCTION = gql`
  query QUERY_EXACT_JOB_IN_PRODUCTION($id: uuid!) {
    jobs(where: { id: { _eq: $id } }) {
      id
      iouparent
      status
      ro_number
      comment
      ownr_fn
      ownr_ln
      category
      ownr_co_nm
      v_model_yr
      v_model_desc
      clm_no
      v_make_desc
      v_color
      plate_no
      actual_in
      scheduled_completion
      scheduled_delivery
      date_last_contacted
      date_next_contact
      ins_co_nm
      clm_total
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      special_coverage_policy
      owner_owing
      production_vars
      kanbanparent
      alt_transport
      employee_body
      employee_refinish
      employee_prep
      employee_csr
      date_repairstarted
      dms_id
      joblines_status {
        part_type
        status
        count
      }
      labhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _neq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      larhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _eq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      subletLines: joblines(
        where: { _and: { part_type: { _in: ["PAS", "PASL"] }, removed: { _eq: false } } }
        order_by: { line_no: asc }
      ) {
        id
        line_desc
        sublet_ignored
        sublet_completed
        jobid
      }
    }
  }
`;

export const QUERY_EXACT_JOBS_IN_PRODUCTION = gql`
  query QUERY_EXACT_JOBS_IN_PRODUCTION($ids: [uuid!]!) {
    jobs(where: { id: { _in: $ids } }) {
      id
      iouparent
      status
      ro_number
      comment
      ownr_fn
      category
      ownr_ln
      ownr_co_nm
      v_model_yr
      v_model_desc
      clm_no
      v_make_desc
      v_color
      plate_no
      actual_in
      scheduled_completion
      scheduled_delivery
      date_last_contacted
      date_next_contact
      ins_co_nm
      clm_total
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      special_coverage_policy
      owner_owing
      production_vars
      kanbanparent
      alt_transport
      employee_body
      employee_refinish
      employee_prep
      employee_csr
      date_repairstarted
      dms_id
      joblines_status {
        part_type
        status
        count
      }
      labhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _neq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      larhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _eq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      subletLines: joblines(
        where: { _and: { part_type: { _in: ["PAS", "PASL"] }, removed: { _eq: false } } }
        order_by: { line_no: asc }
      ) {
        id
        line_desc
        sublet_ignored
        sublet_completed
        jobid
      }
    }
  }
`;

export const QUERY_LBR_HRS_BY_PK = gql`
  query QUERY_LBR_HRS_BY_PK($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      ro_number
      labhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _neq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      larhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _eq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
    }
  }
`;

export const QUERY_JOB_COSTING_DETAILS = gql`
  query QUERY_JOB_COSTING_DETAILS($id: uuid!) {
    jobs_by_pk(id: $id) {
      ro_number
      clm_total
      id
      ded_amt
      ded_status
      depreciation_taxes
      other_amount_payable
      towing_payable
      storage_payable
      adjustment_bottom_line
      federal_tax_rate
      state_tax_rate
      local_tax_rate
      tax_tow_rt
      tax_str_rt
      tax_paint_mat_rt
      tax_sub_rt
      tax_lbr_rt
      tax_levies_rt
      parts_tax_rates
      job_totals
      labor_rate_desc
      rate_la1
      rate_la2
      rate_la3
      rate_la4
      rate_laa
      rate_lab
      rate_lad
      rate_lae
      rate_laf
      rate_lag
      rate_lam
      rate_lar
      rate_las
      rate_lau
      rate_ma2s
      rate_ma2t
      rate_ma3s
      rate_mabl
      rate_macs
      rate_mahw
      rate_mapa
      rate_mash
      rate_matd
      actual_in
      status
      ca_bc_pvrt
      ca_customer_gst
      joblines(where: { removed: { _eq: false } }) {
        id
        unq_seq
        line_ind
        tax_part
        line_desc
        prt_dsmk_p
        prt_dsmk_m
        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
      }
      bills {
        id
        federal_tax_rate
        local_tax_rate
        state_tax_rate
        is_credit_memo
        billlines {
          actual_cost
          cost_center
          id
          quantity
        }
      }
      timetickets {
        id
        rate
        cost_center
        actualhrs
        productivehrs
        flat_rate
      }
    }
  }
`;

export const GET_JOB_BY_PK = gql`
  query GET_JOB_BY_PK($id: uuid!) {
    jobs_by_pk(id: $id) {
      tasks_aggregate(where: { completed: { _eq: false }, deleted: { _eq: false } }) {
        aggregate {
          count
        }
      }
      actual_completion
      actual_delivery
      actual_in
      acv_amount
      admin_clerk
      adjustment_bottom_line
      alt_transport
      area_of_damage
      auto_add_ats
      available_jobs {
        id
      }
      ca_bc_pvrt
      ca_customer_gst
      ca_gst_registrant
      category
      cccontracts {
        agreementnumber
        courtesycar {
          fleetnumber
          id
          make
          model
          plate
          year
        }
        id
        start
        status
        scheduledreturn
      }
      cieca_pfl
      cieca_pfo
      cieca_pft
      cieca_ttl
      class
      clm_no
      clm_total
      comment
      converted
      dms_id
      dms_customer_id
      dms_advisor_id
      csiinvites {
        completedon
        id
      }
      date_estimated
      date_exported
      date_invoiced
      date_last_contacted
      date_lost_sale
      date_next_contact
      date_open
      date_rentalresp
      date_repairstarted
      date_scheduled
      date_towin
      date_void
      ded_amt
      ded_note
      ded_status
      deliverchecklist
      depreciation_taxes
      dms_id
      dms_advisor_id
      dms_customer_id
      driveable
      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
      }
      est_co_nm
      est_ct_fn
      est_ct_ln
      est_ea
      est_ph1
      flat_rate_ats
      federal_tax_rate
      hit_and_run
      id
      inproduction
      ins_addr1
      ins_city
      ins_co_id
      ins_co_nm
      ins_ct_fn
      ins_ct_ln
      ins_ea
      ins_ph1
      intakechecklist
      invoice_final_note
      iouparent
      job_totals
      job_watchers {
        id
        user_email
      }
      joblines(where: { removed: { _eq: false } }, order_by: { line_no: asc }) {
        act_price
        act_price_before_ppc
        ah_detail_line
        alt_partm
        alt_partno
        assigned_team
        billlines(limit: 1, order_by: { bill: { date: desc } }) {
          actual_cost
          actual_price
          bill {
            id
            invoice_number
            vendor {
              id
              name
            }
          }
          id
          joblineid
          quantity
        }
        convertedtolbr
        critical
        db_hrs
        db_price
        db_ref
        id
        ioucreated
        lbr_amt
        lbr_op
        line_desc
        line_ind
        line_no
        line_ref
        location
        manual_line
        mod_lb_hrs
        mod_lbr_ty
        notes
        oem_partno
        op_code_desc
        parts_dispatch_lines(limit: 1, order_by: { accepted_at: desc }) {
          accepted_at
          id
          parts_dispatch {
            employeeid
            id
          }
        }
        part_qty
        part_type
        prt_dsmk_m
        prt_dsmk_p
        status
        tax_part
        unq_seq
        include_in_part_cnt
      }
      kmin
      kmout
      labor_rate_desc
      lbr_adjustments
      local_tax_rate
      loss_date
      loss_desc
      loss_of_use
      lost_sale_reason
      materials
      other_amount_payable
      owner {
        id
        note
        ownr_addr1
        ownr_addr2
        ownr_city
        ownr_co_nm
        ownr_ctry
        ownr_ea
        ownr_fn
        ownr_ln
        ownr_ph1
        ownr_ph2
        ownr_ph1_ty
        ownr_ph2_ty
        ownr_st
        ownr_zip
        tax_number
      }
      ownerid
      owner_owing
      ownr_addr1
      ownr_addr2
      ownr_city
      ownr_co_nm
      ownr_ctry
      ownr_ea
      ownr_fn
      ownr_ln
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      ownr_st
      ownr_zip
      parts_tax_rates
      payments {
        amount
        created_at
        date
        exportedat
        id
        jobid
        memo
        payer
        paymentnum
        transactionid
        type
      }
      plate_no
      plate_st
      po_number
      policy_no
      production_vars
      rate_ats
      rate_ats_flat
      rate_la1
      rate_la2
      rate_la3
      rate_la4
      rate_laa
      rate_lab
      rate_lad
      rate_lae
      rate_laf
      rate_lag
      rate_lam
      rate_lar
      rate_las
      rate_lau
      rate_ma2s
      rate_ma2t
      rate_ma3s
      rate_mabl
      rate_macs
      rate_mahw
      rate_mapa
      rate_mash
      rate_matd
      referral_source
      referral_source_extra
      regie_number
      remove_from_ar
      ro_number
      scheduled_completion
      scheduled_delivery
      scheduled_in
      selling_dealer
      estimate_approved
      estimate_sent_approval
      selling_dealer_contact
      servicing_dealer
      servicing_dealer_contact
      special_coverage_policy
      state_tax_rate
      status
      storage_payable
      suspended
      tax_lbr_rt
      tax_levies_rt
      tax_paint_mat_rt
      tax_registration_number
      tax_shop_mat_rt
      tax_str_rt
      tax_sub_rt
      tax_tow_rt
      tlos_ind
      towin
      towing_payable
      unit_number
      updated_at
      v_color
      v_make_desc
      v_model_yr
      v_model_desc
      v_vin
      notes(where: { pinned: { _eq: true } }, order_by: { updated_at: desc }) {
        created_at
        created_by
        critical
        id
        jobid
        private
        text
        updated_at
        audit
        type
        pinned
      }
      vehicle {
        id
        jobs {
          clm_no
          id
          ro_number
          status
        }
        notes
        plate_no
        plate_st
        v_color
        v_make_desc
        v_model_yr
        v_model_desc
        v_paint_codes
        v_vin
      }
      vehicleid
      voided
    }
  }
`;

export const GET_JOB_RECONCILIATION_BY_PK = gql`
  query GET_JOB_RECONCILIATION_BY_PK($id: uuid!) {
    bills(where: { jobid: { _eq: $id } }) {
      id
      vendorid
      vendor {
        id
        name
      }
      total
      invoice_number
      date
      federal_tax_rate
      state_tax_rate
      local_tax_rate
      is_credit_memo
      isinhouse
      exported
      billlines(where: { deductedfromlbr: { _eq: false } }) {
        actual_price
        quantity
        actual_cost
        cost_center
        id
        joblineid
        line_desc
        applicable_taxes
        deductedfromlbr
        jobline {
          id
          removed
        }
      }
    }
    jobs_by_pk(id: $id) {
      id
      joblines(order_by: { line_no: asc }) {
        id
        removed
        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
        db_ref
        manual_line
        misc_amt
      }
    }
  }
`;

export const QUERY_JOB_CARD_DETAILS = gql`
  query QUERY_JOB_CARD_DETAILS($id: uuid!) {
    jobs_by_pk(id: $id) {
      ownr_fn
      employee_body
      employee_refinish
      alt_transport
      inproduction
      production_vars
      ownr_ln
      ownr_co_nm
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      comment
      ownr_ea
      ca_gst_registrant
      owner_owing
      special_coverage_policy
      suspended
      lbr_adjustments
      available_jobs {
        id
      }
      joblines_status {
        part_type
        count
        status
      }

      joblines(where: { removed: { _eq: false } }, order_by: { line_no: asc }) {
        id
        alt_partm
        line_no
        unq_seq
        line_ind
        line_desc
        line_ref
        part_type
        part_qty
        mod_lbr_ty
        db_hrs
        mod_lb_hrs
        lbr_op
        lbr_amt
        op_code_desc
        status
        notes
        location
        tax_part
        db_ref
        manual_line
        prt_dsmk_p
        prt_dsmk_m
        ioucreated
        convertedtolbr
        critical
        parts_dispatch_lines {
          id
          accepted_at
          quantity
          parts_dispatch {
            id
            employeeid
            dispatched_at
            dispatched_by
            number
          }
        }
      }
      owner {
        id
        preferred_contact
        tax_number
      }
      vehicleid
      v_model_yr
      v_make_desc
      v_model_desc
      v_color
      v_vin
      plate_st
      plate_no
      vehicle {
        id
        v_model_yr
        v_make_desc
        v_model_desc
        v_color
        plate_no
        notes
        jobs {
          id
          clm_no
          ro_number
        }
      }
      actual_completion
      actual_delivery
      actual_in
      scheduled_in
      po_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
      job_totals
      area_of_damage
      ro_number
      scheduled_completion
      scheduled_in
      scheduled_delivery
      date_invoiced
      date_last_contacted
      date_next_contact
      date_open
      date_exported
      date_repairstarted
      date_scheduled
      estimate_sent_approval
      estimate_approved
      date_estimated
      employee_body_rel {
        id
        first_name
        last_name
      }
      employee_refinish_rel {
        id
        first_name
        last_name
      }
      employee_prep_rel {
        id
        first_name
        last_name
      }
      employee_csr_rel {
        id
        first_name
        last_name
      }
      notes {
        id
        text
        critical
        private
        created_at
        pinned
        type
      }
      updated_at
      clm_total
      ded_amt
      voided
      cccontracts {
        id
        agreementnumber
        status
        start
        scheduledreturn
        courtesycar {
          id
          make
          model
          year
          plate
          fleetnumber
        }
      }
      documents(limit: 3, order_by: { created_at: desc }) {
        id
        key
        type
      }
    }
  }
`;

export const QUERY_TECH_JOB_DETAILS = gql`
  query QUERY_TECH_JOB_DETAILS($id: uuid!) {
    jobs_by_pk(id: $id) {
      ownr_fn
      ownr_ln
      v_model_yr
      v_make_desc
      v_model_desc
      v_color
      plate_no
      actual_completion
      actual_delivery
      actual_in
      id
      ins_co_nm
      clm_no
      status
      job_totals
      area_of_damage
      ro_number
      scheduled_completion
      scheduled_in
      scheduled_delivery
      date_invoiced
      date_last_contacted
      date_next_contact
      date_open
      date_exported
      voided
      date_scheduled
      date_estimated
      employee_body
      employee_refinish
      employee_prep
      joblines(where: { removed: { _eq: false } }) {
        id
        unq_seq
        line_ind
        tax_part
        line_desc
        prt_dsmk_p
        prt_dsmk_m
        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
      }
      notes {
        id
        text
        critical
        private
        created_at
        pinned
        type
      }
      updated_at
      documents(order_by: { created_at: desc }) {
        id
        key
        size
        type
      }
    }
  }
`;

export const UPDATE_JOB = gql`
  mutation UPDATE_JOB($jobId: uuid!, $job: jobs_set_input!) {
    update_jobs(where: { id: { _eq: $jobId } }, _set: $job) {
      returning {
        id
        comment
        date_exported
        status
        alt_transport
        ro_number
        production_vars
        lbr_adjustments
        suspended
        queued_for_parts
        scheduled_completion
        scheduled_delivery
        actual_in
        actual_completion
        date_repairstarted
        date_void
        date_lost_sale
        estimate_sent_approval
        estimate_approved
      }
    }
  }
`;
export const JOB_PRODUCTION_TOGGLE = gql`
  mutation UPDATE_JOB($jobId: uuid!, $job: jobs_set_input!) {
    update_jobs(where: { id: { _eq: $jobId } }, _set: $job) {
      returning {
        id
        status
        inproduction
        actual_completion
        scheduled_delivery
        actual_delivery
        scheduled_completion
      }
    }
  }
`;

export const UPDATE_JOB_ASSIGNMENTS = gql`
  mutation UPDATE_JOB_ASSIGNMENTS($jobId: uuid!, $job: jobs_set_input!) {
    update_jobs(where: { id: { _eq: $jobId } }, _set: $job) {
      returning {
        id
        employee_body_rel {
          id
          first_name
          last_name
        }
        employee_refinish_rel {
          id
          first_name
          last_name
        }
        employee_prep_rel {
          id
          first_name
          last_name
        }
        employee_csr_rel {
          id
          first_name
          last_name
        }
        employee_csr
        employee_body
        employee_prep
      }
    }
  }
`;

export const VOID_JOB = gql`
  mutation VOID_JOB($jobId: uuid!, $job: jobs_set_input!, $note: [notes_insert_input!]!) {
    update_jobs_by_pk(_set: $job, pk_columns: { id: $jobId }) {
      id
      date_exported
      date_void
      status
      alt_transport
      ro_number
      production_vars
      lbr_adjustments
    }
    insert_notes(objects: $note) {
      affected_rows
    }
    update_appointments(where: { jobid: { _eq: $jobId } }, _set: { canceled: true }) {
      returning {
        id
        canceled
      }
    }
  }
`;

export const UPDATE_JOBS = gql`
  mutation UPDATE_JOBS($jobIds: [uuid!]!, $fields: jobs_set_input!) {
    update_jobs(where: { id: { _in: $jobIds } }, _set: $fields) {
      returning {
        id
        date_exported
        status
      }
    }
  }
`;

export const CONVERT_JOB_TO_RO = gql`
  mutation CONVERT_JOB_TO_RO($jobId: uuid!, $job: jobs_set_input!) {
    update_jobs(where: { id: { _eq: $jobId } }, _set: $job) {
      returning {
        id
        ro_number
        converted
        class
        ins_co_nm
        referral_source
        referral_source_extra
        employee_csr
        employee_csr_rel {
          id
        }
      }
    }
  }
`;

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

export const GET_JOB_INFO_FOR_STRIPE = gql`
  query GET_JOB_INFO_FOR_STRIPE($jobid: uuid!) {
    jobs_by_pk(id: $jobid) {
      id
      ro_number
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      ownr_ea
    }
  }
`;

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
        status
        inproduction
      }
    }
  }
`;

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

export const SEARCH_JOBS_FOR_AUTOCOMPLETE = gql`
  query SEARCH_JOBS_FOR_AUTOCOMPLETE(
    $search: String
    $isConverted: Boolean
    $notExported: Boolean
    $notInvoiced: Boolean
  ) {
    search_jobs(
      args: { search: $search }
      limit: 25
      where: {
        _and: {
          converted: { _eq: $isConverted }
          date_exported: { _is_null: $notExported }
          date_invoiced: { _is_null: $notInvoiced }
        }
      }
    ) {
      id
      ownr_co_nm
      ownr_fn
      ownr_ln
      ro_number
      clm_no
      vehicleid
      v_make_desc
      v_model_desc
      v_model_yr
      status
    }
  }
`;
export const SEARCH_JOBS_BY_ID_FOR_AUTOCOMPLETE = gql`
  query SEARCH_JOBS_BY_ID_FOR_AUTOCOMPLETE($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      ownr_co_nm
      ownr_fn
      ownr_ln
      ro_number
      clm_no
      vehicleid
      v_make_desc
      v_model_desc
      v_model_yr
      status
    }
  }
`;

export const SEARCH_FOR_JOBS = gql`
  query SEARCH_FOR_JOBS($search: String!) {
    search_jobs(args: { search: $search }, limit: 25) {
      id
      ro_number
      ownr_co_nm
      ownr_fn
      ownr_ln
    }
  }
`;

export const QUERY_JOB_FOR_DUPE = gql`
  query QUERY_JOB_FOR_DUPE($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      agt_addr1
      agt_addr2
      agt_city
      agt_co_id
      agt_co_nm
      agt_ct_fn
      agt_ct_ln
      agt_ct_ph
      agt_ct_phx
      agt_ctry
      agt_ea
      agt_faxx
      agt_fax
      agt_lic_no
      agt_ph1
      agt_ph1x
      agt_ph2
      agt_zip
      agt_st
      agt_ph2x
      area_of_damage
      cat_no
      cieca_pfl
      cieca_pfo
      cieca_pft
      cieca_stl
      cieca_ttl
      clm_addr1
      clm_addr2
      clm_city
      clm_ct_fn
      clm_ct_ln
      clm_ct_ph
      clm_ct_phx
      clm_ctry
      clm_ea
      clm_fax
      clm_faxx
      clm_ofc_id
      clm_ofc_nm
      clm_ph1
      clm_ph1x
      clm_ph2
      clm_ph2x
      clm_st
      clm_title
      clm_total
      clm_zip
      cust_pr
      est_addr1
      est_addr2
      est_city
      est_co_nm
      est_ct_fn
      est_ct_ln
      est_ctry
      est_ea
      est_ph1
      est_st
      est_zip
      federal_tax_rate
      ins_addr1
      ins_addr2
      ins_city
      ins_co_id
      ins_co_nm
      ins_ct_fn
      ins_ct_ln
      ins_ct_ph
      ins_ct_phx
      ins_ctry
      ins_ea
      ins_fax
      ins_faxx
      ins_memo
      ins_ph1
      ins_ph1x
      ins_ph2
      ins_ph2x
      ins_st
      ins_title
      ins_zip
      insd_addr1
      insd_addr2
      insd_city
      insd_co_nm
      insd_ctry
      insd_ea
      insd_fax
      insd_faxx
      insd_fn
      insd_ln
      insd_ph1
      insd_ph1x
      insd_ph2
      insd_ph2x
      insd_st
      insd_title
      insd_zip
      labor_rate_desc
      labor_rate_id
      local_tax_rate
      materials
      other_amount_payable
      owner_owing
      ownerid
      ownr_addr1
      ownr_addr2
      ownr_city
      ownr_co_nm
      ownr_ctry
      ownr_ea
      ownr_fax
      ownr_faxx
      ownr_fn
      ownr_ln
      ownr_ph1
      ownr_ph1x
      ownr_ph1_ty
      ownr_ph2
      ownr_ph2x
      ownr_ph2_ty
      ownr_st
      ownr_title
      ownr_zip
      parts_tax_rates
      plate_no
      plate_st
      po_number
      rate_la1
      rate_la2
      rate_la3
      rate_la4
      rate_laa
      rate_lab
      rate_lad
      rate_lae
      rate_lag
      rate_laf
      rate_lam
      rate_lar
      rate_las
      rate_lau
      rate_ma2s
      rate_ma2t
      rate_ma3s
      rate_mabl
      rate_macs
      rate_mahw
      rate_mapa
      rate_mash
      rate_matd
      regie_number
      selling_dealer
      selling_dealer_contact
      servicing_dealer
      servicing_dealer_contact
      shopid
      state_tax_rate
      tax_lbr_rt
      tax_levies_rt
      tax_paint_mat_rt
      tax_predis
      tax_prethr
      tax_pstthr
      tax_str_rt
      tax_sub_rt
      tax_thramt
      tax_tow_rt
      unit_number
      v_color
      v_make_desc
      v_model_desc
      v_model_yr
      v_vin
      vehicleid
      joblines(where: { removed: { _eq: false } }) {
        act_price
        alt_co_id
        alt_overrd
        alt_part_i
        alt_partm
        bett_amt
        alt_partno
        bett_pctg
        bett_tax
        bett_type
        cert_part
        db_hrs
        db_price
        db_ref
        est_seq
        glass_flag
        id
        lbr_amt
        lbr_hrs_j
        lbr_inc
        lbr_op
        lbr_op_j
        lbr_tax
        lbr_typ_j
        line_desc
        line_ind
        line_ref
        misc_amt
        misc_sublt
        misc_tax
        mod_lb_hrs
        mod_lbr_ty
        oem_partno
        op_code_desc
        paint_stg
        paint_tone
        part_qty
        part_type
        price_inc
        price_j
        prt_dsmk_m
        prt_dsmk_p
        status
        tax_part
        unq_seq
        manual_line
        notes
        line_no
        tran_code
      }
      driveable
      towin
      adj_g_disc
      adj_strdis
      adj_towdis
      ca_gst_registrant
      special_coverage_policy
      tax_registration_number
      tax_shop_mat_rt
    }
  }
`;

export const QUERY_ALL_JOB_FIELDS = gql`
  query QUERY_ALL_JOB_FIELDS($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      adj_g_disc
      adj_strdis
      adj_towdis
      adjustment_bottom_line
      agt_addr1
      agt_addr2
      agt_city
      agt_co_id
      agt_co_nm
      agt_ct_fn
      agt_ct_ln
      agt_ct_ph
      agt_ct_phx
      agt_ctry
      agt_ea
      agt_faxx
      agt_fax
      agt_lic_no
      agt_ph1
      agt_ph1x
      agt_ph2
      agt_zip
      agt_st
      agt_ph2x
      area_of_damage
      cat_no
      cieca_stl
      cieca_ttl
      clm_addr1
      clm_addr2
      clm_city
      clm_ct_fn
      clm_ct_ln
      clm_ct_ph
      clm_ct_phx
      clm_ctry
      clm_ea
      clm_fax
      clm_faxx
      clm_ofc_id
      clm_ofc_nm
      clm_ph1
      clm_ph1x
      clm_ph2
      clm_ph2x
      clm_st
      clm_title
      clm_total
      clm_zip
      cust_pr
      ded_amt
      ded_status
      depreciation_taxes
      est_addr1
      est_addr2
      est_city
      est_co_nm
      est_ct_fn
      est_ct_ln
      est_ctry
      est_ea
      est_ph1
      est_st
      est_zip
      federal_tax_rate
      g_bett_amt
      ins_addr1
      ins_addr2
      ins_city
      ins_co_id
      ins_co_nm
      ins_ct_fn
      ins_ct_ln
      ins_ct_ph
      ins_ct_phx
      ins_ctry
      ins_ea
      ins_fax
      ins_faxx
      ins_memo
      ins_ph1
      ins_ph1x
      ins_ph2
      ins_ph2x
      ins_st
      ins_title
      ins_zip
      insd_addr1
      insd_addr2
      insd_city
      insd_co_nm
      insd_ctry
      insd_ea
      insd_fax
      insd_faxx
      insd_fn
      insd_ln
      insd_ph1
      insd_ph1x
      insd_ph2
      insd_ph2x
      insd_st
      insd_title
      insd_zip
      job_totals
      labor_rate_desc
      labor_rate_id
      local_tax_rate
      other_amount_payable
      owner_owing
      ownerid
      ownr_addr1
      ownr_addr2
      ownr_city
      ownr_co_nm
      ownr_ctry
      ownr_ea
      ownr_fax
      ownr_faxx
      ownr_fn
      ownr_ln
      ownr_ph1
      ownr_ph1x
      ownr_ph1_ty
      ownr_ph2
      ownr_ph2x
      ownr_ph2_ty
      ownr_st
      ownr_title
      ownr_zip
      parts_tax_rates
      pay_amt
      pay_chknm
      pay_type
      payee_nms
      plate_no
      plate_st
      po_number
      policy_no
      rate_la1
      rate_la2
      rate_la3
      rate_la4
      rate_laa
      rate_lab
      rate_lad
      rate_lae
      rate_lag
      rate_laf
      rate_lam
      rate_lar
      rate_las
      rate_lau
      rate_ma2s
      rate_ma2t
      rate_ma3s
      rate_mabl
      rate_macs
      rate_mahw
      rate_mapa
      rate_mash
      rate_matd
      referral_source
      referral_source_extra
      regie_number
      selling_dealer
      selling_dealer_contact
      servicing_dealer
      servicing_dealer_contact
      shopid
      special_coverage_policy
      state_tax_rate
      storage_payable
      tax_lbr_rt
      tax_levies_rt
      tax_paint_mat_rt
      tax_predis
      tax_prethr
      tax_pstthr
      tax_str_rt
      tax_sub_rt
      tax_thramt
      tax_tow_rt
      theft_ind
      tlos_ind
      towing_payable
      unit_number
      v_color
      v_make_desc
      v_model_desc
      v_model_yr
      v_vin
      vehicleid
      joblines(where: { removed: { _eq: false } }) {
        act_price
        alt_co_id
        alt_overrd
        alt_part_i
        alt_partm
        bett_amt
        alt_partno
        bett_pctg
        bett_tax
        bett_type
        cert_part
        db_hrs
        db_price
        db_ref
        est_seq
        glass_flag
        id
        lbr_amt
        lbr_hrs_j
        lbr_inc
        lbr_op
        lbr_op_j
        lbr_tax
        lbr_typ_j
        line_desc
        line_ind
        line_ref
        misc_amt
        misc_sublt
        misc_tax
        mod_lb_hrs
        mod_lbr_ty
        oem_partno
        op_code_desc
        paint_stg
        paint_tone
        part_qty
        part_type
        price_inc
        price_j
        prt_dsmk_m
        prt_dsmk_p
        status
        tax_part
        unq_seq
        manual_line
      }
      employee_body
      employee_refinish
      employee_prep
      driveable
      towin
    }
  }
`;

export const QUERY_ALL_JOBS_PAGINATED_STATUS_FILTERED = gql`
  query QUERY_ALL_JOBS_PAGINATED_STATUS_FILTERED(
    $offset: Int
    $limit: Int
    $order: [jobs_order_by!]
    $statusList: [String!]
  ) {
    jobs(offset: $offset, limit: $limit, order_by: $order, where: { status: { _in: $statusList } }) {
      comment
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      plate_no
      plate_st
      v_vin
      v_model_yr
      v_model_desc
      v_make_desc
      v_color
      vehicleid
      id
      ins_co_nm
      clm_no
      clm_total
      owner_owing
      ro_number
      po_number
      status
      updated_at
      ded_amt
    }
    jobs_aggregate(where: { status: { _in: $statusList } }) {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;
export const QUERY_SIMPLIFIED_PARTS_PAGINATED_STATUS_FILTERED = gql`
  query QUERY_ALL_JOBS_PAGINATED_STATUS_FILTERED(
    $offset: Int
    $limit: Int
    $order: [jobs_order_by!]
    $statusList: [String!]
  ) {
    jobs(offset: $offset, limit: $limit, order_by: $order, where: { status: { _in: $statusList } }) {
      comment
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      plate_no
      plate_st
      v_vin
      v_model_yr
      v_model_desc
      v_make_desc
      v_color
      vehicleid
      id
      ins_co_nm
      clm_no
      clm_total
      owner_owing
      ro_number
      po_number
      converted
      status
      updated_at
      ded_amt
      joblines_status {
        count
        part_type
        status
      }
    }
    jobs_aggregate(where: { status: { _in: $statusList } }) {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;

export const QUERY_JOB_CLOSE_DETAILS = gql`
  query QUERY_JOB_CLOSE_DETAILS($id: uuid!) {
    jobs_by_pk(id: $id) {
      ro_number
      invoice_allocation
      invoice_final_note
      ins_co_id
      dms_allocation
      id
      inproduction
      ded_amt
      ded_status
      depreciation_taxes
      other_amount_payable
      towing_payable
      storage_payable
      adjustment_bottom_line
      federal_tax_rate
      state_tax_rate
      local_tax_rate
      tax_tow_rt
      tax_str_rt
      tax_paint_mat_rt
      tax_sub_rt
      tax_lbr_rt
      tax_levies_rt
      parts_tax_rates
      job_totals
      rate_la1
      rate_la2
      rate_la3
      rate_la4
      rate_laa
      rate_lab
      rate_lad
      rate_lae
      rate_laf
      rate_lag
      rate_lam
      rate_lar
      rate_las
      rate_lau
      rate_ma2s
      rate_ma2t
      rate_ma3s
      rate_mabl
      rate_macs
      rate_mahw
      rate_mapa
      rate_mash
      rate_matd
      status
      date_exported
      date_invoiced
      voided
      scheduled_completion
      actual_completion
      scheduled_delivery
      actual_delivery
      scheduled_in
      date_invoiced
      actual_in
      kmin
      kmout
      qb_multiple_payers
      lbr_adjustments
      ownr_ea
      dms_id
      dms_customer_id
      dms_advisor_id
      payments {
        amount
        created_at
        date
        exportedat
        id
        jobid
        memo
        payer
        paymentnum
        transactionid
        type
      }
      joblines(where: { removed: { _eq: false } }, order_by: { line_no: asc }) {
        id
        removed
        tax_part
        line_desc
        prt_dsmk_p
        prt_dsmk_m
        part_type
        oem_partno
        db_price
        act_price
        part_qty
        notes
        mod_lbr_ty
        db_hrs
        mod_lb_hrs
        lbr_op
        lbr_amt
        op_code_desc
        profitcenter_labor
        profitcenter_part
        prt_dsmk_p
        convertedtolbr
        convertedtolbr_data
        act_price_before_ppc
        sublet_ignored
        sublet_completed
      }
    }
  }
`;

export const generate_UPDATE_JOB_KANBAN = (
  oldChildId,
  oldChildNewParent,
  movedId,
  movedNewParent,
  movedNewStatus,
  newChildId,
  newChildParent
) => {
  const oldChildQuery = `
  updateOldChild: update_jobs(where: { id: { _eq: "${oldChildId}" } },
  _set: {kanbanparent: ${oldChildNewParent ? `"${oldChildNewParent}"` : null}}) {
    returning {
      id
      kanbanparent
    }
  }`;

  const movedQuery = `
  updateMovedChild: update_jobs(where: { id: { _eq: "${movedId}" } },
  _set: {kanbanparent: ${movedNewParent ? `"${movedNewParent}"` : null} , status: "${movedNewStatus}"}) {
    returning {
      id
      status
      kanbanparent
    }
  }`;

  const newChildQuery = `
  updateNewChild: update_jobs(where: { id: { _eq: "${newChildId}" } },
  _set: {kanbanparent: ${newChildParent ? `"${newChildParent}"` : null}}) {
    returning {
      id
      kanbanparent
    }
  }`;

  return gql`
    mutation UPDATE_JOB_KANBAN {
      ${oldChildId ? oldChildQuery : ""}
      ${movedId ? movedQuery : ""}
      ${newChildId ? newChildQuery : ""}
    }
  `;
};

export const QUERY_JOB_LBR_ADJUSTMENTS = gql`
  query QUERY_JOB_LBR_ADJUSTMENTS($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      lbr_adjustments
    }
  }
`;

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

export const QUERY_GET_TASKS_JOB_DETAILS_BY_ID = gql`
  query QUERY_GET_TASKS_JOB_DETAILS_BY_ID($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      scheduled_delivery
      scheduled_completion
      joblines {
        id
        line_desc
      }
      bills {
        id
        vendor {
          id
          name
        }
        invoice_number
      }
      parts_orders {
        id
        vendor {
          id
          name
        }
        order_number
      }
    }
  }
`;

export const GET_JOB_FOR_CC_CONTRACT = gql`
  query GET_JOB_FOR_CC_CONTRACT($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      ownr_fn
      ownr_ln
      ownr_addr1
      ownr_st
      ownr_city
      ownr_zip
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
    }
  }
`;

export const QUERY_JOB_CHECKLISTS = gql`
  query QUERY_JOB_CHECKLISTS($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      deliverchecklist
      intakechecklist
      ro_number
      scheduled_completion
      actual_completion
      scheduled_delivery
      actual_delivery
      production_vars
      owner {
        id
      }
      bodyshop {
        id
        intakechecklist
        deliverchecklist
      }
    }
  }
`;

export const FIND_JOBS_BY_CLAIM = gql`
  query FIND_JOBS_BY_CLAIM($claimNumbers: String!) {
    jobs(where: { clm_no: { _similar: $claimNumbers } }) {
      id
      clm_no
      ro_number
      actual_completion
      ownr_fn
      ownr_ln
      ownr_co_nm
    }
  }
`;

export const QUERY_JOB_EXPORT_DMS = gql`
  query QUERY_JOB_EXPORT_DMS($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      ro_number
      po_number
      clm_no
      job_totals
      ded_amt
      ded_status
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_ph1
      ownr_ph2
      ins_co_nm
      kmin
      kmout
      v_make_desc
      v_model_yr
      v_model_desc
      area_of_damage
      date_exported
      v_vin
      plate_no
      plate_st
      ownr_co_nm
      dms_id
      dms_customer_id
      dms_advisor_id
    }
  }
`;

export const QUERY_RELATED_ROS = gql`
  query QUERY_RELATED_ROS($jobid: uuid!) {
    relatedjobs(where: { _or: [{ childjob: { _eq: $jobid } }, { parentjob: { _eq: $jobid } }] }) {
      parentjob
      id
      parentjob_rel {
        id
        ro_number
      }
      childjob
      childjob_rel {
        id
        ro_number
      }
    }
  }
`;

export const INSERT_RELATED_ROS = gql`
  mutation INSERT_RELATED_ROS($relationship: relatedjobs_insert_input!) {
    insert_relatedjobs_one(object: $relationship) {
      parentjob
      id
      parentjob_rel {
        id
        ro_number
      }
      childjob
      childjob_rel {
        id
        ro_number
      }
    }
  }
`;

export const DELETE_RELATED_RO = gql`
  mutation DELETE_RELATED_RO($relationshipid: uuid!) {
    delete_relatedjobs_by_pk(id: $relationshipid) {
      id
    }
  }
`;

export const GET_JOB_LINE_ORDERS = gql`
  query GET_JOB_LINE_ORDERS($joblineid: uuid!) {
    billlines(where: { joblineid: { _eq: $joblineid } }) {
      actual_cost
      actual_price
      billid
      quantity
      bill {
        id
        invoice_number
        date
        vendorid
        vendor {
          id
          name
        }
      }
    }
    parts_dispatch_lines(where: { joblineid: { _eq: $joblineid } }) {
      id
      accepted_at
      quantity
      parts_dispatch {
        id
        employeeid
        dispatched_at
        dispatched_by
        number
      }
    }
    parts_order_lines(where: { job_line_id: { _eq: $joblineid } }) {
      id
      act_price
      backordered_eta
      backordered_on
      parts_order {
        id
        order_date
        order_number
        orderedby
        return
        comments
        vendor {
          id
          name
        }
      }
    }
  }
`;

export const UPDATE_REMOVE_FROM_AR = gql`
  mutation UPDATE_REMOVE_FROM_AR($jobId: uuid!, $remove_from_ar: Boolean!) {
    update_jobs_by_pk(pk_columns: { id: $jobId }, _set: { remove_from_ar: $remove_from_ar }) {
      id
      remove_from_ar
    }
  }
`;

export const UNVOID_JOB = gql`
  mutation UNVOID_JOB($jobId: uuid!, $default_imported: String!, $currentUserEmail: String!, $text: String!) {
    update_jobs_by_pk(pk_columns: { id: $jobId }, _set: { voided: false, status: $default_imported, date_void: null }) {
      id
      date_void
      voided
      status
    }
    insert_notes(objects: { jobid: $jobId, audit: true, created_by: $currentUserEmail, text: $text }) {
      returning {
        id
      }
    }
  }
`;

export const DELETE_INTAKE_CHECKLIST = gql`
  mutation DELETE_INTAKE($jobId: uuid!) {
    update_jobs_by_pk(pk_columns: { id: $jobId }, _set: { intakechecklist: null }) {
      id
      intakechecklist
    }
  }
`;

export const DELETE_DELIVERY_CHECKLIST = gql`
  mutation DELETE_DELIVERY($jobId: uuid!) {
    update_jobs_by_pk(pk_columns: { id: $jobId }, _set: { deliverchecklist: null }) {
      id
      deliverchecklist
    }
  }
`;

export const MARK_JOB_FOR_REEXPORT = gql`
  mutation MARK_JOB_FOR_REEXPORT($jobId: uuid!, $default_invoiced: String!) {
    update_jobs_by_pk(pk_columns: { id: $jobId }, _set: { date_exported: null, status: $default_invoiced }) {
      id
      date_exported
      status
      date_invoiced
    }
  }
`;

export const MARK_JOB_AS_EXPORTED = gql`
  mutation MARK_JOB_AS_EXPORTED($jobId: uuid!, $date_exported: timestamptz!, $default_exported: String!) {
    update_jobs_by_pk(pk_columns: { id: $jobId }, _set: { date_exported: $date_exported, status: $default_exported }) {
      id
      date_exported
      date_invoiced
      status
    }
  }
`;

export const MARK_JOB_AS_UNINVOICED = gql`
  mutation MARK_JOB_AS_UNINVOICED($jobId: uuid!, $default_delivered: String!) {
    update_jobs_by_pk(
      pk_columns: { id: $jobId }
      _set: { date_exported: null, date_invoiced: null, status: $default_delivered, admin_clerk: null }
    ) {
      id
      date_exported
      date_invoiced
      status
      admin_clerk
    }
  }
`;

export const QUERY_COMPLETED_TASKS = gql`
  query QUERY_COMPLETED_TASKS($jobid: uuid!) {
    jobs_by_pk(id: $jobid) {
      id
      completed_tasks
    }
  }
`;

export const QUERY_JOBS_TECH_ASIGNED_TO_BY_TEAM = gql`
  query QUERY_JOBS_TECH_ASIGNED_TO_BY_TEAM($teamIds: [uuid!]!) {
    jobs(where: { inproduction: { _eq: true }, joblines: { assigned_team: { _in: $teamIds } } }) {
      id
      v_make_desc
      v_model_desc
      v_color
      v_vin
      plate_no
      plate_st
      clm_no
      ownr_fn
      ownr_ln
      ownr_co_nm
      status
      ro_number
    }
  }
`;

export const QUERY_PARTS_QUEUE_CARD_DETAILS = gql`
  query QUERY_PARTS_QUEUE_CARD_DETAILS($id: uuid!) {
    jobs_by_pk(id: $id) {
      actual_completion
      actual_delivery
      actual_in
      alt_transport
      available_jobs {
        id
      }
      area_of_damage
      ca_gst_registrant
      cccontracts {
        agreementnumber
        courtesycar {
          id
          make
          model
          year
          plate
          fleetnumber
        }
        id
        scheduledreturn
        start
        status
      }
      notes(where: { pinned: { _eq: true } }, order_by: { updated_at: desc }) {
        created_at
        created_by
        critical
        id
        jobid
        private
        text
        updated_at
        audit
        type
        pinned
      }
      clm_no
      clm_total
      comment
      date_estimated
      date_exported
      date_invoiced
      date_last_contacted
      date_next_contact
      date_open
      date_repairstarted
      date_scheduled
      ded_amt
      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
      }
      est_co_nm
      est_ct_fn
      est_ct_ln
      est_ea
      est_ph1
      id
      ins_co_nm
      ins_ct_fn
      ins_ct_ln
      ins_ea
      ins_ph1
      inproduction
      job_totals
      joblines(
        order_by: { line_no: asc }
        where: { part_type: { _in: ["PAN", "PAC", "PAR", "PAL", "PAA", "PAM", "PAP", "PAG"] }, removed: { _eq: false } }
      ) {
        act_price
        alt_partno
        db_ref
        id
        line_desc
        line_no
        location
        mod_lbr_ty
        mod_lb_hrs
        oem_partno
        part_qty
        part_type
        prt_dsmk_m
        status
      }
      lbr_adjustments
      ownr_co_nm
      ownr_ea
      ownr_fn
      ownr_ln
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      owner {
        id
        preferred_contact
        tax_number
      }
      owner_owing
      plate_no
      plate_st
      po_number
      production_vars
      estimate_sent_approval
      estimate_approved
      ro_number
      scheduled_completion
      scheduled_delivery
      scheduled_in
      special_coverage_policy
      status
      suspended
      updated_at
      vehicle {
        id
        jobs {
          id
          clm_no
          ro_number
        }
        notes
        plate_no
        v_color
        v_make_desc
        v_model_desc
        v_model_yr
      }
      vehicleid
      v_color
      v_make_desc
      v_model_desc
      v_model_yr
      v_vin
      voided
    }
  }
`;

export const SUBSCRIPTION_JOBS_IN_PRODUCTION = gql`
  subscription SUBSCRIPTION_JOBS_IN_PRODUCTION {
    jobs(where: { inproduction: { _eq: true } }) {
      id
      updated_at
    }
  }
`;
export const SUBSCRIPTION_JOBS_IN_PRODUCTION_VIEW = gql`
  subscription SUBSCRIPTION_JOBS_IN_PRODUCTION_VIEW {
    jobs: jobs_inproduction {
      id
      updated_at
    }
  }
`;

export const QUERY_JOBS_IN_PRODUCTION = gql`
  query QUERY_JOBS_IN_PRODUCTION {
    jobs(where: { inproduction: { _eq: true } }) {
      tasks_aggregate(where: { completed: { _eq: false }, deleted: { _eq: false } }) {
        aggregate {
          count
        }
      }
      id
      updated_at
      comment
      status
      category
      iouparent
      ro_number
      ownerid
      ownr_fn
      ownr_ln
      ownr_co_nm
      v_model_yr
      v_model_desc
      clm_no
      v_make_desc
      v_color
      vehicleid
      plate_no
      actual_in
      actual_completion
      scheduled_completion
      scheduled_delivery
      date_last_contacted
      date_next_contact
      ins_co_nm
      clm_total
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      special_coverage_policy
      owner_owing
      production_vars
      kanbanparent
      alt_transport
      employee_body
      employee_refinish
      employee_prep
      employee_csr
      est_ct_fn
      est_ct_ln
      suspended
      job_totals
      date_repairstarted
      dms_id
      joblines_status {
        part_type
        status
        count
      }
      labhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _neq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      larhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _eq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      subletLines: joblines(
        where: { _and: { part_type: { _in: ["PAS", "PASL"] }, removed: { _eq: false } } }
        order_by: { line_no: asc }
      ) {
        id
        line_desc
        sublet_ignored
        sublet_completed
        jobid
      }
    }
  }
`;

export const GET_JOB_BY_PK_QUICK_INTAKE = gql`
  query GET_JOB_BY_PK_QUICK_INTAKE($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      actual_in
      scheduled_completion
      actual_completion
      scheduled_delivery
      actual_delivery
      labhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _neq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      larhrs: joblines_aggregate(where: { _and: [{ mod_lbr_ty: { _eq: "LAR" } }, { removed: { _eq: false } }] }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
    }
  }
`;

export const GET_JOB_WATCHERS = gql`
  query GET_JOB_WATCHERS($jobid: uuid!) {
    job_watchers(where: { jobid: { _eq: $jobid } }) {
      id
      user_email
    }
  }
`;

export const ADD_JOB_WATCHER = gql`
  mutation ADD_JOB_WATCHER($jobid: uuid!, $userEmail: String!) {
    insert_job_watchers_one(object: { jobid: $jobid, user_email: $userEmail }) {
      id
      jobid
      user_email
    }
  }
`;

export const REMOVE_JOB_WATCHER = gql`
  mutation REMOVE_JOB_WATCHER($jobid: uuid!, $userEmail: String!) {
    delete_job_watchers(where: { jobid: { _eq: $jobid }, user_email: { _eq: $userEmail } }) {
      affected_rows
      returning {
        id
        user_email
      }
    }
  }
`;
