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

export const QUERY_TICKETS_BY_JOBID = gql`
  query QUERY_TICKETS_BY_JOBID($jobid: uuid!) {
    timetickets(where: { jobid: { _eq: $jobid } }) {
      actualhrs
      cost_center
      ciecacode
      rate
      productivehrs
      id
      memo
      jobid
      flat_rate
      commited_by
      committed_at
      employee {
        employee_number
        first_name
        last_name
        id
      }
    }
  }
`;

export const QUERY_TIME_TICKETS_IN_RANGE = gql`
  query QUERY_TIME_TICKETS_IN_RANGE($start: date!, $end: date!) {
    timetickets(where: { date: { _gte: $start, _lte: $end } }) {
      actualhrs
      ciecacode
      clockoff
      clockon
      cost_center
      created_at
      created_by
      date
      id
      rate
      productivehrs
      memo
      jobid
      flat_rate
      commited_by
      committed_at
      task_name
      job {
        id
        ro_number
      }
      employeeid
      employee {
        id
        employee_number
        first_name
        last_name
      }
    }
  }
`;

export const QUERY_TIME_TICKETS_TECHNICIAN_IN_RANGE = gql`
  query QUERY_TIME_TICKETS_TECHNICIAN_IN_RANGE(
    $employeeid: uuid!
    $start: date!
    $end: date!
    $fixedStart: date!
    $fixedEnd: date!
  ) {
    timetickets(
      where: { date: { _gte: $start, _lte: $end }, employeeid: { _eq: $employeeid } }
    ) {
      actualhrs
      ciecacode
      clockoff
      clockon
      cost_center
      created_at
      created_by
      date
      id
      rate
      productivehrs
      memo
      jobid
      commited_by
      committed_at
      flat_rate
      job {
        id
        ro_number
      }
      employeeid
      employee {
        id
        employee_number
        first_name
        last_name
      }
    }
    fixedperiod: timetickets(
      where: { date: { _gte: $fixedStart, _lte: $fixedEnd }, employeeid: { _eq: $employeeid } }
    ) {
      actualhrs
      ciecacode
      clockoff
      clockon
      cost_center
      created_at
      created_by
      date
      id
      rate
      productivehrs
      memo
      jobid
      flat_rate
      commited_by
      committed_at
      job {
        id
        ro_number
      }
      employeeid
      employee {
        id
        employee_number
        first_name
        last_name
      }
    }
  }
`;

export const QUERY_TIME_TICKETS_IN_RANGE_SB = gql`
  query QUERY_TIME_TICKETS_IN_RANGE_SB(
    $start: date!
    $end: date!
    $fixedStart: date!
    $fixedEnd: date!
    $jobStart: timestamptz!
    $jobEnd: timestamptz!
  ) {
    timetickets(
      where: { date: { _gte: $start, _lte: $end }, cost_center: { _neq: "timetickets.labels.shift" } }
    ) {
      actualhrs
      ciecacode
      clockoff
      clockon
      cost_center
      created_at
      created_by
      date
      id
      rate
      actualhrs
      productivehrs
      memo
      jobid
      committed_at
      commited_by
      flat_rate
      commited_by
      committed_at
      job {
        id
        ro_number
      }
      employeeid
      employee {
        id
        employee_number
        first_name
        last_name
      }
    }
    fixedperiod: timetickets(
      where: { date: { _gte: $fixedStart, _lte: $fixedEnd }, cost_center: { _neq: "timetickets.labels.shift" } }
    ) {
      actualhrs
      ciecacode
      clockoff
      clockon
      cost_center
      created_at
      created_by
      date
      id
      rate
      productivehrs
      memo
      jobid
      flat_rate
      job {
        id
        ro_number
      }
      employeeid
      employee {
        id
        employee_number
        first_name
        last_name
      }
    }
    jobs(
      where: {
        date_invoiced: { _is_null: true }
        ro_number: { _is_null: false }
        voided: { _eq: false }
        _or: [
          { actual_completion: { _gte: $jobStart, _lte: $jobEnd } }
          { actual_delivery: { _gte: $jobStart, _lte: $jobEnd } }
        ]
      }
    ) {
      id
      joblines(order_by: { line_no: asc }, where: { removed: { _eq: false } }) {
        id
        convertedtolbr
        convertedtolbr_data
        mod_lb_hrs
        mod_lbr_ty
      }
    }
  }
`;

export const INSERT_NEW_TIME_TICKET = gql`
  mutation INSERT_NEW_TIME_TICKET($timeTicketInput: [timetickets_insert_input!]!) {
    insert_timetickets(objects: $timeTicketInput) {
      returning {
        id
        created_by
        clockon
        clockoff
        employeeid
        productivehrs
        actualhrs
        ciecacode
        date
        memo
        flat_rate
        commited_by
        committed_at
      }
    }
  }
`;

export const INSERT_TIME_TICKET_AND_APPROVE = gql`
  mutation INSERT_TIME_TICKET_AND_APPROVE(
    $timeTicketInput: [timetickets_insert_input!]!
    $approvalIds: [uuid!]!
    $approvalUpdate: tt_approval_queue_set_input
  ) {
    insert_timetickets(objects: $timeTicketInput) {
      returning {
        id
        clockon
        clockoff
        created_by
        employeeid
        productivehrs
        actualhrs
        ciecacode
        date
        memo
        flat_rate
        task_name
        payout_context
        ttapprovalqueueid
        commited_by
        committed_at
      }
    }
    update_tt_approval_queue(where: { id: { _in: $approvalIds } }, _set: $approvalUpdate) {
      returning {
        id
        approved_at
        approved_at
      }
    }
  }
`;

export const UPDATE_TIME_TICKET = gql`
  mutation UPDATE_TIME_TICKET($timeticketId: uuid!, $timeticket: timetickets_set_input!) {
    update_timetickets(where: { id: { _eq: $timeticketId } }, _set: $timeticket) {
      returning {
        id
        clockon
        clockoff
        employeeid
        productivehrs
        actualhrs
        ciecacode
        created_at
        updated_at
        jobid
        date
        flat_rate
        memo
        committed_at
        commited_by
      }
    }
  }
`;

export const UPDATE_TIME_TICKETS = gql`
  mutation UPDATE_TIME_TICKETS($timeticketIds: [uuid!]!, $timeticket: timetickets_set_input!) {
    update_timetickets(where: { id: { _in: $timeticketIds } }, _set: $timeticket) {
      returning {
        id
        clockon
        clockoff
        employeeid
        productivehrs
        actualhrs
        ciecacode
        created_at
        updated_at
        jobid
        date
        flat_rate
        memo
        committed_at
        commited_by
      }
    }
  }
`;

export const QUERY_ACTIVE_TIME_TICKETS = gql`
  query QUERY_ACTIVE_TIME_TICKETS($employeeId: uuid) {
    timetickets(
      where: {
        _and: {
          clockoff: { _is_null: true }
          employeeid: { _eq: $employeeId }
          clockon: { _is_null: false }
          jobid: { _is_null: false }
        }
      }
    ) {
      id
      clockon
      memo
      cost_center
      flat_rate
      jobid
      commited_by
      committed_at
      job {
        id
        ownr_fn
        ownr_ln
        ownr_co_nm
        v_model_desc
        v_make_desc
        v_model_yr
        ro_number
      }
    }
  }
`;

export const QUERY_ACTIVE_SHIFT_TIME_TICKETS = gql`
  query QUERY_ACTIVE_SHIFT_TIME_TICKETS($employeeId: uuid) {
    timetickets(
      where: {
        _and: {
          clockoff: { _is_null: true }
          employeeid: { _eq: $employeeId }
          clockon: { _is_null: false }
          jobid: { _is_null: true }
        }
      }
    ) {
      id
      clockon
      memo
      jobid
      job {
        id
        ownr_fn
        ownr_ln
        ownr_co_nm
        v_model_desc
        v_make_desc
        v_model_yr
        ro_number
      }
    }
  }
`;

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