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

export const INSERT_NEW_CONTRACT = gql`
  mutation INSERT_NEW_CONTRACT(
    $contract: [cccontracts_insert_input!]!
    $ccId: uuid!
    $damage: String
    $mileage: numeric
  ) {
    insert_cccontracts(objects: $contract) {
      returning {
        id
      }
    }
    update_courtesycars_by_pk(
      pk_columns: { id: $ccId }
      _set: { status: "courtesycars.status.out", mileage: $mileage, damage: $damage }
    ) {
      status
      id
      damage
      mileage
    }
  }
`;

export const UPDATE_CONTRACT = gql`
  mutation UPDATE_CONTRACT($contractId: uuid!, $cccontract: cccontracts_set_input!) {
    update_cccontracts(where: { id: { _eq: $contractId } }, _set: $cccontract) {
      returning {
        id
      }
    }
  }
`;
export const RETURN_CONTRACT = gql`
  mutation RETURN_CONTRACT(
    $contractId: uuid!
    $cccontract: cccontracts_set_input!
    $courtesycarid: uuid!
    $courtesycar: courtesycars_set_input!
  ) {
    update_cccontracts(where: { id: { _eq: $contractId } }, _set: $cccontract) {
      returning {
        id
      }
    }
    update_courtesycars(where: { id: { _eq: $courtesycarid } }, _set: $courtesycar) {
      returning {
        id
      }
    }
  }
`;

export const QUERY_CONTRACT_BY_PK = gql`
  query QUERY_CONTRACT_BY_PK($id: uuid!) {
    cccontracts_by_pk(id: $id) {
      actualreturn
      agreementnumber
      courtesycarid
      driver_addr1
      driver_city
      driver_addr2
      driver_dlexpiry
      driver_dlnumber
      driver_dlst
      driver_dob
      driver_fn
      driver_ln
      driver_ph1
      driver_state
      driver_zip
      id
      jobid
      dailyrate
      actax
      dailyfreekm
      refuelcharge
      excesskmrate
      cleanupcharge
      damagewaiver
      federaltax
      statetax
      localtax
      coverage
      fuelin
      fuelout
      damage
      job {
        id

        ro_number
        v_make_desc
        v_model_desc
        ownr_fn
        ownr_ln
        ownr_co_nm
        clm_no
        scheduled_completion
        ownerid
        vehicleid
        owner {
          id
          ownr_fn
          ownr_ln
          ownr_co_nm
        }
        vehicle {
          id
          v_make_desc
          v_model_desc
          v_model_yr
          v_vin
        }
      }
      kmend
      kmstart
      scheduledreturn
      start
      status
      updated_at
      courtesycar {
        id
        fleetnumber
        make
        model
        year
        plate
      }
    }
  }
`;

export const QUERY_ACTIVE_CONTRACTS_PAGINATED = gql`
  query QUERY_ACTIVE_CONTRACTS_PAGINATED($search: String, $offset: Int, $limit: Int, $order: [cccontracts_order_by!]!) {
    search_cccontracts(args: { search: $search }, offset: $offset, limit: $limit, order_by: $order) {
      agreementnumber
      courtesycarid
      driver_fn
      driver_ln
      driver_ph1
      id
      jobid
      job {
        id
        ro_number
        ownr_fn
        ownr_ln
        ownr_co_nm
      }
      scheduledreturn
      actualreturn
      start
      status
      courtesycar {
        id
        fleetnumber
        make
        model
        year
        plate
      }
    }
    search_cccontracts_aggregate(args: { search: $search }) {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;

export const FIND_CONTRACT = gql`
  query FIND_CONTRACT($plate: String, $time: timestamptz!) {
    cccontracts(
      where: {
        _or: [{ actualreturn: { _gte: $time } }, { actualreturn: { _is_null: true } }]
        start: { _lte: $time }
        courtesycar: { plate: { _eq: $plate } }
      }
    ) {
      agreementnumber
      courtesycarid
      driver_fn
      driver_ln
      driver_ph1
      id
      jobid
      job {
        id
        ro_number
        ownr_fn
        ownr_ln
        ownr_co_nm
      }
      scheduledreturn
      actualreturn
      start
      status
      courtesycar {
        id
        fleetnumber
        make
        model
        year
        plate
      }
    }
  }
`;
