-- name: CreateIssue :one
INSERT INTO issues (id, project_id, requirement_id, number, title, description, assignee_id, created_by, workspace_id, parent_id, priority)
VALUES (
  $1, $2, $3,
  (SELECT COALESCE(MAX(number), 0) + 1 FROM issues WHERE project_id = $2),
  $4, $5, $6, $7, $8, $9, $10
)
RETURNING id, project_id, requirement_id, number, title, description, status,
          assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority;

-- name: GetIssueByNumber :one
SELECT id, project_id, requirement_id, number, title, description, status,
       assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
FROM issues
WHERE project_id = $1 AND number = $2;

-- name: GetIssueByID :one
SELECT id, project_id, requirement_id, number, title, description, status,
       assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
FROM issues
WHERE id = $1;

-- name: ListIssues :many
SELECT id, project_id, requirement_id, number, title, description, status,
       assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
FROM issues
WHERE project_id = $1
  AND (sqlc.narg('status')::text  IS NULL OR status = sqlc.narg('status'))
  AND (
    sqlc.narg('requirement_filter')::text IS NULL
    OR (sqlc.narg('requirement_filter') = 'none' AND requirement_id IS NULL)
    OR (sqlc.narg('requirement_filter') = 'id'   AND requirement_id = sqlc.narg('requirement_id'))
  )
  AND (sqlc.narg('assignee_id')::uuid IS NULL OR assignee_id = sqlc.narg('assignee_id'))
ORDER BY number DESC;

-- name: ListIssuesByRequirement :many
SELECT id, project_id, requirement_id, number, title, description, status,
       assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
FROM issues
WHERE requirement_id = $1
ORDER BY number DESC;

-- name: UpdateIssueCore :one
UPDATE issues
SET title          = $2,
    description    = $3,
    requirement_id = $4,
    workspace_id   = $5,
    updated_at     = now()
WHERE id = $1
RETURNING id, project_id, requirement_id, number, title, description, status,
          assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority;

-- name: UpdateIssueAssignee :one
UPDATE issues
SET assignee_id = $2, updated_at = now()
WHERE id = $1
RETURNING id, project_id, requirement_id, number, title, description, status,
          assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority;

-- name: UpdateIssuePriority :one
UPDATE issues
SET priority = $2, updated_at = now()
WHERE id = $1
RETURNING id, project_id, requirement_id, number, title, description, status,
          assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority;

-- name: CloseIssue :exec
UPDATE issues SET status = 'closed', closed_at = now(), updated_at = now()
WHERE id = $1 AND status = 'open';

-- name: ReopenIssue :exec
UPDATE issues SET status = 'open', closed_at = NULL, updated_at = now()
WHERE id = $1 AND status = 'closed';
