-- name: CreateTask :one
INSERT INTO tasks (id, owner_id, title, description, status, priority, due_date, project_id, parent_id, sort_order, recurrence_rule)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
RETURNING *;

-- name: GetTaskByID :one
SELECT * FROM tasks
WHERE id = $1 AND owner_id = $2 AND deleted_at IS NULL;

-- name: GetTaskByIDForUpdate :one
SELECT * FROM tasks
WHERE id = $1 AND owner_id = $2 AND deleted_at IS NULL
FOR UPDATE;

-- name: ListTasks :many
SELECT t.* FROM tasks t
WHERE t.owner_id = @owner_id
  AND t.deleted_at IS NULL
  AND t.parent_id IS NULL
  AND (sqlc.narg('status')::TEXT IS NULL OR t.status = sqlc.narg('status')::TEXT)
  AND (sqlc.narg('priority')::TEXT IS NULL OR t.priority = sqlc.narg('priority')::TEXT)
  AND (sqlc.narg('project_id')::UUID IS NULL OR t.project_id = sqlc.narg('project_id')::UUID)
  AND (sqlc.narg('due_from')::TIMESTAMPTZ IS NULL OR t.due_date >= sqlc.narg('due_from')::TIMESTAMPTZ)
  AND (sqlc.narg('due_to')::TIMESTAMPTZ IS NULL OR t.due_date <= sqlc.narg('due_to')::TIMESTAMPTZ)
  AND (
    sqlc.narg('cursor_time')::TIMESTAMPTZ IS NULL
    OR (t.created_at, t.id) < (sqlc.narg('cursor_time')::TIMESTAMPTZ, sqlc.narg('cursor_id')::UUID)
  )
ORDER BY t.created_at DESC, t.id DESC
LIMIT @lim;

-- name: ListTasksByDueDate :many
SELECT t.* FROM tasks t
WHERE t.owner_id = @owner_id
  AND t.deleted_at IS NULL
  AND t.parent_id IS NULL
  AND (sqlc.narg('status')::TEXT IS NULL OR t.status = sqlc.narg('status')::TEXT)
  AND (sqlc.narg('priority')::TEXT IS NULL OR t.priority = sqlc.narg('priority')::TEXT)
  AND (sqlc.narg('project_id')::UUID IS NULL OR t.project_id = sqlc.narg('project_id')::UUID)
  AND (sqlc.narg('due_from')::TIMESTAMPTZ IS NULL OR t.due_date >= sqlc.narg('due_from')::TIMESTAMPTZ)
  AND (sqlc.narg('due_to')::TIMESTAMPTZ IS NULL OR t.due_date <= sqlc.narg('due_to')::TIMESTAMPTZ)
ORDER BY COALESCE(t.due_date, '9999-12-31'::timestamptz) ASC NULLS LAST, t.id ASC
LIMIT @lim;

-- name: ListTasksByPriority :many
SELECT t.* FROM tasks t
WHERE t.owner_id = @owner_id
  AND t.deleted_at IS NULL
  AND t.parent_id IS NULL
  AND (sqlc.narg('status')::TEXT IS NULL OR t.status = sqlc.narg('status')::TEXT)
  AND (sqlc.narg('priority')::TEXT IS NULL OR t.priority = sqlc.narg('priority')::TEXT)
  AND (sqlc.narg('project_id')::UUID IS NULL OR t.project_id = sqlc.narg('project_id')::UUID)
  AND (sqlc.narg('due_from')::TIMESTAMPTZ IS NULL OR t.due_date >= sqlc.narg('due_from')::TIMESTAMPTZ)
  AND (sqlc.narg('due_to')::TIMESTAMPTZ IS NULL OR t.due_date <= sqlc.narg('due_to')::TIMESTAMPTZ)
ORDER BY CASE t.priority
  WHEN 'critical' THEN 1
  WHEN 'high' THEN 2
  WHEN 'medium' THEN 3
  WHEN 'low' THEN 4
  ELSE 5
END ASC, t.created_at DESC, t.id DESC
LIMIT @lim;

-- name: ListTasksWithTag :many
SELECT DISTINCT t.* FROM tasks t
JOIN task_tags tt ON tt.task_id = t.id
WHERE t.owner_id = @owner_id
  AND t.deleted_at IS NULL
  AND t.parent_id IS NULL
  AND tt.tag_id = ANY(@tag_ids)
  AND (sqlc.narg('status')::TEXT IS NULL OR t.status = sqlc.narg('status')::TEXT)
  AND (sqlc.narg('project_id')::UUID IS NULL OR t.project_id = sqlc.narg('project_id')::UUID)
ORDER BY t.created_at DESC, t.id DESC
LIMIT @lim;

-- name: UpdateTask :one
UPDATE tasks
SET title = COALESCE(sqlc.narg('title'), title),
    description = COALESCE(sqlc.narg('description'), description),
    status = COALESCE(sqlc.narg('status'), status),
    priority = COALESCE(sqlc.narg('priority'), priority),
    due_date = sqlc.narg('due_date'),
    project_id = sqlc.narg('project_id'),
    sort_order = COALESCE(sqlc.narg('sort_order'), sort_order),
    recurrence_rule = sqlc.narg('recurrence_rule'),
    updated_at = now()
WHERE id = @id AND owner_id = @owner_id AND deleted_at IS NULL
RETURNING *;

-- name: SoftDeleteTask :exec
UPDATE tasks SET deleted_at = now(), updated_at = now()
WHERE id = $1 AND owner_id = $2 AND deleted_at IS NULL;

-- name: HardDeleteTask :exec
DELETE FROM tasks WHERE id = $1 AND owner_id = $2;

-- name: MarkTaskComplete :one
UPDATE tasks
SET status = 'done', completed_at = now(), updated_at = now()
WHERE id = $1 AND owner_id = $2 AND deleted_at IS NULL
RETURNING *;

-- name: MarkTaskUncomplete :one
UPDATE tasks
SET status = COALESCE(sqlc.narg('new_status'), 'todo'), completed_at = NULL, updated_at = now()
WHERE id = $1 AND owner_id = $2 AND deleted_at IS NULL
RETURNING *;

-- name: ListSubtasks :many
SELECT * FROM tasks
WHERE parent_id = $1 AND owner_id = $2 AND deleted_at IS NULL
ORDER BY sort_order ASC, created_at ASC;

-- name: CountSubtasksByStatus :one
SELECT
  COUNT(*) FILTER (WHERE status = 'done') AS done_count,
  COUNT(*) AS total_count
FROM tasks
WHERE parent_id = $1 AND deleted_at IS NULL;

-- name: ListTasksWithRecurrence :many
SELECT * FROM tasks
WHERE owner_id = $1 AND deleted_at IS NULL
  AND recurrence_rule IS NOT NULL
  AND parent_id IS NULL;
