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

export const INSERT_INVENTORY_AND_CREDIT = gql`
  mutation INSERT_INVENTORY_AND_CREDIT(
    $inv: inventory_insert_input!
    $cm: bills_insert_input!
    $pol: parts_orders_insert_input!
    $joblineId: uuid!
    $joblineStatus: String
  ) {
    insert_inventory_one(object: $inv) {
      id
    }
    insert_bills_one(object: $cm) {
      id
    }
    insert_parts_orders_one(object: $pol) {
      id
    }
    update_joblines_by_pk(pk_columns: { id: $joblineId }, _set: { status: $joblineStatus }) {
      id
      status
    }
  }
`;
export const UPDATE_INVENTORY_LINES = gql`
  mutation UPDATE_INVENTORY_LINES($InventoryIds: [uuid!]!, $consumedbybillid: uuid!) {
    update_inventory(where: { id: { _in: $InventoryIds } }, _set: { consumedbybillid: $consumedbybillid }) {
      affected_rows
    }
  }
`;

export const QUERY_OUTSTANDING_INVENTORY = gql`
  query QUERY_OUTSTANDING_INVENTORY {
    inventory(where: { consumedbybillid: { _is_null: true } }, order_by: { line_desc: asc }) {
      id
      actual_cost
      actual_price
      quantity
      billlineid
      line_desc
      comment
      manualinvoicenumber
      manualvendor
      consumedbybillid
      billline {
        bill {
          invoice_number
          vendor {
            id
            name
          }
        }
      }
    }
  }
`;

export const QUERY_INVENTORY_PAGINATED = gql`
  query QUERY_INVENTORY_PAGINATED(
    $search: String
    $offset: Int
    $limit: Int
    $order: [inventory_order_by!]
    $consumedIsNull: Boolean
  ) {
    search_inventory(
      args: { search: $search }
      offset: $offset
      limit: $limit
      order_by: $order
      where: { consumedbybillid: { _is_null: $consumedIsNull } }
    ) {
      id
      line_desc
      actual_price
      actual_cost
      comment
      manualinvoicenumber
      manualvendor
      consumedbybillid
      bill {
        id
        invoice_number
        job {
          ro_number
          id
        }
      }
      billline {
        id
        bill {
          id
          invoice_number
          job {
            id
            v_make_desc
            v_model_desc
            v_model_yr
          }
          vendor {
            id
            name
          }
        }
      }
    }
    search_inventory_aggregate(args: { search: $search }, where: { consumedbybillid: { _is_null: $consumedIsNull } }) {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;

export const DELETE_INVENTORY_LINE = gql`
  mutation DELETE_INVENTORY_LINE($lineId: uuid!) {
    delete_inventory_by_pk(id: $lineId) {
      id
    }
  }
`;

export const INSERT_INVENTORY_LINE = gql`
  mutation INSERT_INVENTORY_LINE($inventoryItem: inventory_insert_input!) {
    insert_inventory_one(object: $inventoryItem) {
      id
      line_desc
      consumedbybillid
      billlineid
      actual_price
      actual_cost
      comment
      manualinvoicenumber
      manualvendor
      bill {
        invoice_number
      }
    }
  }
`;

export const UPDATE_INVENTORY_LINE = gql`
  mutation UPDATE_INVENTORY_LINE($inventoryId: uuid!, $inventoryItem: inventory_set_input!) {
    update_inventory_by_pk(pk_columns: { id: $inventoryId }, _set: $inventoryItem) {
      id
      line_desc
      consumedbybillid
      billlineid
      actual_price
      actual_cost
      comment
      manualinvoicenumber
      manualvendor
      bill {
        invoice_number
      }
    }
  }
`;
