
# ------------------------------------------------------------
# LIST ALL INVOICES (admin/debug)
# ------------------------------------------------------------
query listInvoices(
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  invoices(offset: $offset, limit: $limit) {
    id
    status

    vendorId
    businessId
    orderId

    paymentTerms
    invoiceNumber
    issueDate
    dueDate
    hub
    managerName
    vendorNumber
    roles
    charges
    otherCharges
    subtotal
    amount
    notes

    staffCount
    chargesCount

    disputedItems
    disputeReason
    disputeDetails

    vendor { 
      companyName 
      address
      email
      phone
    }

    business { 
      businessName 
      address
      phone
      email
    }
    order { 
      eventName
      hub
      deparment
      poReference
    }
  }
}

# ------------------------------------------------------------
# GET INVOICE BY ID
# ------------------------------------------------------------
query getInvoiceById($id: UUID!) @auth(level: USER) {
  invoice(id: $id) {
    id
    status

    vendorId
    businessId
    orderId

    paymentTerms
    invoiceNumber
    issueDate
    dueDate
    hub
    managerName
    vendorNumber
    roles
    charges
    otherCharges
    subtotal
    amount
    notes

    staffCount
    chargesCount

    disputedItems
    disputeReason
    disputeDetails

    vendor { 
      companyName 
      address
      email
      phone
    }

    business { 
      businessName 
      address
      phone
      email
    }
    order { 
      eventName
      hub
      deparment
      poReference
    }
  }
}

# ------------------------------------------------------------
# LIST INVOICES BY VENDOR
# ------------------------------------------------------------
query listInvoicesByVendorId(
  $vendorId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  invoices(
    where: { vendorId: { eq: $vendorId } }
    offset: $offset
    limit: $limit
    orderBy: { issueDate: DESC }
  ) {
    id
    status

    vendorId
    businessId
    orderId

    paymentTerms
    invoiceNumber
    issueDate
    dueDate
    hub
    managerName
    vendorNumber
    roles
    charges
    otherCharges
    subtotal
    amount
    notes

    staffCount
    chargesCount

    disputedItems
    disputeReason
    disputeDetails

    vendor { 
      companyName 
      address
      email
      phone
    }

    business { 
      businessName 
      address
      phone
      email
    }
    order { 
      eventName
      hub
      deparment
      poReference
    }
  }
}

# ------------------------------------------------------------
# LIST INVOICES BY BUSINESS
# ------------------------------------------------------------
query listInvoicesByBusinessId(
  $businessId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  invoices(
    where: { businessId: { eq: $businessId } }
    offset: $offset
    limit: $limit
    orderBy: { issueDate: DESC }
  ) {
    id
    status

    vendorId
    businessId
    orderId

    paymentTerms
    invoiceNumber
    issueDate
    dueDate
    hub
    managerName
    vendorNumber
    roles
    charges
    otherCharges
    subtotal
    amount
    notes

    staffCount
    chargesCount

    disputedItems
    disputeReason
    disputeDetails

    vendor { 
      companyName 
      address
      email
      phone
    }

    business { 
      businessName 
      address
      phone
      email
    }
    order { 
      eventName
      hub
      deparment
      poReference
    }
  }
}

# ------------------------------------------------------------
# LIST INVOICES BY ORDER
# ------------------------------------------------------------
query listInvoicesByOrderId(
  $orderId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  invoices(
    where: { orderId: { eq: $orderId } }
    offset: $offset
    limit: $limit
    orderBy: { issueDate: DESC }
  ) {
    id
    status

    vendorId
    businessId
    orderId

    paymentTerms
    invoiceNumber
    issueDate
    dueDate
    hub
    managerName
    vendorNumber
    roles
    charges
    otherCharges
    subtotal
    amount
    notes

    staffCount
    chargesCount

    disputedItems
    disputeReason
    disputeDetails

    vendor { 
      companyName 
      address
      email
      phone
    }

    business { 
      businessName 
      address
      phone
      email
    }
    order { 
      eventName
      hub
      deparment
      poReference
    }
  }
}

# ------------------------------------------------------------
# LIST INVOICES BY STATUS
# ------------------------------------------------------------
query listInvoicesByStatus(
  $status: InvoiceStatus!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  invoices(
    where: { status: { eq: $status } }
    offset: $offset
    limit: $limit
    orderBy: { dueDate: ASC }
  ) {
    id
    status

    vendorId
    businessId
    orderId

    paymentTerms
    invoiceNumber
    issueDate
    dueDate
    hub
    managerName
    vendorNumber
    roles
    charges
    otherCharges
    subtotal
    amount
    notes

    staffCount
    chargesCount

    disputedItems
    disputeReason
    disputeDetails

    vendor { 
      companyName 
      address
      email
      phone
    }

    business { 
      businessName 
      address
      phone
      email
    }
    order { 
      eventName
      hub
      deparment
      poReference
    }
  }
}

# ------------------------------------------------------------
# FILTER INVOICES (multi filters)
# NOTE: Timestamp filters use ge/le (NOT gte/lte)
# ------------------------------------------------------------
query filterInvoices(
  $vendorId: UUID
  $businessId: UUID
  $orderId: UUID
  $status: InvoiceStatus

  $issueDateFrom: Timestamp
  $issueDateTo: Timestamp

  $dueDateFrom: Timestamp
  $dueDateTo: Timestamp

  $offset: Int
  $limit: Int
) @auth(level: USER) {
  invoices(
    where: {
      vendorId: { eq: $vendorId }
      businessId: { eq: $businessId }
      orderId: { eq: $orderId }
      status: { eq: $status }

      issueDate: { ge: $issueDateFrom, le: $issueDateTo }
      dueDate: { ge: $dueDateFrom, le: $dueDateTo }
    }
    offset: $offset
    limit: $limit
    orderBy: { issueDate: DESC }
  ) {
    id
    status

    vendorId
    businessId
    orderId

    paymentTerms
    invoiceNumber
    issueDate
    dueDate
    hub
    managerName
    vendorNumber
    roles
    charges
    otherCharges
    subtotal
    amount
    notes

    staffCount
    chargesCount

    disputedItems
    disputeReason
    disputeDetails

    vendor { 
      companyName 
      address
      email
      phone
    }

    business { 
      businessName 
      address
      phone
      email
    }
    order { 
      eventName
      hub
      deparment
      poReference
    }
  }
}

# ------------------------------------------------------------
# OVERDUE INVOICES (dueDate < now and not PAID)
# NOTE: request.time works in @default; for filters, pass $now from client
# ------------------------------------------------------------
query listOverdueInvoices(
  $now: Timestamp!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  invoices(
    where: {
      dueDate: { lt: $now }
      status: { ne: PAID }
    }
    offset: $offset
    limit: $limit
    orderBy: { dueDate: ASC }
  ) {
    id
    status

    vendorId
    businessId
    orderId

    paymentTerms
    invoiceNumber
    issueDate
    dueDate
    hub
    managerName
    vendorNumber
    roles
    charges
    otherCharges
    subtotal
    amount
    notes

    staffCount
    chargesCount

    disputedItems
    disputeReason
    disputeDetails

    vendor { 
      companyName 
      address
      email
      phone
    }

    business { 
      businessName 
      address
      phone
      email
    }
    order { 
      eventName
      hub
      deparment
      poReference
    }
  }
}
