CREATE DATABASE IF NOT EXISTS ewc2025
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

USE ewc2025;

-- ─────────────────────────────────────────────────────────────────────────────
-- LOOKUP TABLES
-- ─────────────────────────────────────────────────────────────────────────────

CREATE TABLE game (
    game_id     INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    name        VARCHAR(100)    NOT NULL,
    game_type   VARCHAR(50)     NOT NULL,
    platform    VARCHAR(50)     NOT NULL,
    PRIMARY KEY (game_id),
    UNIQUE KEY uq_game_name (name)
);

CREATE TABLE country (
    country_id      INT UNSIGNED        NOT NULL AUTO_INCREMENT,
    name            VARCHAR(100)        NOT NULL,
    region          VARCHAR(50)         NOT NULL,
    gold_medals     TINYINT UNSIGNED    NOT NULL DEFAULT 0,
    silver_medals   TINYINT UNSIGNED    NOT NULL DEFAULT 0,
    bronze_medals   TINYINT UNSIGNED    NOT NULL DEFAULT 0,
    total_medals    TINYINT UNSIGNED    NOT NULL DEFAULT 0,
    total_players   SMALLINT UNSIGNED   NOT NULL DEFAULT 0,
    top_game        VARCHAR(100),
    PRIMARY KEY (country_id),
    UNIQUE KEY uq_country_name (name)
);

CREATE TABLE point_system (
    point_system_id INT UNSIGNED        NOT NULL AUTO_INCREMENT,
    placement       TINYINT UNSIGNED    NOT NULL,
    points          SMALLINT UNSIGNED   NOT NULL,
    system_type     ENUM('Standard', 'Co-Placement', 'Extended', 'Special') NOT NULL,
    description     VARCHAR(50),
    PRIMARY KEY (point_system_id),
    UNIQUE KEY uq_placement_type (placement, system_type)
);

CREATE TABLE prize_pool_category (
    prize_category_id   INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    category            VARCHAR(100)    NOT NULL,
    amount_usd          INT UNSIGNED    NOT NULL,
    percentage          DECIMAL(5,2)    NOT NULL,
    description         VARCHAR(200),
    num_recipients      INT UNSIGNED,
    PRIMARY KEY (prize_category_id),
    UNIQUE KEY uq_category (category)
);

-- ─────────────────────────────────────────────────────────────────────────────
-- CORE ENTITIES
-- ─────────────────────────────────────────────────────────────────────────────

CREATE TABLE organization (
    organization_id             INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    name                        VARCHAR(150)    NOT NULL,
    region                      VARCHAR(50),
    country_id                  INT UNSIGNED,
    club_partner_status         ENUM('Current', 'New', 'None') NOT NULL DEFAULT 'None',
    top_8_2024                  BOOLEAN,
    founded_year                SMALLINT UNSIGNED,
    hq_location                 VARCHAR(150),
    ceo                         VARCHAR(150),
    social_media_followers_m    DECIMAL(6,2),
    PRIMARY KEY (organization_id),
    UNIQUE KEY uq_org_name (name),
    CONSTRAINT fk_org_country FOREIGN KEY (country_id) REFERENCES country (country_id)
);

CREATE TABLE tournament (
    tournament_id               INT UNSIGNED        NOT NULL AUTO_INCREMENT,
    game_id                     INT UNSIGNED        NOT NULL,
    event_name                  VARCHAR(200)        NOT NULL,
    start_date                  DATE                NOT NULL,
    end_date                    DATE                NOT NULL,
    prize_pool_usd              INT UNSIGNED        NOT NULL,
    num_participants            TINYINT UNSIGNED    NOT NULL,
    winner                      VARCHAR(200),
    runner_up                   VARCHAR(200),
    club_championship_points    BOOLEAN             NOT NULL DEFAULT FALSE,
    gender                      ENUM('Open', 'Men', 'Women') NOT NULL DEFAULT 'Open',
    PRIMARY KEY (tournament_id),
    CONSTRAINT fk_tournament_game FOREIGN KEY (game_id) REFERENCES game (game_id)
);

CREATE TABLE schedule (
    schedule_id     INT UNSIGNED        NOT NULL AUTO_INCREMENT,
    tournament_id   INT UNSIGNED        NOT NULL,
    week_number     TINYINT UNSIGNED    NOT NULL,
    venue           VARCHAR(200)        NOT NULL,
    time_zone       VARCHAR(10)         NOT NULL,
    duration_days   TINYINT UNSIGNED    NOT NULL,
    PRIMARY KEY (schedule_id),
    UNIQUE KEY uq_schedule_tournament (tournament_id),
    CONSTRAINT fk_schedule_tournament FOREIGN KEY (tournament_id) REFERENCES tournament (tournament_id)
);

CREATE TABLE player (
    player_id                   VARCHAR(20)         NOT NULL,
    player_name                 VARCHAR(100)        NOT NULL,
    country_id                  INT UNSIGNED,
    region                      VARCHAR(50),
    organization_id             INT UNSIGNED,
    game_id                     INT UNSIGNED,
    role                        VARCHAR(50),
    age                         TINYINT UNSIGNED,
    experience_years            TINYINT UNSIGNED,
    previous_team               VARCHAR(150),
    tournament_place            TINYINT UNSIGNED,
    prize_earned_usd            INT UNSIGNED        NOT NULL DEFAULT 0,
    social_media_followers_k    INT UNSIGNED        NOT NULL DEFAULT 0,
    PRIMARY KEY (player_id),
    CONSTRAINT fk_player_country      FOREIGN KEY (country_id)      REFERENCES country      (country_id),
    CONSTRAINT fk_player_org          FOREIGN KEY (organization_id) REFERENCES organization (organization_id),
    CONSTRAINT fk_player_game         FOREIGN KEY (game_id)         REFERENCES game         (game_id)
);

CREATE TABLE medalist (
    medalist_id     INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    tournament_id   INT UNSIGNED    NOT NULL,
    medal           ENUM('Gold', 'Silver', 'Bronze') NOT NULL,
    organization_id INT UNSIGNED,
    player_name     VARCHAR(100)    NOT NULL,
    country_id      INT UNSIGNED,
    role            VARCHAR(50),
    PRIMARY KEY (medalist_id),
    CONSTRAINT fk_medalist_tournament FOREIGN KEY (tournament_id)   REFERENCES tournament  (tournament_id),
    CONSTRAINT fk_medalist_org        FOREIGN KEY (organization_id) REFERENCES organization (organization_id),
    CONSTRAINT fk_medalist_country    FOREIGN KEY (country_id)      REFERENCES country      (country_id)
);

-- team_1 / team_2 / winner are VARCHAR because individual-game matches
-- (Chess, StarCraft II, fighting games) use player names instead of org names
CREATE TABLE match_result (
    match_id            INT UNSIGNED        NOT NULL AUTO_INCREMENT,
    tournament_id       INT UNSIGNED        NOT NULL,
    match_type          VARCHAR(50)         NOT NULL,
    team_1              VARCHAR(200)        NOT NULL,
    team_2              VARCHAR(200)        NOT NULL,
    winner              VARCHAR(200),
    score               VARCHAR(20),
    map                 VARCHAR(100),
    duration_minutes    SMALLINT UNSIGNED,
    mvp                 VARCHAR(100),
    PRIMARY KEY (match_id),
    CONSTRAINT fk_match_tournament FOREIGN KEY (tournament_id) REFERENCES tournament (tournament_id)
);

CREATE TABLE club_championship_standing (
    standing_id         INT UNSIGNED        NOT NULL AUTO_INCREMENT,
    organization_id     INT UNSIGNED        NOT NULL,
    rank                TINYINT UNSIGNED    NOT NULL,
    total_points        SMALLINT UNSIGNED   NOT NULL DEFAULT 0,
    prize_money_usd     INT UNSIGNED        NOT NULL DEFAULT 0,
    tournament_wins     TINYINT UNSIGNED    NOT NULL DEFAULT 0,
    top_8_finishes      TINYINT UNSIGNED    NOT NULL DEFAULT 0,
    eligible_to_win     BOOLEAN             NOT NULL DEFAULT FALSE,
    PRIMARY KEY (standing_id),
    UNIQUE KEY uq_standing_org (organization_id),
    CONSTRAINT fk_standing_org FOREIGN KEY (organization_id) REFERENCES organization (organization_id)
);

-- ─────────────────────────────────────────────────────────────────────────────
-- JUNCTION TABLES
-- ─────────────────────────────────────────────────────────────────────────────

CREATE TABLE organization_game_competing (
    organization_id INT UNSIGNED NOT NULL,
    game_id         INT UNSIGNED NOT NULL,
    PRIMARY KEY (organization_id, game_id),
    CONSTRAINT fk_ogc_org  FOREIGN KEY (organization_id) REFERENCES organization (organization_id),
    CONSTRAINT fk_ogc_game FOREIGN KEY (game_id)         REFERENCES game         (game_id)
);

CREATE TABLE organization_game_won (
    organization_id INT UNSIGNED NOT NULL,
    game_id         INT UNSIGNED NOT NULL,
    PRIMARY KEY (organization_id, game_id),
    CONSTRAINT fk_ogw_org  FOREIGN KEY (organization_id) REFERENCES organization (organization_id),
    CONSTRAINT fk_ogw_game FOREIGN KEY (game_id)         REFERENCES game         (game_id)
);
