-- name: CreateFlagsGame :one
INSERT INTO app_flags_games (uid, tags, question_amount)
VALUES ($1, $2, $3)
RETURNING game_id, game_seed;

-- name: GetFlagsGame :one
SELECT * FROM app_flags_games WHERE game_id = $1 LIMIT 1;

-- name: UpdateFlagsGame :exec
UPDATE app_flags_games
SET question_current = $1, last_activity = CURRENT_TIMESTAMP
WHERE game_id = $2;

-- name: UpdateQuestionCorrect :exec
UPDATE app_flags_games
SET questions_errors = (
    SELECT COALESCE(SUM(errors), 0)
    FROM app_flags_games_answers
    WHERE app_flags_games_answers.game_id = app_flags_games.game_id
)
WHERE app_flags_games.game_id = $1;

-- name: UpsertGameAnswer :exec
INSERT INTO app_flags_games_answers (game_id, question, errors)
VALUES ($1, $2, $3)
    ON CONFLICT (game_id, question)
DO UPDATE SET errors = app_flags_games_answers.errors + EXCLUDED.errors;

