-- Guest-limit foundation (E8): tag each game with its kind so the per-tier, per-kind active-game
-- limits are enforceable, and add the single-row config that holds those limits (tuned in the admin,
-- no release). It replaces the old hardcoded MaxActiveQuickGames=10 combined cap with a per-tier,
-- per-kind config. game_kind: 0=unknown (pre-E8 games, never gated), 1=vs_ai, 2=random, 3=friends —
-- set on creation. The limits are smallint with -1 = unlimited; a guest defaults to 1 vs_ai + 1
-- random (friends 0, moot — the guest gate blocks friend games), a durable account to 10 per kind
-- (the old cap, now per kind). Additive only — applies forward via goose with no data rewrite (no
-- contour wipe); an image rollback ignores the column + table.
-- +goose Up

ALTER TABLE backend.games
    ADD COLUMN game_kind smallint DEFAULT 0 NOT NULL;
ALTER TABLE backend.games
    ADD CONSTRAINT games_game_kind_chk CHECK (game_kind >= 0 AND game_kind <= 3);

CREATE TABLE backend.config (
    only_row boolean DEFAULT true NOT NULL,
    guest_vs_ai_limit smallint DEFAULT 1 NOT NULL,
    guest_random_limit smallint DEFAULT 1 NOT NULL,
    guest_friends_limit smallint DEFAULT 0 NOT NULL,
    durable_vs_ai_limit smallint DEFAULT 10 NOT NULL,
    durable_random_limit smallint DEFAULT 10 NOT NULL,
    durable_friends_limit smallint DEFAULT 10 NOT NULL,
    CONSTRAINT config_pkey PRIMARY KEY (only_row),
    CONSTRAINT config_single_row_chk CHECK (only_row),
    CONSTRAINT config_limits_chk CHECK (
        guest_vs_ai_limit >= -1 AND guest_random_limit >= -1 AND guest_friends_limit >= -1 AND
        durable_vs_ai_limit >= -1 AND durable_random_limit >= -1 AND durable_friends_limit >= -1)
);

INSERT INTO backend.config (only_row) VALUES (true);

-- +goose Down

DROP TABLE backend.config;
ALTER TABLE backend.games DROP COLUMN game_kind;
