-- name: GetUser :one
SELECT id, username FROM users WHERE id = ? LIMIT 1;

-- name: GetUsers :many
SELECT * FROM users ORDER BY name;

-- name: GetUserPassword :one
SELECT id, password FROM users WHERE username = ? LIMIT 1;

-- name: CreateUser :one
INSERT INTO users (
	id,
	username
) VALUES (
    ?, ?
) RETURNING id, username;

-- name: UpdateUser :one
UPDATE users
	set username = ?
WHERE id = ?
RETURNING id, username;

-- name: UpdateUserPassword :one
UPDATE users
	set password = ?
WHERE id = ?
RETURNING id, username;

-- name: GetSession :one
SELECT * FROM sessions WHERE key = ? LIMIT 1;

-- name: CreateSession :one
INSERT INTO sessions (
	key,
	user_id,
	created_at
) VALUES (?, ?, ?)
RETURNING *;

-- name: DeleteSession :exec
DELETE FROM sessions WHERE key = ? LIMIT 1 RETURNING *;

-- name: TaskByID :one
SELECT * FROM tasks WHERE id = ? LIMIT 1;

-- name: AllTasks :many
SELECT * FROM tasks;

-- name: TasksForUser :many
SELECT * FROM tasks WHERE assigned_to = ?;

-- name: UserTasks :many
SELECT * FROM tasks WHERE created_by = ?;


-- name: CategoriesOf :many
SELECT c.* FROM categories c
	JOIN task_categories tc ON tc.category_id = c.id
WHERE tc.task_id IN (sqlc.slice('ids'));

-- name: TaskCategoriesOf :many
SELECT tc.* FROM task_categories tc
	JOIN tasks t ON tc.task_id = t.id
WHERE tc.task_id IN (sqlc.slice('ids'));

-- name: AddTaskCategory :one
INSERT INTO task_categories (task_id, category_id)
VALUES (?, ?)
RETURNING *;

-- name: CreateTask :one
INSERT INTO tasks (
	name,
	description,
	created_by,
	assigned_to,
	created_at,
	deadline,
	completed
) VALUES (?, ?, ?, ?, ?, ?, FALSE)
RETURNING *;

-- name: CompleteTask :one
UPDATE tasks SET completed = TRUE WHERE id = ? RETURNING *;

-- name: UpdateTask :one
UPDATE tasks SET
	name = ?,
	description = ?
WHERE id = ?
RETURNING *;

-- name: DeleteTask :exec
DELETE FROM tasks WHERE id = ? LIMIT 1 RETURNING *;

-- name: CreateCategory :one
INSERT INTO categories (
	name, description, created_by, created_at
) VALUES (?, ?, ?, ?)
RETURNING *;

-- name: ListCategories :many
SELECT * FROM categories;

-- name: UpdateCategory :one
UPDATE categories SET
	name = ?,
	description = ?
WHERE id = ?
RETURNING *;
-- name: DeleteCategory :one
DELETE FROM categories WHERE id = ? LIMIT 1 RETURNING *;

-- name: UserCategories :many
SELECT * FROM categories WHERE created_by = ?;

