-- SerpentRace Database Schema
-- Generated from TypeORM Entity Aggregates
-- This file creates the complete database schema without initial data

-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Create Users table
CREATE TABLE "Users" (
    "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    "orgid" UUID NULL,
    "username" VARCHAR(100) UNIQUE NOT NULL,
    "password" VARCHAR(255) NOT NULL,
    "email" VARCHAR(255) UNIQUE NOT NULL,
    "fname" VARCHAR(100) NOT NULL,
    "lname" VARCHAR(100) NOT NULL,
    "token" VARCHAR(255) NULL,
    "TokenExpires" TIMESTAMP NULL,
    "phone" VARCHAR(20) NULL,
    "state" INTEGER NOT NULL DEFAULT 0,
    "regdate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "Orglogindate" TIMESTAMP NULL
);

-- Create Organizations table
CREATE TABLE "Organizations" (
    "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    "name" VARCHAR(255) NOT NULL,
    "contactfname" VARCHAR(100) NOT NULL,
    "contactlname" VARCHAR(100) NOT NULL,
    "contactphone" VARCHAR(20) NOT NULL,
    "contactemail" VARCHAR(255) NOT NULL,
    "state" INTEGER NOT NULL DEFAULT 0,
    "regdate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "url" VARCHAR(500) NULL,
    "userinorg" INTEGER NOT NULL DEFAULT 0,
    "maxOrganizationalDecks" INTEGER NULL
);

-- Create Decks table
CREATE TABLE "Decks" (
    "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    "name" VARCHAR(255) NOT NULL,
    "type" INTEGER NOT NULL,
    "user_id" UUID NOT NULL,
    "creation_date" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "cards" JSONB NOT NULL DEFAULT '[]',
    "played_number" INTEGER NOT NULL DEFAULT 0,
    "ctype" INTEGER NOT NULL DEFAULT 0,
    "updateDate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "state" INTEGER NOT NULL DEFAULT 0,
    "organization_id" UUID NULL
);

-- Create Chats table
CREATE TABLE "Chats" (
    "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    "type" VARCHAR(50) NOT NULL DEFAULT 'direct',
    "name" VARCHAR(255) NULL,
    "gameId" UUID NULL,
    "createdBy" UUID NULL,
    "users" UUID[] NOT NULL,
    "messages" JSONB NOT NULL DEFAULT '[]',
    "lastActivity" TIMESTAMP NULL,
    "createDate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "state" INTEGER NOT NULL DEFAULT 0,
    "archiveDate" TIMESTAMP NULL
);

-- Create Contacts table
CREATE TABLE "Contacts" (
    "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    "name" VARCHAR(255) NOT NULL,
    "email" VARCHAR(255) NOT NULL,
    "userid" UUID NULL,
    "type" INTEGER NOT NULL,
    "txt" TEXT NOT NULL,
    "state" INTEGER NOT NULL DEFAULT 0,
    "createDate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "adminResponse" TEXT NULL,
    "responseDate" TIMESTAMP NULL,
    "respondedBy" UUID NULL
);

-- Create Games table
CREATE TABLE "Games" (
    "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    "gamecode" VARCHAR(10) UNIQUE NOT NULL,
    "maxplayers" INTEGER NOT NULL,
    "logintype" INTEGER NOT NULL DEFAULT 0,
    "state" INTEGER NOT NULL DEFAULT 0,
    "playerids" UUID[] NOT NULL DEFAULT '{}',
    "decks" JSONB NOT NULL DEFAULT '[]',
    "boardsize" INTEGER NOT NULL DEFAULT 50,
    "createDate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "finishDate" TIMESTAMP NULL,
    "winnerid" UUID NULL,
    "createdBy" UUID NOT NULL,
    "organizationid" UUID NULL
);

-- Add Foreign Key Constraints
ALTER TABLE "Users" 
ADD CONSTRAINT "FK_Users_Organizations" 
FOREIGN KEY ("orgid") REFERENCES "Organizations"("id") ON DELETE SET NULL;

ALTER TABLE "Decks" 
ADD CONSTRAINT "FK_Decks_Users" 
FOREIGN KEY ("user_id") REFERENCES "Users"("id") ON DELETE CASCADE;

ALTER TABLE "Decks" 
ADD CONSTRAINT "FK_Decks_Organizations" 
FOREIGN KEY ("organization_id") REFERENCES "Organizations"("id") ON DELETE SET NULL;

ALTER TABLE "Contacts" 
ADD CONSTRAINT "FK_Contacts_Users" 
FOREIGN KEY ("userid") REFERENCES "Users"("id") ON DELETE SET NULL;

ALTER TABLE "Contacts" 
ADD CONSTRAINT "FK_Contacts_RespondedBy" 
FOREIGN KEY ("respondedBy") REFERENCES "Users"("id") ON DELETE SET NULL;

ALTER TABLE "Chats" 
ADD CONSTRAINT "FK_Chats_CreatedBy" 
FOREIGN KEY ("createdBy") REFERENCES "Users"("id") ON DELETE SET NULL;

ALTER TABLE "Chats" 
ADD CONSTRAINT "FK_Chats_Games" 
FOREIGN KEY ("gameId") REFERENCES "Games"("id") ON DELETE SET NULL;

ALTER TABLE "Games" 
ADD CONSTRAINT "FK_Games_CreatedBy" 
FOREIGN KEY ("createdBy") REFERENCES "Users"("id") ON DELETE CASCADE;

ALTER TABLE "Games" 
ADD CONSTRAINT "FK_Games_Organizations" 
FOREIGN KEY ("organizationid") REFERENCES "Organizations"("id") ON DELETE SET NULL;

ALTER TABLE "Games" 
ADD CONSTRAINT "FK_Games_Winner" 
FOREIGN KEY ("winnerid") REFERENCES "Users"("id") ON DELETE SET NULL;

-- Create Indexes for Performance
CREATE INDEX "IDX_Users_Username" ON "Users" ("username");
CREATE INDEX "IDX_Users_Email" ON "Users" ("email");
CREATE INDEX "IDX_Users_OrgId" ON "Users" ("orgid");
CREATE INDEX "IDX_Users_State" ON "Users" ("state");

CREATE INDEX "IDX_Organizations_Name" ON "Organizations" ("name");
CREATE INDEX "IDX_Organizations_State" ON "Organizations" ("state");

CREATE INDEX "IDX_Decks_UserId" ON "Decks" ("user_id");
CREATE INDEX "IDX_Decks_Type" ON "Decks" ("type");
CREATE INDEX "IDX_Decks_CType" ON "Decks" ("ctype");
CREATE INDEX "IDX_Decks_State" ON "Decks" ("state");
CREATE INDEX "IDX_Decks_OrganizationId" ON "Decks" ("organization_id");

CREATE INDEX "IDX_Chats_Type" ON "Chats" ("type");
CREATE INDEX "IDX_Chats_State" ON "Chats" ("state");
CREATE INDEX "IDX_Chats_GameId" ON "Chats" ("gameId");
CREATE INDEX "IDX_Chats_CreatedBy" ON "Chats" ("createdBy");

CREATE INDEX "IDX_Contacts_Type" ON "Contacts" ("type");
CREATE INDEX "IDX_Contacts_State" ON "Contacts" ("state");
CREATE INDEX "IDX_Contacts_UserId" ON "Contacts" ("userid");

CREATE INDEX "IDX_Games_GameCode" ON "Games" ("gamecode");
CREATE INDEX "IDX_Games_State" ON "Games" ("state");
CREATE INDEX "IDX_Games_CreatedBy" ON "Games" ("createdBy");
CREATE INDEX "IDX_Games_OrganizationId" ON "Games" ("organizationid");

-- Comments for documentation
COMMENT ON TABLE "Users" IS 'User accounts with authentication and profile information';
COMMENT ON TABLE "Organizations" IS 'Organizations that can have multiple users and premium features';
COMMENT ON TABLE "Decks" IS 'Card decks for the game, can be public, private, or organizational';
COMMENT ON TABLE "Chats" IS 'Chat system supporting direct messages, groups, and game chats';
COMMENT ON TABLE "Contacts" IS 'Contact form submissions and support tickets';
COMMENT ON TABLE "Games" IS 'Game sessions with players, decks, and game state';

-- Enum value comments
COMMENT ON COLUMN "Users"."state" IS '0=REGISTERED_NOT_VERIFIED, 1=VERIFIED_REGULAR, 2=VERIFIED_PREMIUM, 3=SOFT_DELETE, 4=DEACTIVATED, 5=ADMIN';
COMMENT ON COLUMN "Organizations"."state" IS '0=REGISTERED, 1=ACTIVE, 2=SOFT_DELETE';
COMMENT ON COLUMN "Decks"."type" IS '0=LUCK, 1=JOKER, 2=QUESTION';
COMMENT ON COLUMN "Decks"."ctype" IS '0=PUBLIC, 1=PRIVATE, 2=ORGANIZATION';
COMMENT ON COLUMN "Decks"."state" IS '0=ACTIVE, 1=SOFT_DELETE';
COMMENT ON COLUMN "Chats"."type" IS 'direct, group, game';
COMMENT ON COLUMN "Chats"."state" IS '0=ACTIVE, 1=ARCHIVE, 2=SOFT_DELETE';
COMMENT ON COLUMN "Contacts"."type" IS '0=BUG, 1=PROBLEM, 2=QUESTION, 3=SALES, 4=OTHER';
COMMENT ON COLUMN "Contacts"."state" IS '0=ACTIVE, 1=RESOLVED, 2=SOFT_DELETE';
COMMENT ON COLUMN "Games"."state" IS '0=WAITING, 1=ACTIVE, 2=FINISHED, 3=CANCELLED';
COMMENT ON COLUMN "Games"."logintype" IS '0=PUBLIC, 1=PRIVATE, 2=ORGANIZATION';

-- Grant permissions for application user
-- Note: Replace 'serpentrace_app' with your actual application database user
-- GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO serpentrace_app;
-- GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO serpentrace_app;
-- GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO serpentrace_app;