-- +mixmaker Up
ALTER TABLE events
    ADD COLUMN IF NOT EXISTS state text NOT NULL DEFAULT 'RegistrationOpen',
    ADD COLUMN IF NOT EXISTS version integer NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS ruleset_id text NOT NULL DEFAULT 'standard-control-hybrid-control',
    ADD COLUMN IF NOT EXISTS active_series_id text NOT NULL DEFAULT '',
    ADD COLUMN IF NOT EXISTS tournament_id text NOT NULL DEFAULT '';

ALTER TABLE events
    DROP CONSTRAINT IF EXISTS events_state_check,
    ADD CONSTRAINT events_state_check CHECK (state IN (
        'RegistrationOpen', 'RegistrationClosed', 'Balancing',
        'RostersDraft', 'RostersConfirmed', 'Live', 'Completed'
    ));

CREATE TABLE IF NOT EXISTS event_rosters (
    event_id text PRIMARY KEY REFERENCES events(id) ON DELETE CASCADE,
    body jsonb NOT NULL,
    version integer NOT NULL DEFAULT 0,
    updated_at timestamptz NOT NULL DEFAULT now()
);

-- +mixmaker Down
DROP TABLE IF EXISTS event_rosters;
ALTER TABLE events
    DROP COLUMN IF EXISTS tournament_id,
    DROP COLUMN IF EXISTS active_series_id,
    DROP COLUMN IF EXISTS ruleset_id,
    DROP COLUMN IF EXISTS version,
    DROP COLUMN IF EXISTS state;
