-- name: CreateKnowledgeBase :one
INSERT INTO knowledge_bases (id, project_id, key, name, description)
VALUES ($1, $2, $3, $4, $5)
RETURNING *;

-- name: GetKnowledgeBase :one
SELECT * FROM knowledge_bases
WHERE id = $1 AND project_id = $2;

-- name: GetKnowledgeBaseByKey :one
SELECT * FROM knowledge_bases
WHERE project_id = $1 AND key = $2;

-- name: GetActiveReleaseByAlias :one
SELECT r.*
FROM releases r
JOIN knowledge_bases kb ON kb.id = r.knowledge_base_id
WHERE kb.project_id = sqlc.arg(project_id)
  AND kb.id = sqlc.arg(knowledge_base_id)
  AND r.alias = sqlc.arg(alias)
  AND r.status = 'active';
