CREATE TABLE campaign_templates (
    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    group_id            UUID NOT NULL REFERENCES game_groups(id) ON DELETE CASCADE,
    name                VARCHAR(200) NOT NULL,
    title               VARCHAR(500) NOT NULL,
    join_link           TEXT NOT NULL,
    session_count       INTEGER NOT NULL CHECK (session_count BETWEEN 1 AND 52),
    interval_days       INTEGER NOT NULL CHECK (interval_days BETWEEN 1 AND 365),
    max_players         INTEGER CHECK (max_players IS NULL OR max_players > 0),
    notification_mode   VARCHAR(32) NOT NULL DEFAULT 'GroupAndDirect'
                        CHECK (notification_mode IN ('GroupAndDirect', 'GroupOnly')),
    created_at          TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at          TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE (group_id, name)
);

CREATE INDEX ix_campaign_templates_group ON campaign_templates (group_id, created_at DESC);
