SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: matches; Type: TABLE; Schema: public;
--

CREATE TABLE public.matches (
    match_id integer NOT NULL,
    player1_uid integer NOT NULL,
    player2_uid integer,
    player1_score integer,
    player2_score integer,
    player1_elo_change integer,
    player2_elo_change integer,
    match_date timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);



--
-- Name: matches_2v2; Type: TABLE; Schema: public;
--

CREATE TABLE public.matches_2v2 (
    match_id integer NOT NULL,
    player1_team1_uid integer,
    player2_team1_uid integer,
    player1_team2_uid integer,
    player2_team2_uid integer,
    player1_team1_score integer DEFAULT 0,
    player2_team1_score integer DEFAULT 0,
    player1_team2_score integer DEFAULT 0,
    player2_team2_score integer DEFAULT 0,
    trueskill_mu_team1 double precision,
    trueskill_mu_team2 double precision,
    trueskill_sigma_team1 double precision,
    trueskill_sigma_team2 double precision,
    team1_score integer DEFAULT 0,
    team2_score integer DEFAULT 0,
    match_date timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    winner_team integer DEFAULT 0,
    match_status character varying(20) DEFAULT 'in_progress'::character varying
);



--
-- Name: matches_2v2_match_id_seq; Type: SEQUENCE; Schema: public;
--

CREATE SEQUENCE public.matches_2v2_match_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;



--
-- Name: matches_2v2_match_id_seq; Type: SEQUENCE OWNED BY; Schema: public;
--

ALTER SEQUENCE public.matches_2v2_match_id_seq OWNED BY public.matches_2v2.match_id;


--
-- Name: matches_match_id_seq; Type: SEQUENCE; Schema: public;
--

CREATE SEQUENCE public.matches_match_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;



--
-- Name: matches_match_id_seq; Type: SEQUENCE OWNED BY; Schema: public;
--

ALTER SEQUENCE public.matches_match_id_seq OWNED BY public.matches.match_id;


--
-- Name: tournament_matches; Type: TABLE; Schema: public;
--

CREATE TABLE public.tournament_matches (
    tournament_match_id integer NOT NULL,
    tournament_id integer NOT NULL,
    round integer NOT NULL,
    match_id integer,
    player1_uid integer NOT NULL,
    player2_uid integer NOT NULL,
    winner_uid integer,
    match_date timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);



--
-- Name: tournament_matches_tournament_match_id_seq; Type: SEQUENCE; Schema: public;
--

CREATE SEQUENCE public.tournament_matches_tournament_match_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;



ALTER SEQUENCE public.tournament_matches_tournament_match_id_seq OWNED BY public.tournament_matches.tournament_match_id;

CREATE TABLE public.tournaments (
    tournament_id integer NOT NULL,
    name character varying(255) NOT NULL,
    start_date date NOT NULL,
    end_date date,
    status character varying(50) DEFAULT 'upcoming'::character varying,
    bracket jsonb
);



CREATE SEQUENCE public.tournaments_tournament_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE public.tournaments_tournament_id_seq OWNED BY public.tournaments.tournament_id;

CREATE TABLE public.users (
    uid integer NOT NULL,
    email character varying(255) NOT NULL,
    display_name character varying(100) NOT NULL,
    password_hash character varying(255) NOT NULL,
    current_elo integer DEFAULT 0,
    friend_list jsonb DEFAULT '[]'::jsonb,
    session_token character varying,
    openskill_mu double precision DEFAULT 25.0,
    openskill_sigma double precision DEFAULT 8.333
);



CREATE SEQUENCE public.users_uid_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;




ALTER SEQUENCE public.users_uid_seq OWNED BY public.users.uid;

ALTER TABLE ONLY public.matches ALTER COLUMN match_id SET DEFAULT nextval('public.matches_match_id_seq'::regclass);

ALTER TABLE ONLY public.matches_2v2 ALTER COLUMN match_id SET DEFAULT nextval('public.matches_2v2_match_id_seq'::regclass);


ALTER TABLE ONLY public.tournament_matches ALTER COLUMN tournament_match_id SET DEFAULT nextval('public.tournament_matches_tournament_match_id_seq'::regclass);

ALTER TABLE ONLY public.tournaments ALTER COLUMN tournament_id SET DEFAULT nextval('public.tournaments_tournament_id_seq'::regclass);

ALTER TABLE ONLY public.users ALTER COLUMN uid SET DEFAULT nextval('public.users_uid_seq'::regclass);

ALTER TABLE ONLY public.matches_2v2
    ADD CONSTRAINT matches_2v2_pkey PRIMARY KEY (match_id);

ALTER TABLE ONLY public.matches
    ADD CONSTRAINT matches_pkey PRIMARY KEY (match_id);

ALTER TABLE ONLY public.tournament_matches
    ADD CONSTRAINT tournament_matches_pkey PRIMARY KEY (tournament_match_id);

ALTER TABLE ONLY public.tournaments
    ADD CONSTRAINT tournaments_pkey PRIMARY KEY (tournament_id);

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_email_key UNIQUE (email);

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (uid);

ALTER TABLE ONLY public.matches
    ADD CONSTRAINT matches_player1_uid_fkey FOREIGN KEY (player1_uid) REFERENCES public.users(uid);

ALTER TABLE ONLY public.matches
    ADD CONSTRAINT matches_player2_uid_fkey FOREIGN KEY (player2_uid) REFERENCES public.users(uid);

ALTER TABLE ONLY public.tournament_matches
    ADD CONSTRAINT tournament_matches_player1_uid_fkey FOREIGN KEY (player1_uid) REFERENCES public.users(uid);

ALTER TABLE ONLY public.tournament_matches
    ADD CONSTRAINT tournament_matches_player2_uid_fkey FOREIGN KEY (player2_uid) REFERENCES public.users(uid);

ALTER TABLE ONLY public.tournament_matches
    ADD CONSTRAINT tournament_matches_tournament_id_fkey FOREIGN KEY (tournament_id) REFERENCES public.tournaments(tournament_id) ON DELETE CASCADE;

ALTER TABLE ONLY public.tournament_matches
    ADD CONSTRAINT tournament_matches_winner_uid_fkey FOREIGN KEY (winner_uid) REFERENCES public.users(uid);


