-- This script was generated by the ERD tool in pgAdmin 4.
-- Please log an issue at https://github.com/pgadmin-org/pgadmin4/issues/new/choose if you find any bugs, including reproduction steps.
BEGIN;

-- ===================================================================
-- STEP 1: Enable Required Extensions
-- ===================================================================
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- ===================================================================
-- STEP 2: Create Tables
-- ===================================================================

CREATE TABLE IF NOT EXISTS public."ChatArchives"
(
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    "chatId" uuid NOT NULL,
    "archivedMessages" json NOT NULL,
    "archivedAt" timestamp without time zone NOT NULL,
    "createDate" timestamp without time zone NOT NULL DEFAULT now(),
    "chatType" character varying(50) COLLATE pg_catalog."default" NOT NULL,
    "chatName" character varying(255) COLLATE pg_catalog."default",
    "gameId" uuid,
    participants uuid[] NOT NULL,
    CONSTRAINT "PK_fe62979fc2061d7afe278d3f14e" PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Chats"
(
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    type character varying(50) COLLATE pg_catalog."default" NOT NULL DEFAULT 'direct'::character varying,
    name character varying(255) COLLATE pg_catalog."default",
    "gameId" uuid,
    "createdBy" uuid,
    users uuid[] NOT NULL,
    messages json NOT NULL DEFAULT '[]'::json,
    "lastActivity" timestamp without time zone,
    "createDate" timestamp without time zone NOT NULL DEFAULT now(),
    "updateDate" timestamp without time zone NOT NULL DEFAULT now(),
    state integer NOT NULL DEFAULT 0,
    "archiveDate" timestamp without time zone,
    CONSTRAINT "PK_64c36c2b8d86a0d5de4cf64de8d" PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Contacts"
(
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    name character varying(255) COLLATE pg_catalog."default" NOT NULL,
    email character varying(255) COLLATE pg_catalog."default" NOT NULL,
    userid uuid,
    type integer NOT NULL,
    txt text COLLATE pg_catalog."default" NOT NULL,
    state integer NOT NULL DEFAULT 0,
    "createDate" timestamp without time zone NOT NULL DEFAULT now(),
    "updateDate" timestamp without time zone NOT NULL DEFAULT now(),
    "adminResponse" text COLLATE pg_catalog."default",
    "responseDate" timestamp without time zone,
    "respondedBy" uuid,
    CONSTRAINT "PK_68782cec65c8eef577c62958273" PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Decks"
(
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    name character varying(255) COLLATE pg_catalog."default" NOT NULL,
    type integer NOT NULL,
    user_id uuid NOT NULL,
    creation_date timestamp without time zone NOT NULL DEFAULT now(),
    cards json NOT NULL,
    played_number integer NOT NULL DEFAULT 0,
    ctype integer NOT NULL DEFAULT 0,
    "updateDate" timestamp without time zone NOT NULL DEFAULT now(),
    state integer NOT NULL DEFAULT 0,
    organization_id uuid,
    CONSTRAINT "PK_001f26cb3ec39c1f25269943473" PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Games"
(
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    gamecode character varying(10) COLLATE pg_catalog."default" NOT NULL,
    maxplayers integer NOT NULL,
    logintype integer NOT NULL DEFAULT 0,
    boardsize integer NOT NULL DEFAULT 50,
    "createdBy" uuid NOT NULL,
    organizationid uuid,
    decks jsonb NOT NULL DEFAULT '[]'::jsonb,
    playerids uuid[] NOT NULL DEFAULT '{}'::uuid[],
    "winnerId" uuid,
    state integer NOT NULL DEFAULT 0,
    "createDate" timestamp without time zone NOT NULL DEFAULT now(),
    start_date timestamp without time zone,
    "finishDate" timestamp without time zone,
    "updateDate" timestamp without time zone NOT NULL DEFAULT now(),
    "organizationId" uuid,
    CONSTRAINT "PK_1950492f583d31609c5e9fbbe12" PRIMARY KEY (id),
    CONSTRAINT "UQ_9d52c646079cbe6f242a85c5c41" UNIQUE (gamecode)
);

CREATE TABLE IF NOT EXISTS public."Organizations"
(
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    name character varying(255) COLLATE pg_catalog."default" NOT NULL,
    contactfname character varying(100) COLLATE pg_catalog."default" NOT NULL,
    contactlname character varying(100) COLLATE pg_catalog."default" NOT NULL,
    contactphone character varying(20) COLLATE pg_catalog."default" NOT NULL,
    contactemail character varying(255) COLLATE pg_catalog."default" NOT NULL,
    state integer NOT NULL DEFAULT 0,
    regdate timestamp without time zone NOT NULL DEFAULT now(),
    "updateDate" timestamp without time zone NOT NULL DEFAULT now(),
    url character varying(500) COLLATE pg_catalog."default",
    userinorg integer NOT NULL DEFAULT 0,
    "maxOrganizationalDecks" integer,
    CONSTRAINT "PK_e0690a31419f6666194423526f2" PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Users"
(
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    orgid uuid,
    username character varying(100) COLLATE pg_catalog."default" NOT NULL,
    password character varying(255) COLLATE pg_catalog."default" NOT NULL,
    email character varying(255) COLLATE pg_catalog."default" NOT NULL,
    fname character varying(100) COLLATE pg_catalog."default" NOT NULL,
    lname character varying(100) COLLATE pg_catalog."default" NOT NULL,
    token character varying(255) COLLATE pg_catalog."default",
    "TokenExpires" timestamp without time zone,
    phone character varying(20) COLLATE pg_catalog."default",
    state integer NOT NULL DEFAULT 0,
    regdate timestamp without time zone NOT NULL DEFAULT now(),
    "updateDate" timestamp without time zone NOT NULL DEFAULT now(),
    "Orglogindate" timestamp without time zone,
    CONSTRAINT "PK_16d4f7d636df336db11d87413e3" PRIMARY KEY (id),
    CONSTRAINT "UQ_3c3ab3f49a87e6ddb607f3c4945" UNIQUE (email),
    CONSTRAINT "UQ_ffc81a3b97dcbf8e320d5106c0d" UNIQUE (username)
);

CREATE TABLE IF NOT EXISTS public.migrations
(
    id serial NOT NULL,
    "timestamp" bigint NOT NULL,
    name character varying COLLATE pg_catalog."default" NOT NULL,
    CONSTRAINT "PK_8c82d7f526340ab734260ea46be" PRIMARY KEY (id)
);

ALTER TABLE IF EXISTS public."Decks"
    ADD CONSTRAINT "FK_06ee28f90d68543a03b14aebe13" FOREIGN KEY (organization_id)
    REFERENCES public."Organizations" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;


ALTER TABLE IF EXISTS public."Decks"
    ADD CONSTRAINT "FK_a39059433e29882e1309d3a5e70" FOREIGN KEY (user_id)
    REFERENCES public."Users" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;


ALTER TABLE IF EXISTS public."Games"
    ADD CONSTRAINT "FK_330362bff8b25bb573f31fb4023" FOREIGN KEY ("winnerId")
    REFERENCES public."Users" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;


ALTER TABLE IF EXISTS public."Games"
    ADD CONSTRAINT "FK_e3c4e8898fa026a5551aefc4f62" FOREIGN KEY ("organizationId")
    REFERENCES public."Organizations" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;


ALTER TABLE IF EXISTS public."Games"
    ADD CONSTRAINT "FK_f32db60863a8a393b30aa222cd5" FOREIGN KEY ("createdBy")
    REFERENCES public."Users" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;

END;