-- +mixmaker Up
CREATE TABLE IF NOT EXISTS schema_migrations (
    version text PRIMARY KEY,
    applied_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE accounts (
    id text PRIMARY KEY,
    discord_id text NOT NULL UNIQUE,
    username text NOT NULL,
    avatar_url text NOT NULL DEFAULT '',
    role text NOT NULL CHECK (role IN ('admin', 'player')),
    created_at timestamptz NOT NULL
);

CREATE TABLE players (
    id text PRIMARY KEY,
    account_id text NOT NULL UNIQUE REFERENCES accounts(id),
    display_name text NOT NULL,
    tank_rating smallint NOT NULL CHECK (tank_rating BETWEEN 1 AND 40),
    damage_rating smallint NOT NULL CHECK (damage_rating BETWEEN 1 AND 40),
    support_rating smallint NOT NULL CHECK (support_rating BETWEEN 1 AND 40),
    preferred_roles jsonb NOT NULL DEFAULT '[]'::jsonb,
    preferred_player_ids jsonb NOT NULL DEFAULT '[]'::jsonb,
    created_at timestamptz NOT NULL,
    updated_at timestamptz NOT NULL
);

CREATE TABLE sessions (
    token_hash text PRIMARY KEY,
    account_id text NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
    expires_at timestamptz NOT NULL
);
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);

CREATE TABLE events (
    id text PRIMARY KEY,
    name text NOT NULL,
    description text NOT NULL DEFAULT '',
    starts_at timestamptz NOT NULL,
    ends_at timestamptz NOT NULL,
    registration_deadline timestamptz NOT NULL,
    created_by text NOT NULL REFERENCES accounts(id),
    created_at timestamptz NOT NULL,
    updated_at timestamptz NOT NULL,
    CHECK (ends_at > starts_at),
    CHECK (registration_deadline <= starts_at)
);

CREATE TABLE rsvps (
    event_id text NOT NULL REFERENCES events(id) ON DELETE CASCADE,
    player_id text NOT NULL REFERENCES players(id) ON DELETE CASCADE,
    status text NOT NULL CHECK (status IN ('Going', 'Maybe', 'NotGoing')),
    actor_account_id text NOT NULL REFERENCES accounts(id),
    source text NOT NULL CHECK (source IN ('player', 'admin')),
    updated_at timestamptz NOT NULL,
    PRIMARY KEY(event_id, player_id)
);

CREATE TABLE teams (
    id text PRIMARY KEY,
    event_id text NOT NULL REFERENCES events(id) ON DELETE CASCADE,
    name text NOT NULL,
    captain_player_id text REFERENCES players(id),
    slots jsonb NOT NULL
);

CREATE TABLE rulesets (
    id text PRIMARY KEY,
    name text NOT NULL,
    body jsonb NOT NULL,
    created_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE series (
    id text PRIMARY KEY,
    tournament_id text NOT NULL DEFAULT '',
    body jsonb NOT NULL,
    version integer NOT NULL DEFAULT 0
);

CREATE TABLE tournaments (
    id text PRIMARY KEY,
    event_id text NOT NULL REFERENCES events(id) ON DELETE CASCADE,
    body jsonb NOT NULL
);

CREATE TABLE draft_states (
    id text PRIMARY KEY,
    kind text NOT NULL CHECK (kind IN ('map', 'hero', 'coin')),
    body jsonb NOT NULL,
    version integer NOT NULL DEFAULT 0,
    updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE audit_log (
    id bigserial PRIMARY KEY,
    actor_account_id text NOT NULL REFERENCES accounts(id),
    action text NOT NULL,
    subject_id text NOT NULL,
    payload jsonb NOT NULL,
    created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX audit_subject_idx ON audit_log(subject_id, created_at);

-- +mixmaker Down
DROP TABLE IF EXISTS audit_log, draft_states, tournaments, series, rulesets, teams,
    rsvps, events, sessions, players, accounts, schema_migrations CASCADE;
