-- =============================================================================
-- EWC 2025 — DATA MART STAR SCHEMA
-- Target: Oracle (university lab schema)
-- =============================================================================
--
-- 3 Fact tables  x  5 Dimension tables
--
--   FACT_TOURNAMENT    — one row per tournament (prize, participants, duration)
--   FACT_MEDAL_AWARD   — one row per player-medal (257 rows)
--   FACT_CLUB_STANDING — snapshot of final Club Championship standings (24 rows)
--
--                    DIM_DATE
--                       |
--   DIM_ORGANIZATION — FACT_TOURNAMENT — DIM_GAME
--
--   DIM_COUNTRY ─┐
--   DIM_ORG     ─┤── FACT_MEDAL_AWARD ── DIM_GAME
--   DIM_MEDAL   ─┘         │
--                        DIM_DATE
--
--   DIM_ORGANIZATION ── FACT_CLUB_STANDING
--
-- =============================================================================

-- -----------------------------------------------------------------------------
-- DIMENSION TABLES
-- -----------------------------------------------------------------------------

-- date_key is YYYYMMDD integer — avoids JOIN to a calendar table for simple
-- filters and makes date range predicates cheap.
CREATE TABLE DIM_DATE (
    date_key        NUMBER(8,0)     NOT NULL,
    full_date       DATE            NOT NULL,
    year            NUMBER(4,0)     NOT NULL,
    quarter         NUMBER(1,0)     NOT NULL,
    month           NUMBER(2,0)     NOT NULL,
    month_name      VARCHAR2(10)    NOT NULL,
    week_number     NUMBER(2,0)     NOT NULL,
    day_of_month    NUMBER(2,0)     NOT NULL,
    day_name        VARCHAR2(10)    NOT NULL,
    CONSTRAINT pk_dim_date PRIMARY KEY (date_key)
);

CREATE TABLE DIM_GAME (
    game_key    NUMBER(10,0)    GENERATED ALWAYS AS IDENTITY,
    game_id     NUMBER(10,0)    NOT NULL,
    name        VARCHAR2(100)   NOT NULL,
    game_type   VARCHAR2(50)    NOT NULL,
    platform    VARCHAR2(50)    NOT NULL,
    CONSTRAINT pk_dim_game      PRIMARY KEY (game_key),
    CONSTRAINT uq_dim_game_id   UNIQUE      (game_id)
);

CREATE TABLE DIM_COUNTRY (
    country_key NUMBER(10,0)    GENERATED ALWAYS AS IDENTITY,
    country_id  NUMBER(10,0)    NOT NULL,
    name        VARCHAR2(100)   NOT NULL,
    region      VARCHAR2(50)    NOT NULL,
    CONSTRAINT pk_dim_country   PRIMARY KEY (country_key),
    CONSTRAINT uq_dim_cntry_id  UNIQUE      (country_id)
);

CREATE TABLE DIM_ORGANIZATION (
    org_key                     NUMBER(10,0)    GENERATED ALWAYS AS IDENTITY,
    org_id                      NUMBER(10,0)    NOT NULL,
    name                        VARCHAR2(150)   NOT NULL,
    region                      VARCHAR2(50),
    country                     VARCHAR2(100),
    club_partner_status         VARCHAR2(10),
    founded_year                NUMBER(4,0),
    social_media_followers_m    NUMBER(6,2),
    CONSTRAINT pk_dim_org       PRIMARY KEY (org_key),
    CONSTRAINT uq_dim_org_id    UNIQUE      (org_id)
);

CREATE TABLE DIM_MEDAL (
    medal_key   NUMBER(10,0)    GENERATED ALWAYS AS IDENTITY,
    medal_type  VARCHAR2(10)    NOT NULL,   -- Gold / Silver / Bronze
    medal_rank  NUMBER(1,0)     NOT NULL,   -- 1 / 2 / 3
    CONSTRAINT pk_dim_medal     PRIMARY KEY (medal_key),
    CONSTRAINT uq_dim_medal_tp  UNIQUE      (medal_type)
);

-- -----------------------------------------------------------------------------
-- FACT TABLES
-- -----------------------------------------------------------------------------

-- Grain: one row per tournament
-- Supports: prize analysis by game type / platform / week / gender
CREATE TABLE FACT_TOURNAMENT (
    fact_id             NUMBER(10,0)    GENERATED ALWAYS AS IDENTITY,
    game_key            NUMBER(10,0)    NOT NULL,
    start_date_key      NUMBER(8,0)     NOT NULL,
    end_date_key        NUMBER(8,0)     NOT NULL,
    winner_org_key      NUMBER(10,0),               -- NULL for individual-winner events
    -- degenerate dimensions
    event_name          VARCHAR2(200)   NOT NULL,
    gender              VARCHAR2(10)    NOT NULL,    -- Open / Men / Women
    -- measures
    prize_pool_usd      NUMBER(12,0)    NOT NULL,
    num_participants    NUMBER(5,0)     NOT NULL,
    duration_days       NUMBER(3,0)     NOT NULL,
    has_club_points     NUMBER(1,0)     NOT NULL,
    CONSTRAINT pk_fact_tourn    PRIMARY KEY (fact_id),
    CONSTRAINT fk_ft_game       FOREIGN KEY (game_key)       REFERENCES DIM_GAME         (game_key),
    CONSTRAINT fk_ft_sdate      FOREIGN KEY (start_date_key) REFERENCES DIM_DATE         (date_key),
    CONSTRAINT fk_ft_edate      FOREIGN KEY (end_date_key)   REFERENCES DIM_DATE         (date_key),
    CONSTRAINT fk_ft_winner     FOREIGN KEY (winner_org_key) REFERENCES DIM_ORGANIZATION (org_key),
    CONSTRAINT ck_ft_pts        CHECK (has_club_points IN (0, 1))
);

-- Grain: one row per player-medal
-- Supports: medal count and medal points by country / region / game / org
CREATE TABLE FACT_MEDAL_AWARD (
    fact_id         NUMBER(10,0)    GENERATED ALWAYS AS IDENTITY,
    game_key        NUMBER(10,0)    NOT NULL,
    medal_key       NUMBER(10,0)    NOT NULL,
    country_key     NUMBER(10,0),
    org_key         NUMBER(10,0),
    date_key        NUMBER(8,0),                    -- tournament start date
    -- degenerate dimension
    player_name     VARCHAR2(100)   NOT NULL,
    -- measures
    medal_count     NUMBER(3,0)     DEFAULT 1   NOT NULL,
    medal_points    NUMBER(3,0)                 NOT NULL,   -- Gold=3, Silver=2, Bronze=1
    CONSTRAINT pk_fact_medal    PRIMARY KEY (fact_id),
    CONSTRAINT fk_fm_game       FOREIGN KEY (game_key)   REFERENCES DIM_GAME         (game_key),
    CONSTRAINT fk_fm_medal      FOREIGN KEY (medal_key)  REFERENCES DIM_MEDAL        (medal_key),
    CONSTRAINT fk_fm_country    FOREIGN KEY (country_key) REFERENCES DIM_COUNTRY     (country_key),
    CONSTRAINT fk_fm_org        FOREIGN KEY (org_key)    REFERENCES DIM_ORGANIZATION (org_key),
    CONSTRAINT fk_fm_date       FOREIGN KEY (date_key)   REFERENCES DIM_DATE         (date_key)
);

-- Grain: one row per club (end-of-season snapshot)
-- Supports: club performance comparison by region / partner status / eligibility
CREATE TABLE FACT_CLUB_STANDING (
    fact_id             NUMBER(10,0)    GENERATED ALWAYS AS IDENTITY,
    org_key             NUMBER(10,0)    NOT NULL,
    -- measures
    final_rank          NUMBER(3,0)     NOT NULL,
    total_points        NUMBER(6,0)     NOT NULL,
    prize_money_usd     NUMBER(12,0)    NOT NULL,
    tournament_wins     NUMBER(3,0)     NOT NULL,
    top_8_finishes      NUMBER(3,0)     NOT NULL,
    eligible_to_win     NUMBER(1,0)     NOT NULL,
    CONSTRAINT pk_fact_club     PRIMARY KEY (fact_id),
    CONSTRAINT fk_fcs_org       FOREIGN KEY (org_key) REFERENCES DIM_ORGANIZATION (org_key),
    CONSTRAINT ck_fcs_elig      CHECK (eligible_to_win IN (0, 1))
);
