CREATE TABLE IF NOT EXISTS enetpulse_sports (
    id BIGSERIAL PRIMARY KEY,
    sport_id VARCHAR(50) NOT NULL UNIQUE,        -- from API "id"
    name VARCHAR(255) NOT NULL,                  -- from API "name"
    updates_count INT DEFAULT 0,                 -- from API "n"
    last_updated_at TIMESTAMPTZ,                 -- from API "ut"
    status INT DEFAULT 1,                        -- optional status (active/inactive)
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS enetpulse_tournament_templates (
    id BIGSERIAL PRIMARY KEY,
    template_id VARCHAR(50) NOT NULL UNIQUE,       -- from API "id"
    name VARCHAR(255) NOT NULL,                    -- from API "name"
    sport_fk VARCHAR(50) NOT NULL REFERENCES enetpulse_sports(sport_id) ON DELETE CASCADE,
    gender VARCHAR(20) DEFAULT 'unknown',         -- from API "gender" {male, female, mixed, unknown}
    updates_count INT DEFAULT 0,                  -- from API "n"
    last_updated_at TIMESTAMPTZ,                  -- from API "ut"
    status INT DEFAULT 1,                          -- optional status (active/inactive)
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS enetpulse_tournaments (
    id BIGSERIAL PRIMARY KEY,
    tournament_id VARCHAR(50) NOT NULL UNIQUE,       -- from API "id"
    name VARCHAR(255) NOT NULL,                      -- from API "name"

    -- Link to the template it belongs to:
    tournament_template_fk VARCHAR(50) NOT NULL
        REFERENCES enetpulse_tournament_templates(template_id) ON DELETE CASCADE,

    updates_count INT DEFAULT 0,                     -- from API "n"
    last_updated_at TIMESTAMPTZ,                     -- from API "ut"
    status INT DEFAULT 1,                            -- optional active/inactive flag
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS enetpulse_tournament_stages (
    id BIGSERIAL PRIMARY KEY,
    stage_id VARCHAR(50) NOT NULL UNIQUE,           -- from API "id"
    name VARCHAR(255) NOT NULL,                     -- from API "name"
    tournament_fk VARCHAR(50) NOT NULL REFERENCES enetpulse_tournaments(tournament_id) ON DELETE CASCADE,
                                                    -- from API "tournamentFK"
    gender VARCHAR(20) DEFAULT 'unknown',           -- from API "gender" {male, female, mixed, unknown}
    country_fk VARCHAR(50),                         -- from API "countryFK"
    country_name VARCHAR(255),                      -- from API "country_name"
    start_date TIMESTAMPTZ,                         -- from API "startdate"
    end_date TIMESTAMPTZ,                           -- from API "enddate"
    updates_count INT DEFAULT 0,                    -- from API "n"
    last_updated_at TIMESTAMPTZ,                    -- from API "ut"
    status INT DEFAULT 1,                           -- optional status (active/inactive)
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_enetpulse_tournament_stages_tournament_fk
    ON enetpulse_tournament_stages (tournament_fk);

CREATE TABLE IF NOT EXISTS enetpulse_fixtures (
    id BIGSERIAL PRIMARY KEY,
    fixture_id VARCHAR(50) NOT NULL UNIQUE, -- from API "id"
    name VARCHAR(255) NOT NULL,             -- fixture name (e.g. 12 de Junio-Sol de America)

    sport_fk VARCHAR(50) NOT NULL REFERENCES enetpulse_sports(sport_id) ON DELETE CASCADE,
    tournament_fk VARCHAR(50),  -- raw tournamentFK (optional)
    tournament_template_fk VARCHAR(50) REFERENCES enetpulse_tournament_templates(template_id) ON DELETE CASCADE,
    -- tournament_stage_fk VARCHAR(50) REFERENCES enetpulse_tournament_stages(stage_id) ON DELETE CASCADE,

    -- tournament_stage_name VARCHAR(255),
    tournament_name VARCHAR(255),
    tournament_template_name VARCHAR(255),
    sport_name VARCHAR(255),
    gender VARCHAR(20),

    start_date TIMESTAMPTZ NOT NULL,         -- startdate
    status_type VARCHAR(50),                 -- Not started, Live, Finished
    status_desc_fk VARCHAR(50),              -- status_descFK
    round_type_fk VARCHAR(50),               -- round_typeFK
    updates_count INT DEFAULT 0,             -- n
    last_updated_at TIMESTAMPTZ,             -- ut

    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS enetpulse_results (
    id BIGSERIAL PRIMARY KEY,
    result_id VARCHAR(50) NOT NULL UNIQUE,       -- from API "id"
    name VARCHAR(255) NOT NULL,                  -- event name (e.g. Brentford-Manchester City)

    sport_fk VARCHAR(50) NOT NULL REFERENCES enetpulse_sports(sport_id) ON DELETE CASCADE,
    tournament_fk VARCHAR(50),
    tournament_template_fk VARCHAR(50) REFERENCES enetpulse_tournament_templates(template_id) ON DELETE CASCADE,
    -- tournament_stage_fk VARCHAR(50) REFERENCES enetpulse_tournament_stages(stage_id) ON DELETE CASCADE,

    -- tournament_stage_name VARCHAR(255),
    tournament_name VARCHAR(255),
    tournament_template_name VARCHAR(255),
    sport_name VARCHAR(255),

    start_date TIMESTAMPTZ NOT NULL,             -- startdate
    status_type VARCHAR(50),                     -- e.g. Finished
    status_desc_fk VARCHAR(50),                  -- status_descFK
    round_type_fk VARCHAR(50),                   -- round_typeFK
    updates_count INT DEFAULT 0,                 -- n
    last_updated_at TIMESTAMPTZ,                 -- ut

    -- Optional metadata (dynamic but common fields are included)
    round VARCHAR(50),
    live VARCHAR(10),
    venue_name VARCHAR(255),
    livestats_plus VARCHAR(10),
    livestats_type VARCHAR(50),
    commentary VARCHAR(50),
    lineup_confirmed BOOLEAN,
    verified BOOLEAN,
    spectators INT,

    -- Time-related metadata
    game_started TIMESTAMPTZ,
    first_half_ended TIMESTAMPTZ,
    second_half_started TIMESTAMPTZ,
    second_half_ended TIMESTAMPTZ,
    game_ended TIMESTAMPTZ,

    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

-- Event Participants (teams involved in the result)
CREATE TABLE IF NOT EXISTS enetpulse_result_participants (
    id BIGSERIAL PRIMARY KEY,
    participant_map_id VARCHAR(50) NOT NULL UNIQUE,  -- from event_participants.*.id
    result_fk VARCHAR(50) NOT NULL REFERENCES enetpulse_results(result_id) ON DELETE CASCADE,
    participant_fk VARCHAR(50) NOT NULL,             -- team/player FK (from API participantFK)
    number INT,                                      -- 1 or 2 (home/away indicator)
    name VARCHAR(255),                               -- team/player name
    gender VARCHAR(20),
    type VARCHAR(50),
    country_fk VARCHAR(50),
    country_name VARCHAR(100),

    -- Result details
    ordinary_time VARCHAR(10),
    running_score VARCHAR(10),
    halftime VARCHAR(10),
    final_result VARCHAR(10),

    last_updated_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- Referees / Match officials
CREATE TABLE IF NOT EXISTS enetpulse_result_referees (
    id BIGSERIAL PRIMARY KEY,
    result_fk VARCHAR(50) NOT NULL REFERENCES enetpulse_results(result_id) ON DELETE CASCADE,
    referee_fk VARCHAR(50),
    assistant1_referee_fk VARCHAR(50),
    assistant2_referee_fk VARCHAR(50),
    fourth_referee_fk VARCHAR(50),
    var1_referee_fk VARCHAR(50),
    var2_referee_fk VARCHAR(50),
    last_updated_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS enetpulse_outcome_types (
    id BIGSERIAL PRIMARY KEY,
    outcome_type_id VARCHAR(50) NOT NULL UNIQUE,  -- from API "id"
    name VARCHAR(100) NOT NULL,                   -- e.g. "1x2"
    description VARCHAR(255),                     -- e.g. "1x2 - 3Way"

    updates_count INT DEFAULT 0,                  -- maps to "n"
    last_updated_at TIMESTAMPTZ,                  -- maps to "ut"

    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

-- Table for pre-match odds (main outcome entries)
CREATE TABLE IF NOT EXISTS enetpulse_preodds (
    id BIGSERIAL PRIMARY KEY,
    preodds_id VARCHAR(50) NOT NULL UNIQUE,          -- from API "id"
    event_fk BIGINT NOT NULL,                         -- maps to objectFK/event
    outcome_type_fk INT,                              -- outcome_typeFK
    outcome_scope_fk INT,                             -- outcome_scopeFK
    outcome_subtype_fk INT,                           -- outcome_subtypeFK
    event_participant_number INT,                     -- event_participant_number
    iparam VARCHAR(50),
    iparam2 VARCHAR(50),
    dparam VARCHAR(50),
    dparam2 VARCHAR(50),
    sparam VARCHAR(50),

    updates_count INT DEFAULT 0,                      -- maps to "n"
    last_updated_at TIMESTAMPTZ,                      -- maps to "ut"

    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

-- Table for nested betting offers within preodds
CREATE TABLE IF NOT EXISTS enetpulse_preodds_bettingoffers (
    id BIGSERIAL PRIMARY KEY,
    bettingoffer_id VARCHAR(50) NOT NULL UNIQUE,      -- from API "id"
    preodds_fk VARCHAR(50) NOT NULL REFERENCES enetpulse_preodds(preodds_id) ON DELETE CASCADE,
    bettingoffer_status_fk INT,                       -- bettingoffer_statusFK
    odds_provider_fk INT,                             -- odds_providerFK
    odds NUMERIC(10,4),                               -- current odds
    odds_old NUMERIC(10,4),                           -- previous odds
    active BOOLEAN,                                   -- maps "yes"/"no" to boolean
    coupon_key VARCHAR(255),                          -- couponKey
    updates_count INT DEFAULT 0,                      -- maps to "n"
    last_updated_at TIMESTAMPTZ,                      -- maps to "ut"

    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);






