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 for tournament stages
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
);

-- Index to quickly query by tournament_fk
CREATE INDEX IF NOT EXISTS idx_enetpulse_tournament_stages_tournament_fk
    ON enetpulse_tournament_stages (tournament_fk);


