-- name: GetEntitlementSetByID :one
SELECT * FROM core.entitlement_sets
WHERE set_id = $1;

-- name: ListEntitlementSets :many
SELECT * FROM core.entitlement_sets
ORDER BY name ASC;

-- name: ListActiveEntitlementSets :many
SELECT * FROM core.entitlement_sets
WHERE is_active = TRUE
ORDER BY name ASC;

-- name: CreateEntitlementSet :one
-- `key` is the optional declarative address (entity-keys §3), root-scoped.
-- The operator create form does not collect one, so a UI-created set leaves
-- it NULL; a seed or a configuration loader supplies it.
INSERT INTO core.entitlement_sets (name, description, is_active, key)
VALUES ($1, $2, $3, sqlc.narg(key))
RETURNING *;

-- name: GetEntitlementSetByKey :one
-- Root key resolver (entity-keys §5). uq_entitlement_sets_key guarantees at
-- most one row matches.
SELECT * FROM core.entitlement_sets
WHERE key = $1;

-- name: UpdateEntitlementSet :one
UPDATE core.entitlement_sets
SET name = $2, description = $3, is_active = $4
WHERE set_id = $1
RETURNING *;

-- name: AnyActiveEntitlementSet :one
-- Setup-checklist existence probe: the whole-table answer, not a lookup.
SELECT EXISTS(SELECT 1 FROM core.entitlement_sets WHERE is_active = TRUE);
