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

export const QUERY_TICKETS_BY_JOBID = gql`
  query QUERY_TICKETS_BY_JOBID($jobid: uuid!) {
    timetickets(
      where: { jobid: { _eq: $jobid } }
      order_by: { date: desc_nulls_first }
    ) {
      actualhrs
      cost_center
      ciecacode
      rate
      productivehrs
      id
      memo
      jobid
      flat_rate
      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 } }
      order_by: { date: desc_nulls_first }
    ) {
      actualhrs
      ciecacode
      clockoff
      clockon
      cost_center
      created_at
      date
      id
      rate
      productivehrs
      memo
      jobid
      flat_rate
      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!
  ) {
    timetickets(
      where: { date: { _gte: $start, _lte: $end } }
      order_by: { date: desc_nulls_first }
    ) {
      actualhrs
      ciecacode
      clockoff
      clockon
      cost_center
      created_at
      date
      id
      rate
      actualhrs
      productivehrs
      memo
      jobid
      flat_rate
      job {
        id
        ro_number
      }
      employeeid
      employee {
        id
        employee_number
        first_name
        last_name
      }
    }
    fixedperiod: timetickets(
      where: { date: { _gte: $fixedStart, _lte: $fixedEnd } }
      order_by: { date: desc_nulls_first }
    ) {
      actualhrs
      ciecacode
      clockoff
      clockon
      cost_center
      created_at
      date
      id
      rate
      productivehrs
      memo
      jobid
      flat_rate
      job {
        id
        ro_number
      }
      employeeid
      employee {
        id
        employee_number
        first_name
        last_name
      }
    }
  }
`;

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

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

export const QUERY_ACTIVE_TIME_TICKETS = gql`
  query QUERY_ACTIVE_TIME_TICKETS($employeeId: uuid) {
    timetickets(
      order_by: { date: desc_nulls_first }
      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
      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
    }
  }
`;
