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

export const QUERY_ALL_PAYMENTS_PAGINATED = gql`
  query QUERY_ALL_PAYMENTS_PAGINATED(
    $search: String
    $offset: Int
    $limit: Int
    $order: [payments_order_by!]!
  ) {
    search_payments(
      args: { search: $search }
      offset: $offset
      limit: $limit
      order_by: $order
    ) {
      id
      created_at
      jobid
      paymentnum
      date
      job {
        id
        ro_number
        ownr_fn
        ownr_ln
        ownr_co_nm
      }
      transactionid
      memo
      type
      amount
      stripeid
      exportedat
      stripeid
      payer
    }
    search_payments_aggregate(args: { search: $search }) {
      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
        transactionid
        memo
        type
        amount
        stripeid
        exportedat
        stripeid
        payer
        paymentnum
        date
      }
    }
  }
`;

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

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