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

export const INSERT_NEW_PAYMENT = gql`
  mutation INSERT_NEW_PAYMENT($paymentInput: [payments_insert_input!]!) {
    insert_payments(objects: $paymentInput) {
      returning {
        id
        jobid
        amount
        payer
        created_at
        transactionid
        memo
        date
        type
        exportedat
      }
    }
  }
`;

export const QUERY_ALL_PAYMENTS_PAGINATED = gql`
  query QUERY_ALL_PAYMENTS_PAGINATED($offset: Int, $limit: Int, $order: [payments_order_by!]!) {
    payments(offset: $offset, limit: $limit, order_by: $order) {
      id
      amount
      created_at
      date
      exportedat
      jobid
      job {
        id
        ownerid
        ownr_co_nm
        ownr_fn
        ownr_ln
        owner {
          id
          ownr_co_nm
          ownr_fn
          ownr_ln
        }
        ro_number
      }
      memo
      payer
      paymentnum
      stripeid
      transactionid
      type
    }
    payments_aggregate {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;

export const UPDATE_PAYMENT = gql`
  mutation UPDATE_PAYMENT($paymentId: uuid!, $payment: payments_set_input!) {
    update_payments(where: { id: { _eq: $paymentId } }, _set: $payment) {
      returning {
        id
        amount
        created_at
        date
        exportedat
        jobid
        job {
          id
          ownerid
          ownr_co_nm
          ownr_fn
          ownr_ln
          owner {
            id
            ownr_co_nm
            ownr_fn
            ownr_ln
          }
          ro_number
        }
        memo
        payer
        paymentnum
        stripeid
        transactionid
        type
      }
    }
  }
`;

export const UPDATE_PAYMENTS = gql`
  mutation UPDATE_PAYMENTS($paymentIdList: [uuid!]!, $payment: payments_set_input!) {
    update_payments(where: { id: { _in: $paymentIdList } }, _set: $payment) {
      returning {
        id
        amount
        created_at
        date
        exportedat
        jobid
        job {
          id
          ownerid
          ownr_co_nm
          ownr_fn
          ownr_ln
          owner {
            id
            ownr_co_nm
            ownr_fn
            ownr_ln
          }
          ro_number
        }
        memo
        payer
        paymentnum
        stripeid
        transactionid
        type
      }
    }
  }
`;

export const QUERY_JOB_PAYMENT_TOTALS = gql`
  query QUERY_JOB_PAYMENT_TOTALS($id: uuid!) {
    jobs_by_pk(id: $id) {
      id
      job_totals
      payments {
        id
        amount
        date
      }
    }
  }
`;

export const QUERY_PAYMENT_BY_ID = gql`
  query QUERY_PAYMENT_BY_ID($paymentId: uuid!) {
    payments_by_pk(id: $paymentId) {
      id
      amount
      created_at
      exportedat
      date
      jobid
      job {
        id
        ownerid
        ownr_co_nm
        ownr_fn
        ownr_ln
        owner {
          id
          ownr_co_nm
          ownr_fn
          ownr_ln
        }
        ro_number
      }
      memo
      payer
      paymentnum
      stripeid
      transactionid
      type
    }
  }
`;
