-- name: ListProjectEntries :many
SELECT id, name, status, created_time, author_id FROM project
WHERE scope_id = ?
ORDER BY status, created_time;

-- name: GetProject :one
SELECT * FROM project WHERE id = ?;

-- name: GetProjectRequirement :one
SELECT * FROM project_requirement WHERE id = ?;

-- name: ListProjectRequirementsByProjectID :many
SELECT * FROM project_requirement WHERE project_id = ?;

-- name: ListProjectRequirementStats :many
SELECT prs.required, s.id, s.name, s.weight FROM project_requirement_stat prs
RIGHT JOIN stat s ON s.id = prs.stat_id
WHERE project_requirement_id = ?;

-- name: InsertProject :execresult
INSERT INTO project (scope_id, author_id, name, status, description, created_time)
VALUES (?, ?, ?, ?, ?, ?);

-- name: UpdateProject :exec
UPDATE project
SET name = ?,
    status = ?,
    description = ?
WHERE id = ?;

-- name: DeleteProject :exec
DELETE FROM project WHERE id = ?;

-- name: InsertProjectRequirement :execresult
INSERT INTO project_requirement (scope_id, project_id, name, status, description)
VALUES (?, ?, ?, ?, ?);

-- name: UpdateProjectRequirement :exec
UPDATE project_requirement
SET name = ?,
    status = ?,
    description = ?
WHERE id = ?;

-- name: DeleteProjectRequirement :exec
DELETE FROM project_requirement WHERE id = ?;

-- name: DeleteAllProjectRequirements :exec
DELETE FROM project_requirement WHERE project_id = ?;

-- name: ReplaceProjectRequirementStat :exec
REPLACE INTO project_requirement_stat (project_requirement_id, stat_id, required)
VALUES (?, ?, ?);

-- name: DeleteProjectRequirementStat :exec
DELETE FROM project_requirement_stat WHERE project_requirement_id = ? AND stat_id = ?;

-- name: DeleteAllProjectRequirementStats :exec
DELETE FROM project_requirement_stat WHERE project_requirement_id = ?;

-- name: DeleteAllProjectRequirementStatsByStat :exec
DELETE FROM project_requirement_stat WHERE stat_id = ?;

