-- migrations/001_initial_schema.up.sql

CREATE TABLE rooms (
    id TEXT PRIMARY KEY,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    creator_name TEXT NOT NULL,
    team_turn TEXT NOT NULL DEFAULT '',
    this_turn_limit INTEGER NOT NULL DEFAULT 0,
    opened_this_turn INTEGER NOT NULL DEFAULT 0,
    blue_counter INTEGER NOT NULL DEFAULT 0,
    red_counter INTEGER NOT NULL DEFAULT 0,
    red_turn BOOLEAN NOT NULL DEFAULT FALSE,
    mime_done BOOLEAN NOT NULL DEFAULT FALSE,
    is_running BOOLEAN NOT NULL DEFAULT FALSE,
    is_over BOOLEAN NOT NULL DEFAULT FALSE,
    team_won TEXT NOT NULL DEFAULT '',
    room_pass TEXT NOT NULL DEFAULT ''
);

CREATE TABLE players (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    room_id TEXT NOT NULL,
    username TEXT NOT NULL,
    team TEXT NOT NULL DEFAULT '', -- 'red' or 'blue'
    role TEXT NOT NULL DEFAULT '', -- 'guesser' or 'mime'
    is_bot BOOLEAN NOT NULL DEFAULT FALSE,
    FOREIGN KEY (room_id) REFERENCES rooms(id)
);

CREATE TABLE word_cards (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    room_id TEXT NOT NULL,
    word TEXT NOT NULL,
    color TEXT NOT NULL DEFAULT '',
    revealed BOOLEAN NOT NULL DEFAULT FALSE,
    mime_view BOOLEAN NOT NULL DEFAULT FALSE,
    FOREIGN KEY (room_id) REFERENCES rooms(id)
);

CREATE TABLE card_marks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    card_id INTEGER NOT NULL,
    username TEXT NOT NULL,
    active BOOLEAN NOT NULL DEFAULT TRUE,
    FOREIGN KEY (card_id) REFERENCES word_cards(id)
);

CREATE TABLE actions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    room_id TEXT NOT NULL,
    actor TEXT NOT NULL,
    actor_color TEXT NOT NULL DEFAULT '',
    action_type TEXT NOT NULL,
    word TEXT NOT NULL DEFAULT '',
    word_color TEXT NOT NULL DEFAULT '',
    number_associated TEXT NOT NULL DEFAULT '', -- for clues
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (room_id) REFERENCES rooms(id)
);

CREATE TABLE settings (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    room_id TEXT NOT NULL,
    language TEXT NOT NULL DEFAULT 'en',
    room_pass TEXT NOT NULL DEFAULT '',
    turn_time INTEGER NOT NULL DEFAULT 60, -- seconds
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (room_id) REFERENCES rooms(id)
);
