exports.FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID = `
query FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID($mssid: String!, $phone: String!) {
    bodyshops(where: { messagingservicesid: { _eq: $mssid } }) {
      id
      conversations(where: { phone_num: { _eq: $phone } }) {
        id
        created_at
      }
    }
  }`;

// Unused
exports.GET_JOB_BY_RO_NUMBER = `
  query GET_JOB_BY_RO_NUMBER($ro_number: String!) {
    jobs(where:{ro_number:{_eq:$ro_number}}) {
      id
      bodyshop {
        id
      }
    }
  }`;

exports.INSERT_NEW_PAYMENT = `
  mutation INSERT_NEW_PAYMENT($paymentInput: [payments_insert_input!]!) {
    insert_payments(objects: $paymentInput) {
      returning {
        id
      }
    }
  }`;

exports.INSERT_PAYMENT_RESPONSE = `
  mutation INSERT_PAYMENT_RESPONSE($paymentResponse: [payment_response_insert_input!]!) {
    insert_payment_response(objects: $paymentResponse) {
      returning {
        id
      }
    }
  }`;

exports.UNARCHIVE_CONVERSATION = `
mutation UNARCHIVE_CONVERSATION($id: uuid!) {
  update_conversations_by_pk(pk_columns: {id: $id}, _set: {archived: false}) {
    id
  }
}`;

exports.INSERT_NEW_JOB_LINE = `
  mutation INSERT_NEW_JOB_LINE($lineInput: [joblines_insert_input!]!) {
    insert_joblines(objects: $lineInput) {
      returning {
        id
      }
    }
  }`;

exports.UPDATE_JOB_LINE = `
  mutation UPDATE_JOB_LINE($lineId: uuid!, $line: joblines_set_input!) {
    update_joblines(where: { id: { _eq: $lineId } }, _set: $line) {
      returning {
        id
        notes
        mod_lbr_ty
        part_qty
        db_price
        act_price
        line_desc
        line_no
        oem_partno
        notes
        location
        status
        removed
      }
    }
  }`;

exports.RECEIVE_MESSAGE = `
mutation RECEIVE_MESSAGE($msg: [messages_insert_input!]!) {
  insert_messages(objects: $msg) {
    returning {
      conversation {
        id
        archived
        bodyshop {
          imexshopid
        }
        created_at
        updated_at
        unreadcnt
        phone_num
        label
        job_conversations {
          job {
            id
            ro_number
            ownr_fn
            ownr_ln
            ownr_co_nm
          }
        }
        messages_aggregate (where: { read: { _eq: false }, isoutbound: { _eq: false } }){
          aggregate {
            count
          }
        }
      }
      conversationid
      created_at
      id
      image_path
      image
      isoutbound
      msid
      read
      text
      updated_at
      status
      userid
    }
  }
}`;

exports.INSERT_MESSAGE = `
mutation INSERT_MESSAGE($msg: [messages_insert_input!]!, $conversationid: uuid!) {
  update_conversations_by_pk(pk_columns: { id: $conversationid }, _set: { archived: false }) {
    id
    archived
  }
  insert_messages(objects: $msg) {
    returning {
      conversation {
        id
        archived
        bodyshop {
          id
          imexshopid
        }
        created_at
        updated_at
        unreadcnt
        phone_num
      }
      conversationid
      created_at
      id
      image_path
      image
      isoutbound
      is_system
      msid
      read
      text
      updated_at
      status
      userid
    }
  }
}`;

exports.UPDATE_MESSAGE_STATUS = `
mutation UPDATE_MESSAGE($msid: String!, $fields: messages_set_input!) {
    update_messages(where: { msid: { _eq: $msid } }, _set: $fields) {
      returning {
        id
        status
        conversationid
        conversation{
        bodyshopid
        }
      }
    }
  }`;

exports.QUERY_JOBS_FOR_RECEIVABLES_EXPORT = `
query QUERY_JOBS_FOR_RECEIVABLES_EXPORT($ids: [uuid!]!) {
  jobs(where: {id: {_in: $ids}}) {
    id
    job_totals
    ded_amt
    date_invoiced
    ro_number
    clm_total
    clm_no
    v_model_yr
    v_model_desc
    v_make_desc
    v_vin
    plate_no
    ownerid
    ownr_ln
    ownr_fn
    ownr_co_nm
    ownr_addr1
    ownr_addr2
    ownr_zip
    ownr_city
    ownr_st
    ownr_ea
    ins_co_nm
    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
    class
    shopid
    ca_bc_pvrt
    ca_customer_gst
    towing_payable
    storage_payable
    adjustment_bottom_line
    state_tax_rate
    qb_multiple_payers
    parts_tax_rates
    tax_paint_mat_rt
    tax_lbr_rt
    tax_shop_mat_rt
    tax_sub_rt
    tax_tow_rt
    tax_str_rt
    owner {
      accountingid
    }
    joblines(where:{removed: {_eq:false}}) {
      id
      line_desc
      part_type
      act_price
      mod_lb_hrs
      mod_lbr_ty
      part_qty
      op_code_desc
      profitcenter_labor
      profitcenter_part
      db_ref
      prt_dsmk_p
      prt_dsmk_m
      tax_part
      lbr_tax
      line_ref
      unq_seq
      lbr_op
    }
  }
  bodyshops(where: {associations: {active: {_eq: true}}}) {
    id
    md_responsibility_centers
    region_config
    accountingconfig
    md_ins_cos
    timezone
    md_ro_statuses
  }
}`;

exports.QUERY_JOBS_FOR_CDK_EXPORT = `
query QUERY_JOBS_FOR_CDK_EXPORT($id: uuid!) {
  jobs_by_pk(id: $id) {
    id
    job_totals
    date_invoiced
    ro_number
    clm_total
    clm_no
    invoice_allocation
    ownerid
    ownr_ln
    ownr_fn
    ownr_co_nm
    ownr_addr1
    ownr_addr2
    ownr_ph1
    ownr_ph2
    ownr_zip
    ownr_city
    ownr_ctry
    ownr_st
    ownr_ea
    ins_co_nm
    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
    class
    ca_bc_pvrt
    plate_no
    plate_st
    v_vin
    v_model_yr
    v_model_desc
    v_make_desc
    v_color
    ca_customer_gst
    bodyshop {
      id
      md_ro_statuses
      md_responsibility_centers
      accountingconfig
      cdk_dealerid
      cdk_configuration
      timezone
    }
    owner {
      accountingid
    }
    joblines(where:{removed: {_eq:false}}) {
      id
      line_desc
      part_type
      act_price
      mod_lb_hrs
      mod_lbr_ty
      part_qty
      op_code_desc
      profitcenter_labor
      profitcenter_part
      db_ref
      prt_dsmk_p
    }
  }
}`;

exports.QUERY_JOBS_FOR_PBS_EXPORT = `
query QUERY_JOBS_FOR_PBS_EXPORT($id: uuid!) {
  jobs_by_pk(id: $id) {
    id
    job_totals
    date_invoiced
    ro_number
    clm_total
    clm_no
    dms_allocation
    invoice_allocation
    kmin
    kmout
    ownerid
    ownr_ln
    ownr_fn
    ownr_co_nm
    ownr_addr1
    ownr_addr2
    ownr_ph1
    ownr_ph2
    ownr_zip
    ownr_city
    ownr_ctry
    ownr_st
    ownr_ea
    ins_co_nm
    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
    class
    ca_bc_pvrt
    plate_no
    plate_st
    v_vin
    v_model_yr
    v_model_desc
    v_make_desc
    v_color
    ca_customer_gst
    scheduled_completion
    actual_completion
    vehicle {
      v_trimcode
      v_makecode
    }
    bodyshop {
      id
      md_ro_statuses
      md_responsibility_centers
      accountingconfig
      pbs_serialnumber
      pbs_configuration
      timezone
    }
    owner {
      id
      accountingid
    }
    joblines(where:{removed: {_eq:false}}) {
      id
      line_desc
      part_type
      act_price
      mod_lb_hrs
      mod_lbr_ty
      part_qty
      op_code_desc
      profitcenter_labor
      profitcenter_part
      db_ref
      prt_dsmk_p
      unq_seq
      line_ref
    }
  }
}`;

exports.QUERY_BILLS_FOR_PAYABLES_EXPORT = `
query QUERY_BILLS_FOR_PAYABLES_EXPORT($bills: [uuid!]!) {
  bodyshops(where: {associations: {active: {_eq: true}}}) {
    id
    md_responsibility_centers
    timezone
    region_config
    accountingconfig
  }
  bills(where: {id: {_in: $bills}}) {
    id
    date
    due_date
    federal_tax_rate
    invoice_number
    is_credit_memo
    invoice_number
    job {
      id
      ro_number
      clm_no
      ownr_fn
      ownr_ln
      ownr_co_nm
      class
      shopid
    }
    billlines{
      id
      cost_center
      actual_cost
      quantity
      applicable_taxes
    }
    state_tax_rate
    local_tax_rate
    total
    vendor{
      id
      name
      due_date
    }
  }
}`;

exports.QUERY_PAYMENTS_FOR_EXPORT = `
  query QUERY_PAYMENTS_FOR_EXPORT($payments: [uuid!]!) {
    bodyshops(where: {associations: {active: {_eq: true}}}) {
      id
      md_responsibility_centers
      accountingconfig
      timezone
      md_ins_cos
    }
    payments(where: {id: {_in: $payments}}) {
      id
      created_at
      jobid
      job {
        id
        ro_number
        ownerid
        ownr_ln
        ownr_fn
        ownr_addr1
        ownr_addr2
        ownr_zip
        ownr_city
        ownr_st
        ins_co_nm
        owner{
          accountingid
        }
        ownr_fn
        ownr_ln
        ownr_co_nm
        shopid
        bodyshop {
          accountingconfig
          md_responsibility_centers
          md_ins_cos
        }
      }
      transactionid
      memo
      amount
      stripeid
      exportedat
      stripeid
      type
      payer
      paymentnum
      date
    }
  }`;

exports.QUERY_TRANSITIONS_BY_JOBID = `query QUERY_TRANSITIONS_BY_JOBID($jobids: [uuid!]!) {
  transitions(where: {jobid: {_in: $jobids}}, order_by: {end: desc}) {
    start
    end
    value
    prev_value
    next_value
    duration
    type
    created_at
    updated_at
    jobid
  }
}`;

exports.QUERY_UPCOMING_APPOINTMENTS = `query QUERY_UPCOMING_APPOINTMENTS($now: timestamptz!, $jobId: uuid!) {
  jobs_by_pk(id: $jobId) {
    bodyshop {
      ssbuckets
      ss_configuration
      target_touchtime
      workingdays
      timezone
    }
    jobhrs: joblines_aggregate(where: {removed: {_eq: false}}) {
      aggregate {
        sum {
          mod_lb_hrs
        }
      }
    }
  }
  blockedDays: appointments(where: {_and: {canceled: {_eq: false}, block: {_eq: true}, start: {_gte: $now}}}) {
    start
    block
  }
  arrJobs: jobs(where: {scheduled_in: {_gte: $now}, suspended: {_eq: false}}) {
    id
    scheduled_in
    actual_in
    scheduled_completion
    ro_number
    ownr_fn
    ownr_ln
    ownr_co_nm
    alt_transport
    actual_completion
    inproduction
    status
    labhrs: joblines_aggregate(where: {mod_lbr_ty: {_neq: "LAR"}, removed: {_eq: false}}) {
      aggregate {
        sum {
          mod_lb_hrs
        }
      }
    }
    larhrs: joblines_aggregate(where: {mod_lbr_ty: {_eq: "LAR"}, removed: {_eq: false}}) {
      aggregate {
        sum {
          mod_lb_hrs
        }
      }
    }
  }
  compJobs: jobs(where: {_and: [{suspended: {_eq: false}}, {_or: [{scheduled_completion: {_gte: $now}}, {actual_completion: {_gte: $now}}]}]}) {
    id
    status
    ro_number
    scheduled_completion
    actual_completion
    scheduled_in
    ownr_fn
    ownr_ln
    ownr_co_nm
    inproduction
    labhrs: joblines_aggregate(where: {mod_lbr_ty: {_neq: "LAR"}, removed: {_eq: false}}) {
      aggregate {
        sum {
          mod_lb_hrs
        }
      }
    }
    larhrs: joblines_aggregate(where: {mod_lbr_ty: {_eq: "LAR"}, removed: {_eq: false}}) {
      aggregate {
        sum {
          mod_lb_hrs
        }
      }
    }
  }
  prodJobs: jobs(where: {inproduction: {_eq: true}, suspended: {_eq: false}}) {
    id
    actual_in
    scheduled_in
    actual_completion
    scheduled_completion
    inproduction
    ro_number
    labhrs: joblines_aggregate(where: {mod_lbr_ty: {_neq: "LAR"}, removed: {_eq: false}}) {
      aggregate {
        sum {
          mod_lb_hrs
        }
      }
    }
    larhrs: joblines_aggregate(where: {mod_lbr_ty: {_eq: "LAR"}, removed: {_eq: false}}) {
      aggregate {
        sum {
          mod_lb_hrs
        }
      }
    }
  }
}`;

exports.QUERY_EMPLOYEE_PIN = `query QUERY_EMPLOYEE_PIN($shopId: uuid!, $employeeId: String!) {
  employees(where: {_and: {shopid: {_eq: $shopId}, employee_number: {_eq: $employeeId}}}) {
    last_name
    first_name
    employee_number
    id
    pin
    active
  }
}`;

exports.AUTOHOUSE_QUERY = `query AUTOHOUSE_EXPORT($start: timestamptz, $bodyshopid: uuid!, $end: timestamptz) {
  bodyshops_by_pk(id: $bodyshopid){
      id
      shopname
      address1
      city
      state
      zip_post
      country
      phone
      md_ro_statuses
      md_order_statuses
      autohouseid
      md_responsibility_centers
      jc_hourly_rates
      cdk_dealerid
      pbs_serialnumber
      use_paint_scale_data
    timezone
  }
  jobs(where: {_and: [{converted: {_eq: true}}, {updated_at: {_gt: $start}}, {updated_at: {_lte: $end}}, {shopid: {_eq: $bodyshopid}}]}) {
    id
    created_at
    ro_number
    status
    est_ct_fn
    est_ct_ln
    ownr_fn
    ownr_ln
    ownr_co_nm
    ownr_addr1
    ownr_city
    ownr_st
    ownr_ph1
    ownr_zip
    loss_type
    v_model_yr
    v_model_desc
    v_make_desc
    v_vin
    plate_no
    plate_st
    kmin
    v_color
    tlos_ind
    ins_co_nm
    ins_addr1
    ins_city
    ins_st
    ins_zip
    ins_ph1
    loss_type
    policy_no
    clm_no
    loss_date
    asgn_date
    date_estimated
    date_open
    scheduled_in
    actual_in
    scheduled_completion
    actual_completion
    scheduled_delivery
    actual_delivery
    date_invoiced
    date_exported
    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_matd
    rate_mapa
    rate_mash
    job_totals
    driveable
    parts_tax_rates
    ded_amt
    date_repairstarted
    joblines(where: {removed: {_eq: false}}) {
      id
      line_no
      line_ind
      status
      line_ind
      db_price
      act_price
      mod_lb_hrs
      mod_lbr_ty
      line_desc
      prt_dsmk_m
      prt_dsmk_p
      part_qty
      part_type
      oem_partno
      lbr_op
      profitcenter_part
      profitcenter_labor
      ah_detail_line
      parts_order_lines(order_by: {parts_order: {order_date: desc_nulls_last}} limit: 1){
        parts_order{
          id
          order_date
        }
      }
      billlines(order_by: {bill: {date: desc_nulls_last}} limit: 1) {
        actual_cost
        actual_price
        quantity
        bill {
          vendor {
            name
          }
          invoice_number
          date
        }
      }
    }
    bills {
      id
      federal_tax_rate
      local_tax_rate
      state_tax_rate
      is_credit_memo
      billlines {
        actual_cost
        cost_center
        id
        quantity
      }
    }
    timetickets(where: {cost_center: {_neq: "timetickets.labels.shift"}}) {
      id
      rate
      ciecacode
      cost_center
      actualhrs
      productivehrs
      flat_rate
      employeeid
      employee {
        employee_number
        flat_rate
        first_name
        last_name
      }
    }
    area_of_damage
    employee_prep_rel {
      first_name
      last_name
      employee_number
      id
    }
    employee_refinish_rel {
      first_name
      last_name
      employee_number
      id
    }
    employee_body_rel {
      first_name
      last_name
      employee_number
      id
    }
    mixdata(limit: 1, order_by: {updated_at: desc}) {
      jobid
      totalliquidcost
    }
  }
}`;

exports.CHATTER_QUERY = `query CHATTER_EXPORT($start: timestamptz, $bodyshopid: uuid!, $end: timestamptz) {
  bodyshops_by_pk(id: $bodyshopid){
    id
    shopname 
    chatterid
    timezone
  }
  jobs(where: {_and: [{converted: {_eq: true}}, {actual_delivery: {_gt: $start}}, {actual_delivery: {_lte: $end}}, {shopid: {_eq: $bodyshopid}}, {_or: [{ownr_ph1: {_is_null: false}}, {ownr_ea: {_is_null: false}}]}]}) {
    actual_delivery
    id
    created_at
    ro_number
    ownr_fn
    ownr_ln
    ownr_co_nm
    ownr_ph1
    ownr_ea
  }
}`;

exports.CARFAX_QUERY = `query CARFAX_EXPORT($start: timestamptz, $bodyshopid: uuid!, $end: timestamptz) {
  bodyshops_by_pk(id: $bodyshopid) {
    id
    shopname
    imexshopid
    timezone
  }
  jobs(where: {_and: [{_or: [{date_invoiced: {_gt: $start, _lte: $end}}, {actual_delivery: {_gt: $start, _lte: $end}, date_invoiced: {_is_null: true}}, {actual_completion: {_gt: $start, _lte: $end}, actual_delivery: {_is_null: true}, date_invoiced: {_is_null: true}}]}, {_not: {_and: [{date_invoiced: {_is_null: true}}, {actual_delivery: {_is_null: true}}, {actual_completion: {_is_null: true}}]}}, {shopid: {_eq: $bodyshopid}}, {voided: {_neq: true}}, {v_vin: {_is_null: false}}, {ro_number: {_is_null: false}}]}) {
    actual_completion  
    actual_delivery
    area_of_damage
    created_at
    date_estimated
    date_invoiced
    date_open
    id
    ins_co_nm
    job_totals
    joblines(where: {removed: {_eq: false}}) {
      act_price
      alt_partno
      line_desc
      mod_lb_hrs
      mod_lbr_ty
      lbr_op
      oem_partno
      op_code_desc
      part_type
      part_qty
    }
    loss_date
    loss_desc
    ro_number
    theft_ind
    tlos_ind
    v_make_desc
    v_model_desc
    v_model_yr
    v_vin
  }
}`;

exports.CARFAX_RPS_QUERY = `query CARFAX_RPS_EXPORT($starttz: timestamptz, $endtz: timestamptz,$start: date, $end: date, $bodyshopid: uuid!) {
  bodyshops_by_pk(id: $bodyshopid) {
    id
    shopname
  }
  jobs(where: {_and: [{_or: [{close_date: {_gt: $start, _lte: $end}}, {created_at: {_gt: $starttz, _lte: $endtz}, close_date: {_is_null: true}}]}, {_not: {_and: [{close_date: {_is_null: true}}, {created_at: {_is_null: true}}]}}, {bodyshopid: {_eq: $bodyshopid}}, {v_vin: {_is_null: false}}]}) {
    close_date
    created_at
    id
    ins_co_nm
    impact_1
    impact_2
    joblines {
      act_price
      alt_partno
      line_desc
      mod_lb_hrs
      mod_lbr_ty
      oem_partno
      lbr_op
      part_type
      part_qty
    }
    loss_date
    loss_desc
    theft_ind
    tlos_ind
    totals
    v_makedesc
    v_model
    v_model_yr
    v_vin
  }
}`;

exports.CLAIMSCORP_QUERY = `query CLAIMSCORP_EXPORT($start: timestamptz, $bodyshopid: uuid!, $end: timestamptz) {
  bodyshops_by_pk(id: $bodyshopid){
    id
    shopname
    address1
    city
    state
    zip_post
    country
    phone
    md_ro_statuses
    md_order_statuses
    claimscorpid
    md_responsibility_centers
    jc_hourly_rates
    cdk_dealerid
    pbs_serialnumber
    rr_dealerid
    use_paint_scale_data
    timezone
  }
  jobs(where: {_and: [{converted: {_eq: true}}, {updated_at: {_gt: $start}}, {updated_at: {_lte: $end}}, {shopid: {_eq: $bodyshopid}}]}) {
    id
    created_at
    ro_number
    status
    est_ct_fn
    est_ct_ln
    ownr_st
    ownr_zip
    tlos_ind
    v_color
    v_model_yr
    v_model_desc
    v_make_desc
    v_vin
    vehicle {
      v_bstyle
    }
    ins_co_nm
    clm_no
    loss_date
    asgn_date
    date_estimated
    date_open
    scheduled_in
    actual_in
    scheduled_completion
    actual_completion
    scheduled_delivery
    actual_delivery
    date_invoiced
    date_exported
    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_matd
    rate_mapa
    rate_mash
    job_totals
    parts_tax_rates
    date_repairstarted
    joblines(where: {removed: {_eq: false}}) {
      id
      line_no
      line_ind
      status
      line_ind
      db_price
      act_price
      mod_lb_hrs
      mod_lbr_ty
      line_desc
      prt_dsmk_m
      prt_dsmk_p
      part_qty
      part_type
      oem_partno
      lbr_op
      profitcenter_part
      profitcenter_labor
      ah_detail_line
      parts_order_lines(order_by: {parts_order: {order_date: desc_nulls_last}} limit: 1){
        parts_order{
          id
          order_date
        }
      }
      billlines(order_by: {bill: {date: desc_nulls_last}} limit: 1) {
        actual_cost
        actual_price
        quantity
        bill {
          vendor {
            name
          }
          invoice_number
          date
        }
      }
    }
    bills {
      id
      federal_tax_rate
      local_tax_rate
      state_tax_rate
      is_credit_memo
      billlines {
        actual_cost
        cost_center
        id
        quantity
      }
    }
    employee_body_rel {
      first_name
      last_name
      employee_number
      id
    }
    employee_csr_rel {
      first_name
      last_name
      employee_number
      id
    }
    employee_prep_rel {
      first_name
      last_name
      employee_number
      id
    }
    employee_refinish_rel {
      first_name
      last_name
      employee_number
      id
    }
    parts_orders(limit: 1, order_by: {created_at: desc}) {
      created_at
    }
    timetickets {
      id
      rate
      cost_center
      actualhrs
      productivehrs
      flat_rate
    }
    mixdata(limit: 1, order_by: {updated_at: desc}) {
      jobid
      totalliquidcost
    }
  }
}`;

exports.ENTEGRAL_EXPORT = `
query ENTEGRAL_EXPORT($bodyshopid: uuid!) {
  jobs(where: {_and: [{converted: {_eq: true}}, {shopid: {_eq: $bodyshopid}}]}) {
    joblines {
      id
      line_ind
    }
    id
    ro_number
    status
    asgn_date
    date_open
    kmin
    scheduled_completion
    actual_completion
    actual_delivery
    date_exported
    ins_co_nm
    ins_addr1
    ins_addr2
    ins_city
    ins_st
    ins_zip
    ins_ctry
    ins_ph1
    ins_ph2
    est_ct_ln
    est_ct_fn
    insd_fn
    insd_ln
    ownr_fn
    ownr_ln
    ownr_co_nm
    ownr_addr1
    ownr_addr2
    ownr_city
    ownr_st
    ownr_zip
    ownr_ctry
    ownr_ph1
    ownr_ph2
    ownr_ea
    clm_ct_fn
    clm_ct_ln
    v_vin
    plate_no
    v_model_yr
    v_make_desc
    v_model_desc
    v_color
    driveable
    clm_no
    policy_no
    loss_date
    area_of_damage
    tlos_ind
    parts_tax_rates
    federal_tax_rate
    state_tax_rate
    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
    job_totals
    ded_amt
    cieca_ttl
    adjustment_bottom_line
  }
}`;

exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uuid!, $end: timestamptz) {
  bodyshops_by_pk(id: $bodyshopid) {
    id
    shopname
    address1
    city
    state
    zip_post
    country
    phone
    last_name_first
    md_ro_statuses
    md_order_statuses
    md_responsibility_centers
    jc_hourly_rates
    cdk_dealerid
    pbs_serialnumber
    rr_dealerid
    use_paint_scale_data
    timezone
  }
  jobs(where: {_and: [{updated_at: {_gt: $start}}, {updated_at: {_lte: $end}}, {converted: {_eq: true}}, {shopid: {_eq: $bodyshopid}}]}) {
    actual_completion
    actual_delivery
    actual_in
    asgn_date
    bills {
      billlines {
        actual_cost
        actual_price
        cost_center
        deductedfromlbr
        id
        line_desc
        quantity
      }
      date
      federal_tax_rate
      id
      is_credit_memo
      invoice_number
      isinhouse
      local_tax_rate
      state_tax_rate
      vendor {
        name
      }
    }
    created_at
    clm_no
    date_estimated
    date_exported
    date_invoiced
    date_open
    date_repairstarted
    date_void
    dms_allocation
    ded_amt
    employee_body_rel {
      first_name
      last_name
      employee_number
      id
    }
    employee_csr_rel {
      first_name
      last_name
      employee_number
      id
    }
    employee_prep_rel {
      first_name
      last_name
      employee_number
      id
    }
    employee_refinish_rel {
      first_name
      last_name
      employee_number
      id
    }
    est_ct_fn
    est_ct_ln
    id
    ins_co_nm
    joblines(where: {removed: {_eq: false}}) {
      act_price
      alt_partno
      billlines(order_by: {bill: {date: desc_nulls_last}}, limit: 1) {
        actual_cost
        actual_price
        quantity
        bill {
          vendor {
            name
          }
          invoice_number
          date
        }
      }
      db_price
      id
      lbr_amt
      lbr_op
      line_desc
      line_ind
      line_no
      mod_lb_hrs
      mod_lbr_ty
      oem_partno
      op_code_desc
      parts_order_lines(order_by: {parts_order: {order_date: desc_nulls_last}}, limit: 1) {
        parts_order {
          id
          order_date
        }
      }
      part_qty
      part_type
      profitcenter_part
      profitcenter_labor
      prt_dsmk_m
      prt_dsmk_p
      status
    }
    job_totals
    loss_date
    mixdata(limit: 1, order_by: {updated_at: desc}) {
      jobid
      totalliquidcost
    }
    notes {
      created_at
      created_by
      critical
      private
      text
      type
    }
    ownr_addr1
    ownr_addr2
    ownr_city
    ownr_co_nm
    ownr_fn
    ownr_ln
    ownr_st
    ownr_zip
    parts_orders(limit: 1, order_by: {created_at: desc}) {
      created_at
    }
    parts_tax_rates
    plate_no
    policy_no
    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_matd
    rate_mapa
    rate_mash
    ro_number
    scheduled_completion
    scheduled_delivery
    scheduled_in
    status
    timetickets {
      actualhrs
      cost_center
      date
      employee {
        employee_number
        first_name
        last_name
      }
      flat_rate
      id
      productivehrs
      rate
    }
    tlos_ind
    v_color
    v_model_yr
    v_model_desc
    v_make_desc
    v_vin
    vehicle {
      v_bstyle
    }
  }
}`;

exports.PODIUM_QUERY = `query PODIUM_EXPORT($start: timestamptz, $bodyshopid: uuid!, $end: timestamptz) {
  bodyshops_by_pk(id: $bodyshopid){
    id
    shopname 
    podiumid
    timezone
  }
  jobs(where: {_and: [{converted: {_eq: true}}, {actual_delivery: {_gt: $start}}, {actual_delivery: {_lte: $end}}, {shopid: {_eq: $bodyshopid}}, {_or: [{ownr_ph1: {_is_null: false}}, {ownr_ea: {_is_null: false}}]}]}) {
    actual_delivery
    id
    created_at
    ro_number
    ownr_fn
    ownr_ln
    ownr_co_nm
    ownr_ph1
    ownr_ph2
    ownr_ea
  }
}`;

exports.UPDATE_JOB = `
  mutation UPDATE_JOB($jobId: uuid!, $job: jobs_set_input!) {
    update_jobs(where: { id: { _eq: $jobId } }, _set: $job) {
      returning {
        id
        date_exported
        status
        alt_transport
        ro_number
        production_vars
        lbr_adjustments
      }
    }
  }`;

exports.GET_JOB_BY_PK = `query GET_JOB_BY_PK($id: uuid!) {
    jobs_by_pk(id: $id) {
      cieca_stl
      updated_at
      alt_transport
      intakechecklist
      loss_desc
      kmin
      kmout
      comment
      referral_source
      referral_source_extra
      unit_number
      po_number
      special_coverage_policy
      scheduled_delivery
      converted
      lbr_adjustments
      ro_number
      clm_total
      inproduction
      vehicleid
      plate_no
      v_vin
      v_model_yr
      v_model_desc
      v_make_desc
      v_color
      vehicleid
      driveable
      towin
      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
      shopid
      est_ct_ln
      ciecaid
      cieca_pfl
      cieca_pft
      cieca_pfo
      cieca_ttl
      vehicle {
        id
        notes
      }
      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
      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_shop_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
      ownr_ph2
      production_vars
      ca_gst_registrant
      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
      federal_tax_rate
      local_tax_rate
      state_tax_rate
      scheduled_completion
      scheduled_in
      actual_completion
      scheduled_delivery
      actual_delivery
      date_estimated
      date_open
      date_scheduled
      date_invoiced
      date_exported
      date_repairstarted
      status
      owner_owing
      tax_registration_number
      class
      category
      deliverchecklist
      voided
      ca_bc_pvrt
      ca_customer_gst
      materials
      auto_add_ats
      rate_ats
      flat_rate_ats
      rate_ats_flat
      dms_id
      dms_customer_id
      dms_advisor_id
      joblines(where: { removed: { _eq: false } }){
        id
        line_no
        unq_seq
        line_ind
        line_desc
        part_type
        line_ref
        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
        prt_dsmk_p
        prt_dsmk_m
        misc_amt
        misc_tax
      }
    }
  }`;
//TODO:AIO The above query used to have parts order lines in it. Validate that this doesn't need it.

exports.QUERY_JOB_COSTING_DETAILS = `
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
      dms_allocation
      cieca_pfl
      cieca_stl
      materials
      joblines(where: { removed: { _eq: false } }) {
        id
        db_ref
        line_ref
        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
        profitcenter_part
        profitcenter_labor
        act_price_before_ppc
        manual_line
      }
      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
        ciecacode
      }
      mixdata(limit: 1, order_by: {updated_at: desc}) {
        jobid
        totalliquidcost
      }
      bodyshop{
        id
        md_responsibility_centers
        jc_hourly_rates
        cdk_dealerid
        pbs_serialnumber
        rr_dealerid
        use_paint_scale_data
      }
    }
  }`;

exports.QUERY_JOB_COSTING_DETAILS_MULTI = ` query QUERY_JOB_COSTING_DETAILS_MULTI($ids: [uuid!]!) {
  jobs( where: {id: {_in: $ids}}) {
    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
    dms_allocation
    cieca_pfl
    cieca_stl
    materials
    joblines(where: {removed: {_eq: false}}) {
      id
      db_ref
      unq_seq
      line_ref
      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
      profitcenter_part
      profitcenter_labor
      manual_line
    }
    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
      ciecacode
    }
    mixdata(limit: 1, order_by: {updated_at: desc}) {
      jobid
      totalliquidcost
    }
    bodyshop {
      id
      md_responsibility_centers
      jc_hourly_rates
      cdk_dealerid
      pbs_serialnumber
      rr_dealerid
      use_paint_scale_data
    }
  }
}`;

// Exists in Commented out Query
exports.INSERT_IOEVENT = ` mutation INSERT_IOEVENT($event: ioevents_insert_input!) {
  insert_ioevents_one(object: $event) {
    id
  }
}`;

exports.GET_AUTOHOUSE_SHOPS = `query GET_AUTOHOUSE_SHOPS {
  bodyshops(where: {autohouseid: {_is_null: false}, _or: {autohouseid: {_neq: ""}}}){
      id
      shopname
      address1
      city
      state
      zip_post
      country
      phone
      md_ro_statuses
      md_order_statuses
      autohouseid
      md_responsibility_centers
      jc_hourly_rates
      imexshopid
      timezone
  }
}`;

exports.GET_CHATTER_SHOPS = `query GET_CHATTER_SHOPS {
  bodyshops(
    where: {
      chatterid: { _is_null: false, _neq: "" }
      _or: [
        { chatter_company_id: { _is_null: true } }
        { chatter_company_id: { _eq: "" } }
      ]
    }
  ) {
    id
    shopname
    chatterid
    chatter_company_id
    imexshopid
    timezone
  }
}`;

exports.GET_CHATTER_SHOPS_WITH_COMPANY = `query GET_CHATTER_SHOPS_WITH_COMPANY {
  bodyshops(
    where: {
      chatterid: { _is_null: false, _neq: "" }
      chatter_company_id: { _is_null: false, _neq: "" }
    }
  ) {
    id
    shopname
    chatterid
    chatter_company_id
    imexshopid
    timezone
  }
}`;

exports.GET_CARFAX_SHOPS = `query GET_CARFAX_SHOPS {
  bodyshops(where: {external_shop_id: {_is_null: true}, carfax_exclude: {_neq: "true"}}){
      id
      shopname
      imexshopid
  }
}`;

exports.GET_CARFAX_RPS_SHOPS = `query GET_CARFAX_RPS_SHOPS {
  bodyshops(where: {carfax_exclude: {_neq: "true"}}){
      id
      shopname
  }
}`;

exports.GET_CLAIMSCORP_SHOPS = `query GET_CLAIMSCORP_SHOPS {
  bodyshops(where: {claimscorpid: {_is_null: false}, _or: {claimscorpid: {_neq: ""}}}){
      id
      shopname
      address1
      city
      state
      zip_post
      country
      phone
      md_ro_statuses
      md_order_statuses
      claimscorpid
      md_responsibility_centers
      jc_hourly_rates
      imexshopid
      timezone
  }
}`;

exports.GET_ENTEGRAL_SHOPS = `query GET_ENTEGRAL_SHOPS {
  bodyshops(where: {entegral_id: {_is_null: false}, _or: {entegral_id: {_neq: ""}}}){
      id
      shopname
      address1
      city
      state
      zip_post
      country
      phone
      md_ro_statuses
      md_order_statuses
      entegral_id
      md_responsibility_centers
      imexshopid
      timezone
      features
  }
}`;

exports.GET_KAIZEN_SHOPS = `query GET_KAIZEN_SHOPS($imexshopid: [String]) {
  bodyshops(where: {imexshopid: {_in: $imexshopid}}){
      id
      shopname
      address1
      city
      state
      zip_post
      country
      phone
      md_ro_statuses
      md_order_statuses
      autohouseid
      md_responsibility_centers
      jc_hourly_rates
      imexshopid
      timezone
  }
}`;

exports.GET_PODIUM_SHOPS = `query GET_PODIUM_SHOPS {
  bodyshops(where: {podiumid: {_is_null: false}, _or: {podiumid: {_neq: ""}}}){
    id
    shopname
    podiumid
    imexshopid
    timezone
  }
}`;

exports.DELETE_ALL_DMS_VEHICLES = `mutation DELETE_ALL_DMS_VEHICLES{
  delete_dms_vehicles(where: {}) {
    affected_rows
  }
}`;

exports.INSERT_DMS_VEHICLES = `mutation INSERT_DMS_VEHICLES($vehicles: [dms_vehicles_insert_input!]!) {
  insert_dms_vehicles(objects: $vehicles) {
affected_rows
  }
}`;

exports.GET_CDK_ALLOCATIONS = `query QUERY_JOB_CLOSE_DETAILS($id: uuid!) {
  jobs_by_pk(id: $id) {
    bodyshop {
      id
      md_responsibility_centers
      cdk_configuration
      pbs_configuration
      use_paint_scale_data
    }
    ro_number
    dms_allocation
    invoice_allocation
    ins_co_id
    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
    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
    actual_in
    ca_bc_pvrt
    cieca_pfl
    materials
    timetickets {
      id
      actualhrs
      cost_center
      productivehrs
      rate
      ciecacode
      employee {
        flat_rate
      }
    }
    bills(where: {isinhouse: {_eq: false}}) {
      id
      federal_tax_rate
      local_tax_rate
      state_tax_rate
      is_credit_memo
      billlines {
        actual_cost
        cost_center
        id
        quantity
        applicable_taxes
      }
    }
    joblines(where: {removed: {_eq: false}}) {
      id
      removed
      tax_part
      line_desc
      prt_dsmk_p
      prt_dsmk_m
      part_type
      oem_partno
      db_price
      db_ref
      act_price
      part_qty
      mod_lbr_ty
      db_hrs
      mod_lb_hrs
      lbr_op
      lbr_amt
      op_code_desc
      profitcenter_labor
      profitcenter_part
      line_ref
      unq_seq
    }
    mixdata(limit: 1, order_by: {updated_at: desc}) {
      jobid
      totalliquidcost
    }
  }
}`;

exports.GET_QBO_AUTH = `query GET_QBO_AUTH($email: String!) {
  associations(where: {_and: {active: {_eq: true}, useremail: {_eq: $email}}}){
    id
    qbo_auth
    qbo_realmId
  }
}`;

exports.SET_QBO_AUTH_WITH_REALM = `mutation SET_QBO_AUTH($email: String!, $qbo_auth: jsonb!, $qbo_realmId: String) {
  update_associations(_set: {qbo_auth: $qbo_auth, qbo_realmId: $qbo_realmId}, where: {_and: {active: {_eq: true}, useremail: {_eq: $email}}}){
    affected_rows
  }
}`;

exports.SET_QBO_AUTH = `mutation SET_QBO_AUTH($email: String!, $qbo_auth: jsonb!) {
  update_associations(_set: {qbo_auth: $qbo_auth}, where: {_and: {active: {_eq: true}, useremail: {_eq: $email}}}){
    affected_rows
  }
}`;

exports.MARK_JOB_EXPORTED = `
mutation MARK_JOB_EXPORTED($jobId: uuid!, $job: jobs_set_input!, $log: exportlog_insert_input!, $bill: bills_set_input!) {
  update_jobs(where: {id: {_eq: $jobId}}, _set: $job) {
    returning {
      id
      date_exported
      status
      alt_transport
      ro_number
      production_vars
      lbr_adjustments
    }
  }
  insert_exportlog_one(object: $log) {
    id
  }
  update_bills(where:{jobid:{_eq :$jobId}}, _set:$bill){
    affected_rows
  }
}`;

exports.MARK_BILLS_EXPORTED = `
mutation UPDATE_BILLS($billids: [uuid!]!, $bill: bills_set_input!, $logs: [exportlog_insert_input!]!) {
  update_bills(where: {id: {_in: $billids}}, _set: $bill) {
    returning {
      id
      exported
      exported_at
    }
  }
  insert_exportlog(objects: $logs) {
    returning{
      id
    }
  }
}`;

exports.UPDATE_OLD_TRANSITION = `mutation UPDATE_OLD_TRANSITION($jobid: uuid!, $existingTransition: transitions_set_input!){
  update_transitions(where:{jobid:{_eq:$jobid}, end:{_is_null:true
  }}, _set:$existingTransition){
    affected_rows
    returning{
      id
      start
      end
      prev_value
      next_value
      value
    }
  }
}`;

exports.INSERT_NEW_TRANSITION = (
  includeOldTransition
) => `mutation INSERT_NEW_TRANSITION($newTransition: transitions_insert_input!, ${
  includeOldTransition ? `$oldTransitionId: uuid!, $duration: numeric` : ""
}) {
  insert_transitions_one(object: $newTransition) {
    id
  }
  ${
    includeOldTransition
      ? `update_transitions(where: {id: {_eq: $oldTransitionId}}, _set: {duration: $duration}) {
    affected_rows
  }`
      : ""
  }
}`;

exports.QUERY_JOB_ID_MIXDATA = `query QUERY_JOB_ID_MIXDATA($roNumbers: [String!]!) {
  jobs(where: {ro_number: {_in: $roNumbers}}) {
    id
    ro_number
    mixdata {
      id
    }
  }
}`;

exports.QBO_MARK_JOB_EXPORTED = `
mutation QBO_MARK_JOB_EXPORTED($jobId: uuid!, $job: jobs_set_input!, $logs: [exportlog_insert_input!]!) {
  insert_exportlog(objects: $logs) {
    affected_rows
  }
  update_jobs(where: {id: {_eq: $jobId}}, _set: $job) {
    returning {
      id

    }
  }
}`;

exports.QBO_MARK_BILL_EXPORTED = `
mutation QBO_MARK_BILL_EXPORTED($billId: uuid!, $bill: bills_set_input!, $logs: [exportlog_insert_input!]!) {
  insert_exportlog(objects: $logs) {
    affected_rows
  }
    update_bills(where: { id: { _eq: $billId } }, _set: $bill) {
      returning {
        id
      }
    }
}`;

exports.QBO_MARK_PAYMENT_EXPORTED = `
mutation QBO_MARK_PAYMENT_EXPORTED($paymentId: uuid!, $payment: payments_set_input!, $logs: [exportlog_insert_input!]!) {
  insert_exportlog(objects: $logs) {
    affected_rows
  }
  update_payments(where: {id: {_eq: $paymentId}}, _set: $payment) {
    returning {
      id
    }
  }
}`;

exports.INSERT_EXPORT_LOG = `
mutation INSERT_EXPORT_LOG($logs: [exportlog_insert_input!]!) {
  insert_exportlog(objects: $logs) {
    affected_rows
  }
}`;

exports.INSERT_EMAIL_AUDIT = `mutation INSERT_EMAIL_AUDIT($email: email_audit_trail_insert_input!) {
  insert_email_audit_trail_one(object: $email) {
    id
  }
}`;

exports.DELETE_MEDIA_DOCUMENTS = `
mutation DELETE_DOCUMENTS($ids: [uuid!]!) {
  delete_documents(where: { id: { _in: $ids } }) {
    returning {
      id
    }
  }
}`;

exports.UPDATE_EMAIL_AUDIT = `
mutation ($sesid: String!, $status: String, $context: jsonb) {
  update_email_audit_trail(where: {sesmessageid: {_eq: $sesid}}, _set: {status: $status, status_context: $context}) {
    returning {
      contents
    }
  }
}`;

exports.GET_PBS_AP_ALLOCATIONS = `
query GET_PBS_AP_ALLOCATIONS($billids: [uuid!]) {
  bodyshops(where: {associations: {active: {_eq: true}}}) {
    md_responsibility_centers
    timezone
    pbs_serialnumber
    pbs_configuration
    id
  }
  bills(where: {id: {_in: $billids}, exported:{_eq: false}}) {
    id
    date
    isinhouse
    invoice_number
    federal_tax_rate
    is_credit_memo
    jobid
    job {
      id
      ro_number
    }
    local_tax_rate
    state_tax_rate
    total
    vendorid
    vendor {
      id
      name
      dmsid
    }
    billlines {
      id
      actual_cost
      actual_price
      applicable_taxes
      cost_center
      deductedfromlbr
      lbr_adjustment
      quantity
    }
  }
}`;

exports.GET_JOB_FOR_PPC = `query GET_JOB_FOR_PPC($jobid: uuid!) {
  jobs_by_pk(id: $jobid) {
    id
    ciecaid
    ro_number
    joblines(where: {removed: {_eq: false}, act_price_before_ppc: {_is_null: false}}) {
      id
      act_price
      unq_seq
    }
    bodyshop {
      timezone
    }
  }
}

  `;

exports.QUERY_PARTS_SCAN = `query QUERY_PARTS_SCAN ($id: uuid!) {
  jobs_by_pk(id: $id) {
    bodyshop {
      id
      md_parts_scan
    }
    joblines(where: {removed: {_eq: false}}) {
      id
      line_desc
      critical
      part_type
      act_price
      part_qty
      mod_lbr_ty
      mod_lb_hrs
      oem_partno
      alt_partno
      op_code_desc
    }
  }
}`;

exports.UPDATE_PARTS_CRITICAL = `mutation UPDATE_PARTS_CRITICAL ($IdsToMarkCritical:[uuid!]!, $jobid: uuid!) {
  critical: update_joblines(where: {id: {_in: $IdsToMarkCritical}}, _set: {critical: true}) {
    affected_rows
  }
  notcritical: update_joblines(where: {id: {_nin: $IdsToMarkCritical}, jobid: {_eq: $jobid}}, _set: {critical: false}) {
    affected_rows
  }
}`;

exports.ACTIVE_SHOP_BY_USER = `query ACTIVE_SHOP_BY_USER($user: String) {
  associations(where: {active: {_eq: true}, useremail: {_eq: $user}}) {
    id
    shopid
    bodyshop {
      rr_dealerid
    }
  }
}`;

exports.QUERY_JOB_PAYROLL_DATA = `query QUERY_JOB_PAYROLL_DATA($id: uuid!) {
  jobs_by_pk(id: $id) {
    bodyshop{
      id
      md_responsibility_centers
      md_tasks_presets
      employee_teams{
        id
        name
        employee_team_members{
          id
          employee{
            id
            first_name
            last_name
          }
          percentage
          labor_rates
          payout_method
          commission_rates
        }
      }
    }
    timetickets{
      id
      employeeid
      rate
      productivehrs
      actualhrs
      ciecacode
      payout_context
    }
    lbr_adjustments
    ro_number
    id
    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
    materials
    completed_tasks
    joblines(where: { removed: { _eq: false } }){
      id
      line_no
      unq_seq
      line_ind
      line_desc
      part_type
      line_ref
      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
      prt_dsmk_p
      prt_dsmk_m
      misc_amt
      misc_tax
      assigned_team
      convertedtolbr
      convertedtolbr_data
    }
  }
}`;

exports.INSERT_TIME_TICKETS = `mutation INSERT_TIMETICKETS($timetickets: [timetickets_insert_input!]!) {
  insert_timetickets(objects: $timetickets) {
    affected_rows
  }
}
`;

exports.QUERY_JOB_PAYROLL_DATA = `query QUERY_JOB_PAYROLL_DATA($id: uuid!) {
  jobs_by_pk(id: $id) {
    bodyshop{
      id
      md_responsibility_centers
      md_tasks_presets
      employee_teams{
        id
        name
        employee_team_members{
          id
          employee{
            id
            first_name
            last_name
          }
          percentage
          labor_rates
          payout_method
          commission_rates
        }
      }
    }
    timetickets{
      id
      employeeid
      rate
      productivehrs
      actualhrs
      ciecacode
      payout_context
    }
    lbr_adjustments
    ro_number
    id
    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
    materials
    completed_tasks
    joblines(where: { removed: { _eq: false } }){
      id
      line_no
      unq_seq
      line_ind
      line_desc
      part_type
      line_ref
      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
      prt_dsmk_p
      prt_dsmk_m
      misc_amt
      misc_tax
      assigned_team
      convertedtolbr
      convertedtolbr_data
    }
  }
}`;

exports.INSERT_TIME_TICKETS = `mutation INSERT_TIMETICKETS($timetickets: [timetickets_insert_input!]!) {
  insert_timetickets(objects: $timetickets) {
    affected_rows
  }
}
`;

exports.QUERY_SURVEY = `query QUERY_SURVEY($surveyId: uuid!) {
  csi_by_pk(id: $surveyId) {
    completedon
    csiquestion {
      id
      config
    }
    id
    relateddata
    valid
    validuntil
  }
}`;

exports.COMPLETE_SURVEY = `mutation COMPLETE_SURVEY($surveyId: uuid!, $survey: csi_set_input) {
    update_csi(where: { id: { _eq: $surveyId } }, _set: $survey) {
      affected_rows
    }
  }`;

exports.QUERY_REMIND_TASKS = `
  query QUERY_REMIND_TASKS($time: timestamptz!) {
    tasks(
      where: {
        _and: [
          { remind_at: { _is_null: false } }
          { remind_at: { _lte: $time } }
          { remind_at_sent: { _is_null: true } }
        ]
      }
    ) {
      id
      title
      description
      due_date
      created_by
      assigned_to
      assigned_to_employee {
        id
        user_email
      }
      remind_at
      remind_at_sent
      priority
      job {
        id
        ownr_co_nm
        ownr_fn
        ownr_ln
        v_make_desc
        v_model_desc
        v_model_yr
        ro_number
      }
      jobid
      bodyshop {
        shopname
        convenient_company
        timezone
      }
      bodyshopid
    }
  }
`;

exports.UPDATE_TASKS_REMIND_AT_SENT = `mutation UPDATE_TASK_REMIND_AT_SENT($taskIds: [uuid!]!, $now: timestamptz!) {
  update_tasks_many(updates: {where: {id: {_in: $taskIds}}, _set: {remind_at_sent: $now}}) {
    affected_rows
  }
}`;

exports.QUERY_TASK_BY_ID = `
query QUERY_TASK_BY_ID($id: uuid!) {
  tasks_by_pk(id: $id) {
    id
    created_by
    assigned_to_employee{
      id
      user_email
    }
    bodyshop{
      shopname
      convenient_company
      timezone
    }
    job{
      ro_number
      ownr_fn
      ownr_ln
      ownr_co_nm
      v_make_desc
      v_model_desc
      v_model_yr
    }
  }
}

`;

exports.GET_JOBS_BY_PKS = `query GET_JOBS_BY_PKS($ids: [uuid!]!) {
  jobs(where: {id: {_in: $ids}}) {
    id
    shopid
    ro_number
    ownr_co_nm
    ownr_fn
    ownr_ln
    v_make_desc
    v_model_yr
    v_model_desc
  }
}
`;

exports.MARK_MESSAGES_AS_READ = `mutation MARK_MESSAGES_AS_READ($conversationId: uuid!) {
  update_messages(where: { conversationid: { _eq: $conversationId } }, _set: { read: true }) {
      returning {
      id
    }
    affected_rows
  }
}
`;

exports.CREATE_CONVERSATION = `mutation CREATE_CONVERSATION($conversation: [conversations_insert_input!]!) {
  insert_conversations(objects: $conversation) {
    returning {
      id
      phone_num
      archived
      label
      unreadcnt
      job_conversations {
        jobid
        conversationid
        job {
          id
          ro_number
          ownr_fn
          ownr_ln
          ownr_co_nm
        }
      }
      messages_aggregate(where: { read: { _eq: false }, isoutbound: { _eq: false } }) {
        aggregate {
          count
        }
      }
      created_at
      updated_at
    }
  }
}
`;

exports.STATUS_UPDATE = `query STATUS_UPDATE($period: timestamptz!, $today: timestamptz!) {
  bodyshops(where: { created_at: { _gte: $period } }) {
    shopname
    id
    created_at
    jobs_created: jobs_aggregate(where: { created_at: { _gte: $today } }) {
      aggregate {
        count
      }
    }
    jobs_updated: jobs_aggregate(where: { updated_at: { _gte: $today } }) {
      aggregate {
        count
      }
    }

    owners_created: owners_aggregate(where: { created_at: { _gte: $today } }) {
      aggregate {
        count
      }
    }
    owners_updated: owners_aggregate(where: { updated_at: { _gte: $today } }) {
      aggregate {
        count
      }
    }

    vehicles_created: vehicles_aggregate(
      where: { created_at: { _gte: $today } }
    ) {
      aggregate {
        count
      }
    }
    vehicles_updated: vehicles_aggregate(
      where: { updated_at: { _gte: $today } }
    ) {
      aggregate {
        count
      }
    }

    tasks_created: tasks_aggregate(where: { created_at: { _gte: $today } }) {
      aggregate {
        count
      }
    }
    tasks_updated: tasks_aggregate(where: { updated_at: { _gte: $today } }) {
      aggregate {
        count
      }
    }
    jobs {
      parts_orders_created: parts_orders_aggregate(
        where: { created_at: { _gte: $today } }
      ) {
        aggregate {
          count
        }
      }
      parts_orders_updated: parts_orders_aggregate(
        where: { updated_at: { _gte: $today } }
      ) {
        aggregate {
          count
        }
      }
    }
  }
}
`;

exports.INSERT_AUDIT_TRAIL = `
  mutation INSERT_AUDIT_TRAIL($auditObj: audit_trail_insert_input!) {
    insert_audit_trail_one(object: $auditObj) {
      id
      jobid
      billid
      bodyshopid
      created
      operation
      type
      useremail
    }
  }
`;

exports.GET_JOB_WATCHERS = `
query GET_JOB_WATCHERS($jobid: uuid!) {
  job_watchers(where: { jobid: { _eq: $jobid } }) {
    user_email
    user { 
      authid
      employee {
        id
        first_name
        last_name
      }
    }
  }
  job: jobs_by_pk(id: $jobid) {
    id
    ro_number
    clm_no 
    bodyshop {
      id
      shopname
      timezone
    }
  }
}
`;

exports.GET_NOTIFICATION_ASSOCIATIONS = `
query GET_NOTIFICATION_ASSOCIATIONS($emails: [String!]!, $shopid: uuid!) {
  associations(where: { 
    useremail: { _in: $emails },
    shopid: { _eq: $shopid }
  }) {
    id
    useremail
    notification_settings
  }
}
`;

exports.INSERT_NOTIFICATIONS_MUTATION = `  mutation INSERT_NOTIFICATIONS($objects: [notifications_insert_input!]!) {
    insert_notifications(objects: $objects) {
      affected_rows
      returning {
        id
        jobid
        associationid
        scenario_text
        fcm_text
        scenario_meta
      }
    }
  }`;

// REMEMBER: Update the cache_bodyshop event in hasura to include any added fields
exports.GET_BODYSHOP_BY_ID = `
  query GET_BODYSHOP_BY_ID($id: uuid!) {
    bodyshops_by_pk(id: $id) {
      id
      md_ro_statuses
      md_order_statuses
      shopname
      imexshopid
      intellipay_config
      state
      notification_followers
      timezone
      rr_dealerid
      rr_configuration
    }
  }
`;

exports.GET_BODYSHOP_WATCHERS_BY_ID = `
  query GET_BODYSHOP_BY_ID($id: uuid!) {
    bodyshops_by_pk(id: $id) {
      id
      notification_followers
    }
  }
`;

exports.GET_DOCUMENTS_BY_JOB = `
  query GET_DOCUMENTS_BY_JOB($jobId: uuid!) {
    jobs_by_pk(id: $jobId) {
      id
      ro_number
    }
    documents_aggregate(where: { jobid: { _eq: $jobId } }) {
      aggregate {
        sum {
          size
        }
      }
    }
    documents(order_by: { takenat: desc }, where: { jobid: { _eq: $jobId } }) {
      id
      name
      key
      type
      size
      takenat
      extension
      bill {
        id
        invoice_number
        date
        vendor {
          id
          name
        }
      }
    }
  }`;
exports.GET_DOCUMENTS_BY_BILL = `
query GET_DOCUMENTS_BY_BILL($billId: uuid!) {
  documents_aggregate(where: {billid: {_eq: $billId}}) {
    aggregate {
      sum {
        size
      }
    }
  }
  documents(order_by: {takenat: desc}, where: {billid: {_eq: $billId}}) {
    id
    name
    key
    type
    size
    takenat
    extension
  }
}
`;

exports.QUERY_TEMPORARY_DOCS = `  query QUERY_TEMPORARY_DOCS {
    documents(where: { jobid: { _is_null: true } }, order_by: { takenat: desc }) {
      id
      name
      key
      type
      extension
      size
      takenat
    }
  }`;

exports.GET_DOCUMENTS_BY_IDS = `
  query GET_DOCUMENTS_BY_IDS($documentIds: [uuid!]!) {
  documents(where: {id: {_in: $documentIds}}, order_by: {takenat: desc}) {
    id
    name
    key
    type
    extension
    size
    takenat
  }
}`;

exports.GET_JOBID_BY_MERCHANTID_RONUMBER = `
query GET_JOBID_BY_MERCHANTID_RONUMBER($merchantID: String!, $roNumber: String!) {
  jobs(where: {ro_number: {_eq: $roNumber}, bodyshop: {intellipay_merchant_id: {_eq: $merchantID}}}) {
    id
    shopid
    bodyshop {
      id
      intellipay_config
      email
      timezone
    }
  }
}`;

exports.GET_BODYSHOP_BY_MERCHANT_ID = `
query GET_BODYSHOP_BY_MERCHANTID($merchantID: String!) {
  bodyshops(where: {intellipay_merchant_id: {_eq: $merchantID}}) {
    id
    email
    timezone
  }
}`;

exports.GET_USER_BY_EMAIL = `
query GET_USER_BY_EMAIL($email: String!) {
  users(where: {email: {_eq: $email}}) {
    email
    validemail
    associations {
      id
      shopid
      bodyshop {
        id
        convenient_company
        features
        timezone
      }
    }
  }
}`;

// Define the GraphQL query to get a job by RO number and shop ID
exports.GET_JOB_BY_RO_NUMBER_AND_SHOP_ID = `
  query GET_JOB_BY_RO_NUMBER_AND_SHOP_ID($roNumber: String!, $shopId: uuid!) {
    jobs(where: {ro_number: {_eq: $roNumber}, shopid: {_eq: $shopId}}, limit: 1) {
      id
      shopid
      bodyshop {
        timezone
      }
    }
  }
`;

// Define the mutation to insert a new document
exports.INSERT_NEW_DOCUMENT = `
  mutation INSERT_NEW_DOCUMENT($docInput: [documents_insert_input!]!) {
    insert_documents(objects: $docInput) {
      returning {
        id
        name
        key
      }
    }
  }
`;

exports.INSERT_JOB_WATCHERS = `
  mutation INSERT_JOB_WATCHERS($watchers: [job_watchers_insert_input!]!) {
    insert_job_watchers(objects: $watchers, on_conflict: { constraint: job_watchers_pkey, update_columns: [] }) {
      affected_rows
    }
  }
`;

exports.GET_NOTIFICATION_WATCHERS = `
  query GET_NOTIFICATION_WATCHERS($shopId: uuid!, $employeeIds: [uuid!]!, $createdUserEmail: String!) {
    associations(where: {
      _and: [
        { shopid: { _eq: $shopId } },
        { active: { _eq: true } },
        { notifications_autoadd: { _eq: true } },
        { useremail: { _eq: $createdUserEmail } }
      ]
    }) {
      id
      useremail
    }

    employees(where: { id: { _in: $employeeIds }, shopid: { _eq: $shopId }, active: { _eq: true } }) {
      user_email
    }
  }
`;

exports.GET_JOB_WATCHERS_MINIMAL = `
  query GET_JOB_WATCHERS_MINIMAL($jobid: uuid!) {
    job_watchers(where: { jobid: { _eq: $jobid } }) {
      user_email
      user {
        authid
      }
    }
  }
`;

exports.INSERT_INTEGRATION_LOG = `
  mutation INSERT_INTEGRATION_LOG($log: integration_log_insert_input!) {
    insert_integration_log_one(object: $log) {
      id
    }
  }
`;

exports.INSERT_PHONE_NUMBER_OPT_OUT = `
  mutation INSERT_PHONE_NUMBER_OPT_OUT($optOutInput: [phone_number_opt_out_insert_input!]!) {
    insert_phone_number_opt_out(objects: $optOutInput, on_conflict: { constraint: phone_number_consent_bodyshopid_phone_number_key, update_columns: [updated_at] }) {
      affected_rows
      returning {
        id
        bodyshopid
        phone_number
        created_at
        updated_at
      }
    }
  }
`;

// Query to check if a phone number is opted out
exports.CHECK_PHONE_NUMBER_OPT_OUT = `
  query CHECK_PHONE_NUMBER_OPT_OUT($bodyshopid: uuid!, $phone_number: String!) {
    phone_number_opt_out(where: { bodyshopid: { _eq: $bodyshopid }, phone_number: { _eq: $phone_number } }) {
      id
      bodyshopid
      phone_number
      created_at
      updated_at
    }
  }
`;

// Query to check if a phone number is opted out
exports.CHECK_PHONE_NUMBER_OPT_OUT = `
  query CHECK_PHONE_NUMBER_OPT_OUT($bodyshopid: uuid!, $phone_number: String!) {
    phone_number_opt_out(where: { bodyshopid: { _eq: $bodyshopid }, phone_number: { _eq: $phone_number } }) {
      id
      bodyshopid
      phone_number
      created_at
      updated_at
    }
  }
`;

// Mutation to delete a phone number opt-out record
exports.DELETE_PHONE_NUMBER_OPT_OUT = `
  mutation DELETE_PHONE_NUMBER_OPT_OUT($bodyshopid: uuid!, $phone_number: String!) {
    delete_phone_number_opt_out(where: { bodyshopid: { _eq: $bodyshopid }, phone_number: { _eq: $phone_number } }) {
      affected_rows
      returning {
        id
        bodyshopid
        phone_number
      }
    }
  }
`;

exports.INSERT_MEDIA_ANALYTICS = `
mutation INSERT_MEDIA_ANALYTICS($mediaObject: media_analytics_insert_input!) {
  insert_media_analytics_one(object: $mediaObject) {
    id
  }
}
`;

exports.GET_USERS_FCM_TOKENS_BY_EMAILS = /* GraphQL */ `
  query GET_USERS_FCM_TOKENS_BY_EMAILS($emails: [String!]!) {
    users(where: { email: { _in: $emails } }) {
      email
      fcmtokens
    }
  }
`;

exports.UPDATE_USER_FCM_TOKENS_BY_EMAIL = /* GraphQL */ `
  mutation UPDATE_USER_FCM_TOKENS_BY_EMAIL($email: String!, $fcmtokens: jsonb) {
    update_users(where: { email: { _eq: $email } }, _set: { fcmtokens: $fcmtokens }) {
      affected_rows
    }
  }
`;

exports.SET_JOB_DMS_ID = `mutation SetJobDmsId($id: uuid!, $dms_id: String!, $dms_customer_id: String, $dms_advisor_id: String, $kmin: Int) {
    update_jobs_by_pk(pk_columns: { id: $id }, _set: { dms_id: $dms_id, dms_customer_id: $dms_customer_id, dms_advisor_id: $dms_advisor_id, kmin: $kmin }) {
      id
      dms_id
      dms_customer_id
      dms_advisor_id
      kmin
    }
  }`;
