-- name: CreateRequirementArtifact :one
INSERT INTO requirement_artifacts (id, requirement_id, phase, version, content, note, source_message_id, created_by)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict;

-- name: ListRequirementArtifactsByRequirement :many
SELECT id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict
FROM requirement_artifacts
WHERE requirement_id = $1 AND ($2::text = '' OR phase = $2)
ORDER BY phase ASC, version ASC;

-- name: GetRequirementArtifactByVersion :one
SELECT id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict
FROM requirement_artifacts
WHERE requirement_id = $1 AND phase = $2 AND version = $3;

-- name: GetRequirementArtifactByID :one
SELECT id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict
FROM requirement_artifacts
WHERE id = $1;

-- name: GetMaxRequirementArtifactVersion :one
SELECT COALESCE(MAX(version), 0)::int FROM requirement_artifacts
WHERE requirement_id = $1 AND phase = $2;

-- name: ApproveRequirementArtifact :one
UPDATE requirement_artifacts SET verdict = $2 WHERE id = $1
RETURNING id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict;
