import { gql } from "graphql-tag";

export const QUERY_ALL_ACTIVE_JOBS = gql`
  query QUERY_ALL_ACTIVE_JOBS($statuses: [String!]!) {
    jobs(
      where: { status: { _in: $statuses } }
      order_by: { created_at: desc }
    ) {
      ownr_fn
      ownr_ln
      ownr_co_nm
      clm_no
      v_model_yr
      v_model_desc
      v_make_desc
      id
      owner_owing
      ro_number
      status
    }
  }
`;

export const SUBSCRIPTION_JOBS_IN_PRODUCTION = gql`
  subscription SUBSCRIPTION_JOBS_IN_PRODUCTION($statusList: [String!]!) {
    jobs(where: { inproduction: { _eq: true } }) {
      id
      status
      ro_number

      ownr_fn
      ownr_ln
      v_model_yr
      v_model_desc
      clm_no
      v_make_desc
      v_color
      plate_no
      actual_in
      scheduled_completion
      scheduled_delivery
      ins_co_nm
      clm_total
      ownr_ph1
      special_coverage_policy
      production_vars
      kanbanparent
      employee_body
      employee_body_rel {
        id
        first_name
        last_name
      }
      employee_refinish
      employee_refinish_rel {
        id
        first_name
        last_name
      }
      employee_prep
      employee_prep_rel {
        id
        first_name
        last_name
      }
      partcount: joblines_aggregate {
        nodes {
          status
        }
      }

      labhrs: joblines_aggregate(where: { mod_lbr_ty: { _eq: "LAB" } }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      larhrs: joblines_aggregate(where: { mod_lbr_ty: { _eq: "LAR" } }) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
    }
  }
`;

export const GET_JOB_BY_PK = gql`
  query GET_JOB_BY_PK($id: uuid!) {
    jobs_by_pk(id: $id) {
      updated_at
      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
      }
      loss_desc
      kmin
      kmout
      referral_source
      unit_number
      po_number
      special_coverage_policy
      scheduled_delivery
      converted
      ro_number
      clm_total
      inproduction
      vehicleid
      plate_no
      v_vin
      v_model_yr
      v_model_desc
      v_make_desc
      v_color

      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

      regie_number
      scheduled_completion
      id
      ded_amt
      ded_status
      depreciation_taxes
      production_vars
      other_amount_payable
      towing_payable
      storage_payable
      adjustment_bottom_line
      job_totals
      ownr_fn
      ownr_ln
      ownr_ea
      ownr_addr1
      ownr_addr2
      ownr_city
      ownr_st
      ownr_zip
      ownr_ctry
      ownr_ph1
      actual_in
      scheduled_completion
      scheduled_in
      actual_completion
      scheduled_delivery
      actual_delivery
      date_estimated
      date_open
      date_scheduled
      date_invoiced

      date_exported
      status
      owner_owing
      joblines(where: { removed: { _eq: false } }, order_by: { line_no: asc }) {
        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(order_by: { created_at: desc }) {
        id
        text
        critical
        private
        pinned
        created_at
        updated_at
        created_by
      }
      documents(order_by: { takenat: desc }) {
        id
        name
        key
        created_at
        type
        extension
      }
    }
  }
`;

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
      ownr_ln
      ownr_ph1
      ownr_ea
      joblines_status {
        part_type
        count
        status
      }
      owner {
        id
        allow_text_message
        preferred_contact
      }
      vehicleid
      v_model_yr
      v_make_desc
      v_model_desc
      v_color
      plate_no
      vehicle {
        id
        v_model_yr
        v_make_desc
        v_model_desc
        v_color
        plate_no
      }
      actual_completion
      actual_delivery
      actual_in

      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_open
      date_exported

      date_scheduled
      date_estimated

      notes {
        id
        text
        critical
        private
        created_at
      }
      updated_at
      clm_total
      ded_amt
      cccontracts {
        id
        agreementnumber
        status
        start
        scheduledreturn
      }
      documents(limit: 3, order_by: { takenat: desc }) {
        id
        key
      }
    }
  }
`;

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_open
      date_exported

      date_scheduled
      date_estimated
      employee_body
      employee_refinish
      employee_prep
      joblines {
        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
      }
      updated_at
      documents(order_by: { takenat: desc }) {
        id
        key
      }
    }
  }
`;

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

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

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

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

      vehicleid
      v_make_desc
      v_model_desc
      v_model_yr
    }
  }
`;

export const SEARCH_FOR_JOBS = gql`
  query SEARCH_FOR_JOBS($search: String!) {
    jobs(where: { ro_number: { _ilike: $search } }) {
      id
      ro_number
      ownr_fn
      ownr_ln
    }
  }
`;
//TODO Ensure this is always up to date.
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
      employee_csr_rel {
        id
        first_name
        last_name
      }
      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_ph2
      ownr_ph2x
      ownr_st
      ownr_title
      ownr_zip
      parts_tax_rates
      pay_amt
      pay_chknm
      pay_date
      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
      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 {
        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
      }
      employee_body
      employee_refinish
      employee_prep
    }
  }
`;

export const QUERY_ALL_JOBS_PAGINATED = gql`
  query QUERY_ALL_JOBS_PAGINATED(
    $search: String
    $offset: Int
    $limit: Int
    $order: [jobs_order_by!]!
  ) {
    search_jobs(
      args: { search: $search }
      offset: $offset
      limit: $limit
      order_by: $order
    ) {
      ownr_fn
      ownr_ln
      ownr_ph1
      ownr_ea
      plate_no
      plate_st
      v_vin
      v_model_yr
      v_model_desc
      v_make_desc
      v_color
      vehicleid
      actual_completion
      actual_delivery
      actual_in

      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
      vehicleid
    }
    search_jobs_aggregate(args: { search: $search }) {
      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
      clm_total
      inproduction
      plate_no
      v_vin
      v_model_yr
      v_model_desc
      v_make_desc
      v_color
      invoice_allocation
      ins_co_id
      policy_no
      clm_no
      ins_co_nm
      regie_number
      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
      ownr_fn
      ownr_ln
      ownr_ea
      ownr_addr1
      ownr_addr2
      ownr_city
      ownr_st
      ownr_zip
      ownr_ctry
      ownr_ph1

      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
      owner_owing
      date_exported
      joblines {
        id
        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
      }
    }
  }
`;

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 GET_JOB_TOMBSTONE = gql`
 query GET_JOB_TOMBSTONE($id: uuid!) {
  jobs_by_pk(id: $id) {
    updated_at
    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
    }
    loss_desc
    kmin
    kmout
    referral_source
    unit_number
    po_number
    special_coverage_policy
    scheduled_delivery
    converted
    ro_number
    clm_total
    inproduction
    vehicleid
    plate_no
    v_vin
    v_model_yr
    v_model_desc
    v_make_desc
    v_color
    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
    regie_number
    scheduled_completion
    id
    ded_amt
    ded_status
    depreciation_taxes
    production_vars
    other_amount_payable
    towing_payable
    storage_payable
    adjustment_bottom_line
    job_totals
    ownr_fn
    ownr_ln
    ownr_ea
    ownr_addr1
    ownr_addr2
    ownr_city
    ownr_st
    ownr_zip
    ownr_ctry
    ownr_ph1
    actual_in
    scheduled_completion
    scheduled_in
    actual_completion
    scheduled_delivery
    actual_delivery
    date_estimated
    date_open
    date_scheduled
    date_invoiced
    date_exported
    status
    owner_owing
    vehicle {
      id
      jobs {
        id
        ro_number
        status
      }
    }
  }
}

`;
export const GET_JOB_LINES = gql`
 query GET_JOB_LINES($id: uuid!) {
  jobs_by_pk(id: $id) {
    id
       joblines(where: { removed: { _eq: false } }, order_by: { line_no: asc }) {
        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
      }

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

      notes(order_by: { created_at: desc }) {
        id
        text
        critical
        private
        pinned
        type
        created_at
        updated_at
        created_by
      }

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

      documents(order_by: { takenat: desc }) {
        id
        name
        key
        created_at
        type
        extension
      }
  }
}
`;