-- +goose Up
-- The first-move draw (docs/ARCHITECTURE.md §6): before a game starts, each seated
-- player draws one tile from the bag and the tile closest to "A" decides who moves
-- first (a blank supersedes all letters); players tied for the best tile re-draw
-- until a single leader remains. Each draw uses fresh entropy (not the game's
-- deterministic bag seed), so this record — not a seed — is the only account of how
-- the order was chosen. It is kept for tournaments, where the draw becomes a manual
-- per-tile call. The record is dictionary-independent: the decoded letter, the blank
-- flag and the numeric draw rank describe each draw without any alphabet table. The
-- winner is reflected as seat 0 in game_players, so no order column is duplicated
-- here. In auto-match the opponent is unknown at draw time (the draw runs against a
-- synthetic placeholder when the game opens), so their draw rows carry a NULL account_id,
-- back-filled when a real opponent joins. Hidden from players for now; surfaced only in the
-- admin console.
SET search_path = backend, pg_catalog;

CREATE TABLE game_setup_draws (
    game_id    uuid        NOT NULL REFERENCES games (game_id) ON DELETE CASCADE,
    round      smallint    NOT NULL,
    pick_no    smallint    NOT NULL,
    account_id uuid        REFERENCES accounts (account_id) ON DELETE CASCADE,
    letter     text        NOT NULL,
    is_blank   boolean     NOT NULL DEFAULT false,
    draw_rank  smallint    NOT NULL,
    created_at timestamptz NOT NULL DEFAULT now(),
    PRIMARY KEY (game_id, round, pick_no)
);

-- +goose Down
SET search_path = backend, pg_catalog;
DROP TABLE game_setup_draws;
