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

export const QUERY_SCOREBOARD = gql`
  query QUERY_SCOREBOARD($start: date!, $end: date!) {
    scoreboard(where: { _and: { date: { _gte: $start, _lte: $end } } }, order_by: { date: asc }) {
      id
      painthrs
      bodyhrs
      date
      job {
        id
        ro_number
        ownr_fn
        ownr_ln
        ownr_co_nm
        v_make_desc
        v_model_desc
        v_model_yr
        job_totals
      }
    }
  }
`;

export const DELETE_SCOREBOARD_ENTRY = gql`
  mutation DELETE_SCOREBOARD_ENTRY($sbId: uuid!) {
    delete_scoreboard_by_pk(id: $sbId) {
      id
    }
  }
`;
export const INSERT_SCOREBOARD_ENTRY = gql`
  mutation INSERT_SCOREBOARD_ENTRY($sbInput: [scoreboard_insert_input!]!) {
    insert_scoreboard(objects: $sbInput) {
      returning {
        id
        date
        bodyhrs
        painthrs
      }
    }
  }
`;

export const UPDATE_SCOREBOARD_ENTRY = gql`
  mutation UPDATE_SCOREBOARD_ENTRY($sbId: uuid!, $sbInput: scoreboard_set_input!) {
    update_scoreboard_by_pk(_set: $sbInput, pk_columns: { id: $sbId }) {
      id
      date
      bodyhrs
      painthrs
    }
  }
`;

export const QUERY_SCOREBOARD_ENTRY = gql`
  query QUERY_SCOREBOARD_ENTRY($jobid: uuid!) {
    scoreboard(where: { jobid: { _eq: $jobid } }) {
      bodyhrs
      date
      id
      painthrs
    }
  }
`;

export const GET_BLOCKED_DAYS = gql`
  query GET_BLOCKED_DAYS($start: timestamptz, $end: timestamptz) {
    appointments(
      where: {
        _and: [
          { block: { _eq: true } }
          { canceled: { _eq: false } }
          { start: { _gte: $start } }
          { end: { _lte: $end } }
        ]
      }
    ) {
      id
      block
      start
      end
    }
  }
`;

export const QUERY_SCOREBOARD_PAGINATED = gql`
  query QUERY_SCOREBOARD_PAGINATED($search: String, $offset: Int, $limit: Int, $order: [scoreboard_order_by!]) {
    scoreboard(where: { job: { ro_number: { _ilike: $search } } }, offset: $offset, limit: $limit, order_by: $order) {
      id
      jobid
      job {
        id
        ro_number
        invoice_date
        v_make_desc
        v_model_desc
        v_model_yr
        ownr_fn
        ownr_ln
        ownr_co_nm
      }
      date
      bodyhrs
      painthrs
    }
    scoreboard_aggregate(where: { job: { ro_number: { _ilike: $search } } }) {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;
