CREATE TYPE wenglin_t AS (
	rating float8,
	uncertainty float8
);

CREATE TABLE players (
	id INTEGER GENERATED ALWAYS AS IDENTITY,

	wenglin wenglin_t NOT NULL DEFAULT ROW(25.0, 25.0 / 3),

	telegram_id BIGINT,

	CONSTRAINT telegram_ids_are_unique UNIQUE (telegram_id),
	PRIMARY KEY (id)
);

CREATE TYPE outcome_t AS ENUM (
	'AWins',
	'BWins',
	'Tie'
);

CREATE TABLE matches (
	id INTEGER GENERATED ALWAYS AS IDENTITY,
	instant TIMESTAMPTZ NOT NULL DEFAULT now(),
	name VARCHAR,

	player_a_id BIGINT NOT NULL,
	player_a_wenglin_before wenglin_t NOT NULL,
	player_a_wenglin_after wenglin_t NOT NULL,

	player_b_id BIGINT NOT NULL,
	player_b_wenglin_before wenglin_t NOT NULL,
	player_b_wenglin_after wenglin_t NOT NULL,

	outcome outcome_t NOT NULL,

	CONSTRAINT match_unique_name UNIQUE (name),
	CONSTRAINT not_same_player CHECK (player_a_id != player_b_id),
	FOREIGN KEY (player_a_id) REFERENCES players (id),
	FOREIGN KEY (player_b_id) REFERENCES players (id),
	PRIMARY KEY (id)
);
