-- name: GetConfig :one
SELECT ticket_key, ticket_id
FROM appconfig;

-- name: SetNewConfig :one
UPDATE appconfig
SET ticket_id = ticket_id + 1
RETURNING *;

-- name: CreateTicket :one
INSERT INTO tickets (
    key, channelID, title
) VALUES (
    $1, $2, $3
    )
    RETURNING *;

-- name: UpdateTicketFolder :one
UPDATE tickets
SET folder = $1, updated_at = $2
WHERE channelID = $3
RETURNING *;

-- name: UpdateTicketProjectGit :one
UPDATE tickets
SET project_git = $1, updated_at = $2
WHERE channelID = $3
RETURNING *;

-- name: UpdateTicketBuildGit :one
UPDATE tickets
SET build_git = $1, updated_at = $2
WHERE channelID = $3
RETURNING *;

-- name: ListTickets :many
SELECT * FROM tickets WHERE deleted_at IS NULL;

-- name: ListTicketsWithDeleted :many
SELECT * FROM tickets;

-- name: GetTicketByID :one
SELECT * FROM tickets WHERE id = $1;

-- name: GetTicketByChannelID :one
SELECT * FROM tickets WHERE channelID = $1;

-- name: UpdateTicketByID :exec
UPDATE tickets SET project_git = $1, build_git = $2, folder = $3 WHERE id = $4;

-- name: DeleteTicketByID :exec
UPDATE tickets SET deleted_at = current_timestamp WHERE id = $1;

-- name: DeleteTicketByKey :exec
UPDATE tickets SET deleted_at = current_timestamp WHERE key = $1;

-- name: InsertTask :one
INSERT INTO tasks (
    creator, creator_link, description
) VALUES (
    $1, $2, $3
    )
    RETURNING *;

-- name: UpdateTaskWithMessageID :exec
UPDATE tasks
SET messageID = $1
WHERE id = $2;

-- name: StartTask :one
UPDATE tasks
SET updated_at = $1, assignee = $2
WHERE messageID = $3
RETURNING *;

-- name: CloseTask :one
UPDATE tasks
SET deleted_at = $1, assignee = $2
WHERE messageID = $3
RETURNING *;

-- name: GetTaskByMessage :one
SELECT * FROM tasks WHERE messageID = $1;

-- name: ListTasksByCreator :many
SELECT * FROM tasks WHERE creator_link = $1 AND deleted_at is NULL;

-- name: GetTaskByID :one
SELECT * FROM tasks WHERE id = $1;