# ------------------------------------------------------------
# LIST ALL (admin/debug)
# ------------------------------------------------------------
query listRecentPayments(
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  recentPayments(offset: $offset, limit: $limit) {
    id
    workedTime
    status
    staffId
    applicationId
    invoiceId
    createdAt
    updatedAt
    createdBy

    application {
      checkInTime
      checkOutTime

      shiftRole {
        hours
        totalValue
        startTime
        endTime

        role {
          name
          costPerHour
        }

        shift {
          title
          date
          location
          locationAddress
          description
        }
      }
    }

    invoice {
      status
      invoiceNumber
      issueDate

      business { id businessName }
      vendor { id companyName }
      order { id eventName  }
    }
  }
}

# ------------------------------------------------------------
# GET BY ID
# ------------------------------------------------------------
query getRecentPaymentById($id: UUID!) @auth(level: USER) {
  recentPayment(id: $id) {
    id
    workedTime
    status
    staffId
    applicationId
    invoiceId
    createdAt
    updatedAt
    createdBy

    application {
      checkInTime
      checkOutTime

      shiftRole {
        hours
        totalValue
        startTime
        endTime

        role {
          name
          costPerHour
        }

        shift {
          title
          date
          location
          locationAddress
          description
        }
      }
    }

    invoice {
      status
      invoiceNumber
      issueDate

      business { id businessName }
      vendor { id companyName }
      order { id eventName  }
    }
  }
}

# ------------------------------------------------------------
# LIST BY STAFF (Staff view)
# ------------------------------------------------------------
query listRecentPaymentsByStaffId(
  $staffId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  recentPayments(
    where: { staffId: { eq: $staffId } }
    offset: $offset
    limit: $limit
    orderBy: { createdAt: DESC }
  ) {
    id
    workedTime
    status
    staffId
    applicationId
    invoiceId
    createdAt
    updatedAt
    createdBy

    application {
      id
      status
      shiftRole {
        startTime
        endTime
        hours
        totalValue

        role { id name costPerHour }

        shift {
          id
          title
          date
          locationAddress
        }
      }
    }

    invoice {
      id
      invoiceNumber
      status
      issueDate
      dueDate
      amount

      business { id businessName }
      vendor { id companyName }
      order { id eventName location }
    }
  }
}

# ------------------------------------------------------------
# LIST BY APPLICATION (debug / trace)
# ------------------------------------------------------------
query listRecentPaymentsByApplicationId(
  $applicationId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  recentPayments(
    where: { applicationId: { eq: $applicationId } }
    offset: $offset
    limit: $limit
  ) {
    id
    workedTime
    status
    staffId
    applicationId
    invoiceId
    createdAt
    updatedAt
    createdBy

    application {
      id
      status
      shiftRole {
        hours
        totalValue
        role { id name costPerHour }
        shift { id title date locationAddress }
      }
    }

    invoice {
      id
      invoiceNumber
      status
      amount
      business { id businessName }
      vendor { id companyName }
      order { id eventName location }
    }
  }
}

# ------------------------------------------------------------
# LIST BY INVOICE (Vendor/Business can see all payments for invoice)
# ------------------------------------------------------------
query listRecentPaymentsByInvoiceId(
  $invoiceId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  recentPayments(
    where: { invoiceId: { eq: $invoiceId } }
    offset: $offset
    limit: $limit
  ) {
    id
    workedTime
    status
    staffId
    applicationId
    invoiceId
    createdAt
    updatedAt
    createdBy

    application {
      id
      staffId
      shiftRole {
        hours
        totalValue
        role { id name costPerHour }
        shift { id title date locationAddress }
      }
    }

    invoice {
      id
      invoiceNumber
      status
      amount
      business { id businessName }
      vendor { id companyName }
      order { id eventName location }
    }
  }
}

# ------------------------------------------------------------
# FILTER BY STATUS (common)
# ------------------------------------------------------------
query listRecentPaymentsByStatus(
  $status: RecentPaymentStatus!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  recentPayments(
    where: { status: { eq: $status } }
    offset: $offset
    limit: $limit
    orderBy: { createdAt: DESC }
  ) {
    id
    workedTime
    status
    staffId
    applicationId
    invoiceId
    createdAt
    updatedAt
    createdBy

    application {
      id
      shiftRole {
        hours
        totalValue
        role { id name costPerHour }
        shift { id title date locationAddress }
      }
    }

    invoice {
      id
      invoiceNumber
      status
      amount
      business { id businessName }
      vendor { id companyName }
      order { id eventName location }
    }
  }
}

# ------------------------------------------------------------
# BY BUSINESS (2-step)
# Step 2: pass invoiceIds (from query: listInvoicesByBusinessId)
# ------------------------------------------------------------
query listRecentPaymentsByInvoiceIds(
  $invoiceIds: [UUID!]!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  recentPayments(
    where: { invoiceId: { in: $invoiceIds } }
    offset: $offset
    limit: $limit
    orderBy: { createdAt: DESC }
  ) {
    id
    workedTime
    status
    staffId
    applicationId
    invoiceId
    createdAt
    updatedAt
    createdBy

    application {
      id
      shiftRole {
        hours
        totalValue
        role { id name costPerHour }
        shift { id title date locationAddress }
      }
    }

    invoice {
      id
      invoiceNumber
      status
      amount
      business { id businessName }
      vendor { id companyName }
      order { id eventName location }
    }
  }
}

# ------------------------------------------------------------
# LIST BY BUSINESS ID (direct)
# ------------------------------------------------------------
query listRecentPaymentsByBusinessId(
  $businessId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  recentPayments(
    where: {
      invoice: {
        businessId: { eq: $businessId }
      }
    }
    offset: $offset
    limit: $limit
    orderBy: { createdAt: DESC }
  ) {
    id
    workedTime
    status
    staffId
    applicationId
    invoiceId
    createdAt
    updatedAt
    createdBy

    application {
      id
      staffId
      checkInTime
      checkOutTime
      shiftRole {
        startTime
        endTime
        hours
        totalValue

        role { id name costPerHour }

        shift {
          id
          title
          date
          location
          locationAddress
          description
        }
      }
    }

    invoice {
      id
      invoiceNumber
      status
      issueDate
      dueDate
      amount

      business { id businessName }
      vendor { id companyName }
      order { id eventName location }
    }
  }
}
