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

export const INSERT_NEW_BILL = gql`
  mutation INSERT_NEW_BILL($bill: [bills_insert_input!]!) {
    insert_bills(objects: $bill) {
      returning {
        id
        invoice_number
      }
    }
  }
`;

export const DELETE_BILL = gql`
  mutation DELETE_BILL($billId: uuid!) {
    delete_bills_by_pk(id: $billId) {
      id
    }
  }
`;

export const QUERY_ALL_BILLS_PAGINATED = gql`
  query QUERY_ALL_BILLS_PAGINATED($offset: Int, $limit: Int, $order: [bills_order_by!]!, $where: bills_bool_exp) {
    bills(offset: $offset, limit: $limit, order_by: $order, where: $where) {
      id
      vendorid
      vendor {
        id
        name
      }
      federal_tax_rate
      local_tax_rate
      state_tax_rate
      is_credit_memo
      total
      invoice_number
      date
      isinhouse
      exported
      job {
        id
        ro_number
      }
    }
    bills_aggregate(where: $where) {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;

export const QUERY_PARTS_BILLS_BY_JOBID = gql`
  query QUERY_PARTS_BILLS_BY_JOBID($jobid: uuid!) {
    parts_orders(where: { jobid: { _eq: $jobid } }, order_by: { order_date: desc }) {
      id
      vendor {
        id
        name
        email
      }
      tasks {
        id
        due_date
      }
      order_date
      deliver_by
      return
      returnfrombill
      orderedby
      parts_order_lines {
        id
        act_price
        db_price
        line_desc
        oem_partno
        status
        line_remarks
        quantity
        job_line_id
        part_type
        cost
        cm_received
        jobline {
          id
          part_type
        }
        backordered_eta
        backordered_on
      }
      order_number
      comments
      user_email
      bill {
        id
        invoice_number
      }
    }
    parts_dispatch(where: { jobid: { _eq: $jobid } }) {
      id
      dispatched_at
      dispatched_by
      employeeid
      number
      parts_dispatch_lines {
        joblineid
        id
        quantity
        accepted_at
        jobline {
          id
          line_desc
        }
      }
    }
    bills(where: { jobid: { _eq: $jobid } }, order_by: { date: desc }) {
      id
      vendorid
      vendor {
        id
        name
        email
      }
      total
      invoice_number
      date
      federal_tax_rate
      state_tax_rate
      local_tax_rate
      is_credit_memo
      isinhouse
      exported
      billlines {
        actual_price
        quantity
        actual_cost
        cost_center
        id
        joblineid
        line_desc
        applicable_taxes
        deductedfromlbr
        lbr_adjustment
        jobline {
          oem_partno
          part_type
        }
      }
    }
  }
`;

export const QUERY_BILL_BY_PK = gql`
  query QUERY_BILL_BY_PK($billid: uuid!) {
    bills_by_pk(id: $billid) {
      due_date
      exported
      exported_at
      id
      invoice_number
      date
      is_credit_memo
      jobid
      total
      updated_at
      vendorid
      local_tax_rate
      state_tax_rate
      federal_tax_rate
      isinhouse
      inventories {
        id
        line_desc
      }
      vendor {
        id
        name
        discount
      }
      billlines {
        id
        line_desc
        actual_price
        actual_cost
        cost_center
        quantity
        joblineid
        inventories {
          id
        }
        jobline {
          alt_partno
          oem_partno
          part_type
        }
        applicable_taxes
        deductedfromlbr
        lbr_adjustment
      }
      documents {
        id
        key
        name
        type
        size
      }
    }
  }
`;

export const UPDATE_BILL = gql`
  mutation UPDATE_BILL($billId: uuid!, $bill: bills_set_input!) {
    update_bills(where: { id: { _eq: $billId } }, _set: $bill) {
      returning {
        id
        exported
        exported_at
      }
    }
  }
`;

export const UPDATE_BILLS = gql`
  mutation UPDATE_BILLS($billIdList: [uuid!]!, $bill: bills_set_input!) {
    update_bills(where: { id: { _in: $billIdList } }, _set: $bill) {
      returning {
        id
        exported
        exported_at
      }
    }
  }
`;

export const CHECK_BILL_INVOICE_NUMBER = gql`
  query CHECK_BILL_INVOICE_NUMBER($invoice_number: String!, $vendorid: uuid!) {
    bills_aggregate(where: { _and: { invoice_number: { _ilike: $invoice_number }, vendorid: { _eq: $vendorid } } }) {
      aggregate {
        count
      }
      nodes {
        id
      }
    }
  }
`;
