-- Writes to core.pool_provisions go through the conferral functions
-- (queries/conferral.sql) alone; core_writer holds no direct DML on this
-- table (migration 00005 enclosure). Only read paths live here.

-- name: GetPoolProvisionsByPoolID :many
SELECT * FROM core.pool_provisions
WHERE pool_id = $1
ORDER BY created_at ASC;

-- name: GetActivePoolProvisionsByPoolID :many
SELECT * FROM core.pool_provisions
WHERE pool_id = $1 AND status = 'active'
ORDER BY created_at ASC;

-- name: GetPoolProvisionByGrantID :one
SELECT * FROM core.pool_provisions
WHERE grant_id = $1;

-- name: GetPoolProvisionByProvisionID :one
SELECT * FROM core.pool_provisions
WHERE provision_id = $1;

-- name: GetPoolProvisionBySubscriptionID :one
SELECT * FROM core.pool_provisions
WHERE subscription_id = $1;

-- name: GetLivePoolProvisionsBySubscriptionID :many
-- Live (non-ended) provisions of a subscription, one per product. Reconcile
-- diffs these against the subscription's desired-by-product line items.
SELECT * FROM core.pool_provisions
WHERE subscription_id = $1 AND status <> 'ended'
ORDER BY created_at ASC;

-- name: GetLivePoolProvisionsByProductID :many
-- Live (non-ended) provisions delivering a product. Tier add/remove aligns
-- each of these to the product's new conferral shape.
SELECT * FROM core.pool_provisions
WHERE product_id = $1 AND status <> 'ended'
ORDER BY created_at ASC;
