-- 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: DeleteEnetpulseTournamentTemplateByID :exec
-- DELETE FROM enetpulse_tournament_templates WHERE template_id = $1;

-- 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;

-- name: CreateEnetpulseFixture :one
INSERT INTO enetpulse_fixtures (
    fixture_id,
    name,
    sport_fk,
    tournament_fk,
    tournament_template_fk,
    tournament_stage_fk,
    tournament_stage_name,
    tournament_name,
    tournament_template_name,
    sport_name,
    gender,
    start_date,
    status_type,
    status_desc_fk,
    round_type_fk,
    updates_count,
    last_updated_at,
    created_at,
    updated_at
) VALUES (
    $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP
)
ON CONFLICT (fixture_id) DO UPDATE
SET
    name = EXCLUDED.name,
    sport_fk = EXCLUDED.sport_fk,
    tournament_fk = EXCLUDED.tournament_fk,
    tournament_template_fk = EXCLUDED.tournament_template_fk,
    tournament_stage_fk = EXCLUDED.tournament_stage_fk,
    tournament_stage_name = EXCLUDED.tournament_stage_name,
    tournament_name = EXCLUDED.tournament_name,
    tournament_template_name = EXCLUDED.tournament_template_name,
    sport_name = EXCLUDED.sport_name,
    gender = EXCLUDED.gender,
    start_date = EXCLUDED.start_date,
    status_type = EXCLUDED.status_type,
    status_desc_fk = EXCLUDED.status_desc_fk,
    round_type_fk = EXCLUDED.round_type_fk,
    updates_count = EXCLUDED.updates_count,
    last_updated_at = EXCLUDED.last_updated_at,
    updated_at = CURRENT_TIMESTAMP
RETURNING *;

-- name: GetAllEnetpulseFixtures :many
SELECT *
FROM enetpulse_fixtures
ORDER BY created_at DESC;

-- name: CreateEnetpulseResult :one
INSERT INTO enetpulse_results (
    result_id,
    name,
    sport_fk,
    tournament_fk,
    tournament_template_fk,
    tournament_stage_fk,
    tournament_stage_name,
    tournament_name,
    tournament_template_name,
    sport_name,
    start_date,
    status_type,
    status_desc_fk,
    round_type_fk,
    updates_count,
    last_updated_at,
    round,
    live,
    venue_name,
    livestats_plus,
    livestats_type,
    commentary,
    lineup_confirmed,
    verified,
    spectators,
    game_started,
    first_half_ended,
    second_half_started,
    second_half_ended,
    game_ended
) VALUES (
    $1, $2, $3, $4, $5, $6,
    $7, $8, $9, $10, $11,
    $12, $13, $14, $15, $16,
    $17, $18, $19, $20, $21,
    $22, $23, $24, $25, $26,
    $27, $28, $29, $30
)
ON CONFLICT (result_id) DO UPDATE
SET
    name = EXCLUDED.name,
    sport_fk = EXCLUDED.sport_fk,
    tournament_fk = EXCLUDED.tournament_fk,
    tournament_template_fk = EXCLUDED.tournament_template_fk,
    tournament_stage_fk = EXCLUDED.tournament_stage_fk,
    tournament_stage_name = EXCLUDED.tournament_stage_name,
    tournament_name = EXCLUDED.tournament_name,
    tournament_template_name = EXCLUDED.tournament_template_name,
    sport_name = EXCLUDED.sport_name,
    start_date = EXCLUDED.start_date,
    status_type = EXCLUDED.status_type,
    status_desc_fk = EXCLUDED.status_desc_fk,
    round_type_fk = EXCLUDED.round_type_fk,
    updates_count = EXCLUDED.updates_count,
    last_updated_at = EXCLUDED.last_updated_at,
    round = EXCLUDED.round,
    live = EXCLUDED.live,
    venue_name = EXCLUDED.venue_name,
    livestats_plus = EXCLUDED.livestats_plus,
    livestats_type = EXCLUDED.livestats_type,
    commentary = EXCLUDED.commentary,
    lineup_confirmed = EXCLUDED.lineup_confirmed,
    verified = EXCLUDED.verified,
    spectators = EXCLUDED.spectators,
    game_started = EXCLUDED.game_started,
    first_half_ended = EXCLUDED.first_half_ended,
    second_half_started = EXCLUDED.second_half_started,
    second_half_ended = EXCLUDED.second_half_ended,
    game_ended = EXCLUDED.game_ended,
    updated_at = CURRENT_TIMESTAMP
RETURNING *;

-- name: GetAllEnetpulseResults :many
SELECT *
FROM enetpulse_results
ORDER BY created_at DESC;

-- name: CreateEnetpulseOutcomeType :one
INSERT INTO enetpulse_outcome_types (
    outcome_type_id,
    name,
    description,
    updates_count,
    last_updated_at,
    created_at,
    updated_at
) VALUES (
    $1, $2, $3, $4, $5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
)
ON CONFLICT (outcome_type_id) DO UPDATE
SET
    name = EXCLUDED.name,
    description = EXCLUDED.description,
    updates_count = EXCLUDED.updates_count,
    last_updated_at = EXCLUDED.last_updated_at,
    updated_at = CURRENT_TIMESTAMP
RETURNING *;

-- name: GetAllEnetpulseOutcomeTypes :many
SELECT *
FROM enetpulse_outcome_types
ORDER BY created_at DESC;

-- name: CreateEnetpulsePreodds :one
INSERT INTO enetpulse_preodds (
    preodds_id,
    event_fk,
    outcome_type_fk,
    outcome_scope_fk,
    outcome_subtype_fk,
    event_participant_number,
    iparam,
    iparam2,
    dparam,
    dparam2,
    sparam,
    updates_count,
    last_updated_at,
    created_at,
    updated_at
) VALUES (
    $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP
)
ON CONFLICT (preodds_id) DO UPDATE
SET
    event_fk = EXCLUDED.event_fk,
    outcome_type_fk = EXCLUDED.outcome_type_fk,
    outcome_scope_fk = EXCLUDED.outcome_scope_fk,
    outcome_subtype_fk = EXCLUDED.outcome_subtype_fk,
    event_participant_number = EXCLUDED.event_participant_number,
    iparam = EXCLUDED.iparam,
    iparam2 = EXCLUDED.iparam2,
    dparam = EXCLUDED.dparam,
    dparam2 = EXCLUDED.dparam2,
    sparam = EXCLUDED.sparam,
    updates_count = EXCLUDED.updates_count,
    last_updated_at = EXCLUDED.last_updated_at,
    updated_at = CURRENT_TIMESTAMP
RETURNING *;

-- name: GetAllEnetpulsePreodds :many
SELECT *
FROM enetpulse_preodds
ORDER BY created_at DESC;


-- name: CreateEnetpulsePreoddsBettingOffer :one
INSERT INTO enetpulse_preodds_bettingoffers (
    bettingoffer_id,
    preodds_fk,
    bettingoffer_status_fk,
    odds_provider_fk,
    odds,
    odds_old,
    active,
    coupon_key,
    updates_count,
    last_updated_at,
    created_at,
    updated_at
) VALUES (
    $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP
)
ON CONFLICT (bettingoffer_id) DO UPDATE
SET
    preodds_fk = EXCLUDED.preodds_fk,
    bettingoffer_status_fk = EXCLUDED.bettingoffer_status_fk,
    odds_provider_fk = EXCLUDED.odds_provider_fk,
    odds = EXCLUDED.odds,
    odds_old = EXCLUDED.odds_old,
    active = EXCLUDED.active,
    coupon_key = EXCLUDED.coupon_key,
    updates_count = EXCLUDED.updates_count,
    last_updated_at = EXCLUDED.last_updated_at,
    updated_at = CURRENT_TIMESTAMP
RETURNING *;

-- name: GetAllEnetpulsePreoddsBettingOffers :many
SELECT *
FROM enetpulse_preodds_bettingoffers
ORDER BY created_at DESC;







