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

export const INSERT_NEW_COURTESY_CAR = gql`
  mutation INSERT_NEW_COURTESY_CAR($courtesycar: [courtesycars_insert_input!]!) {
    insert_courtesycars(objects: $courtesycar) {
      returning {
        id
      }
    }
  }
`;

export const QUERY_AVAILABLE_CC = gql`
  query QUERY_AVAILABLE_CC($today: date) {
    courtesycars(
      where: {
        _or: [{ serviceenddate: { _is_null: true } }, { serviceenddate: { _gt: $today } }]
        status: { _eq: "courtesycars.status.in" }
      }
      order_by: { fleetnumber: asc }
    ) {
      color
      dailycost
      damage
      fleetnumber
      fuel
      id
      insuranceexpires
      make
      mileage
      model
      notes
      nextservicekm
      nextservicedate
      plate
      readiness
      status
      year
    }
  }
`;

export const CHECK_CC_FLEET_NUMBER = gql`
  query CHECK_CC_FLEET_NUMBER($name: String!) {
    courtesycars_aggregate(where: { fleetnumber: { _ilike: $name } }) {
      aggregate {
        count
      }
      nodes {
        id
      }
    }
  }
`;
export const QUERY_ALL_CC = gql`
  query QUERY_ALL_CC {
    courtesycars(order_by: { fleetnumber: asc }) {
      color
      created_at
      dailycost
      damage
      fleetnumber
      fuel
      id
      insuranceexpires
      leaseenddate
      make
      mileage
      model
      nextservicedate
      nextservicekm
      notes
      plate
      purchasedate
      readiness
      registrationexpires
      serviceenddate
      servicestartdate
      status
      vin
      year
      cccontracts(where: { status: { _eq: "contracts.status.out" } }, order_by: { contract_date: desc }, limit: 1) {
        id
        scheduledreturn
        job {
          id
          ro_number
          ownr_fn
          ownr_ln
          ownr_co_nm
        }
      }
    }
  }
`;

export const QUERY_CC_BY_PK = gql`
  query QUERY_CC_BY_PK($id: uuid!, $offset: Int, $limit: Int, $order: [cccontracts_order_by!]!) {
    courtesycars_by_pk(id: $id) {
      bodyshopid
      color
      created_at
      dailycost
      damage
      fleetnumber
      fuel
      id
      insuranceexpires
      leaseenddate
      make
      mileage
      model
      nextservicedate
      nextservicekm
      notes
      plate
      purchasedate
      readiness
      registrationexpires
      serviceenddate
      servicestartdate
      status
      vin
      year
      cccontracts_aggregate {
        aggregate {
          count(distinct: true)
        }
      }
      cccontracts(offset: $offset, limit: $limit, order_by: $order) {
        agreementnumber
        driver_fn
        driver_ln
        id
        kmstart
        kmend
        scheduledreturn
        start
        status
        job {
          id
          ro_number
          ownr_fn
          ownr_ln
          ownr_co_nm
        }
      }
    }
  }
`;

export const UPDATE_CC = gql`
  mutation UPDATE_CC($ccId: uuid!, $cc: courtesycars_set_input!) {
    update_courtesycars(where: { id: { _eq: $ccId } }, _set: $cc) {
      returning {
        id
      }
    }
  }
`;
