-- name: CreateEnetpulseSport :one
INSERT INTO enetpulse_sports (
    sport_id,
    name,
    updates_count,
    last_updated_at,
    status,
    updated_at
) VALUES (
    $1, $2, $3, $4, $5, NOW()
)
ON CONFLICT (sport_id) DO UPDATE
SET
    name = EXCLUDED.name,
    updates_count = EXCLUDED.updates_count,
    last_updated_at = EXCLUDED.last_updated_at,
    status = EXCLUDED.status,
    updated_at = NOW()
RETURNING *;

-- name: GetAllEnetpulseSports :many
SELECT
    id,
    sport_id,
    name,
    updates_count,
    last_updated_at,
    status,
    created_at,
    updated_at
FROM enetpulse_sports
ORDER BY name;

-- name: CreateEnetpulseTournamentTemplate :one
INSERT INTO enetpulse_tournament_templates (
    template_id,
    name,
    sport_fk,
    gender,
    updates_count,
    last_updated_at,
    status,
    updated_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, NOW())
ON CONFLICT (template_id) DO UPDATE
SET
    name = EXCLUDED.name,
    sport_fk = EXCLUDED.sport_fk,
    gender = EXCLUDED.gender,
    updates_count = EXCLUDED.updates_count,
    last_updated_at = EXCLUDED.last_updated_at,
    status = EXCLUDED.status,
    updated_at = NOW()
RETURNING *;

-- name: GetAllEnetpulseTournamentTemplates :many
SELECT 
    id,
    template_id,
    name,
    sport_fk,
    gender,
    updates_count,
    last_updated_at,
    status,
    created_at,
    updated_at
FROM enetpulse_tournament_templates
ORDER BY name;

-- name: CreateEnetpulseTournament :one
INSERT INTO enetpulse_tournaments (
    tournament_id,
    name,
    tournament_template_fk,
    updates_count,
    last_updated_at,
    status
) VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (tournament_id) DO UPDATE
SET
    name = EXCLUDED.name,
    tournament_template_fk = EXCLUDED.tournament_template_fk,
    updates_count = EXCLUDED.updates_count,
    last_updated_at = EXCLUDED.last_updated_at,
    status = EXCLUDED.status
RETURNING *;

-- name: GetAllEnetpulseTournaments :many
SELECT *
FROM enetpulse_tournaments
ORDER BY created_at DESC;

-- name: CreateEnetpulseTournamentStage :one
INSERT INTO enetpulse_tournament_stages (
    stage_id,
    name,
    tournament_fk,
    gender,
    country_fk,
    country_name,
    start_date,
    end_date,
    updates_count,
    last_updated_at,
    status
) VALUES (
    $1,  -- stage_id
    $2,  -- name
    $3,  -- tournament_fk
    $4,  -- gender
    $5,  -- country_fk
    $6,  -- country_name
    $7,  -- start_date
    $8,  -- end_date
    $9,  -- updates_count
    $10, -- last_updated_at
    $11  -- status
)
RETURNING *;

-- name: GetAllEnetpulseTournamentStages :many
SELECT *
FROM enetpulse_tournament_stages
ORDER BY created_at DESC;

-- name: GetTournamentStagesByTournamentFK :many
SELECT *
FROM enetpulse_tournament_stages
WHERE tournament_fk = $1
ORDER BY created_at DESC;



