-- name: CreateSnakeGame :one
INSERT INTO games (id, board, current_turn, status, game_type, grid_width, grid_height, max_players, game_mode, snake_speed)
VALUES (?, ?, 0, ?, 'snake', ?, ?, 8, ?, ?)
RETURNING *;

-- name: GetSnakeGame :one
SELECT * FROM games WHERE id = ? AND game_type = 'snake';

-- name: UpdateSnakeGame :exec
UPDATE games
SET board = ?, status = ?, winner_user_id = ?, rematch_game_id = ?, score = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND game_type = 'snake';

-- name: DeleteSnakeGame :exec
DELETE FROM games WHERE id = ? AND game_type = 'snake';

-- name: GetActiveSnakeGames :many
SELECT * FROM games WHERE game_type = 'snake' AND status < 2 AND game_mode = 0;

-- name: CreateSnakePlayer :exec
INSERT INTO game_players (game_id, user_id, guest_player_id, nickname, color, slot)
VALUES (?, ?, ?, ?, ?, ?);

-- name: GetSnakePlayers :many
SELECT * FROM game_players WHERE game_id = ? ORDER BY slot;

-- name: GetUserActiveSnakeGames :many
SELECT
    g.id,
    g.status,
    g.grid_width,
    g.grid_height,
    g.updated_at
FROM games g
JOIN game_players gp_user ON g.id = gp_user.game_id AND gp_user.user_id = ?
WHERE g.game_type = 'snake' AND g.status < 3
ORDER BY g.updated_at DESC;
