CREATE TABLE `pool_shark_achievements` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`achievementType` enum('first_win','five_wins','ten_wins','fifty_wins','hundred_wins','pool_shark','hustler','legend','perfect_break','high_accuracy','winning_streak','prize_master') NOT NULL,
	`title` varchar(100) NOT NULL,
	`description` text,
	`badgeUrl` text,
	`unlockedAt` timestamp NOT NULL DEFAULT (now()),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `pool_shark_achievements_id` PRIMARY KEY(`id`),
	CONSTRAINT `idx_pool_shark_achievements_unique` UNIQUE(`userId`,`achievementType`)
);
--> statement-breakpoint
CREATE TABLE `pool_shark_leaderboards` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`totalMatches` int NOT NULL DEFAULT 0,
	`totalWins` int NOT NULL DEFAULT 0,
	`totalLosses` int NOT NULL DEFAULT 0,
	`winRate` decimal(5,2) NOT NULL DEFAULT '0.00',
	`totalPrizeWon` decimal(15,2) NOT NULL DEFAULT '0.00',
	`highestBreak` int NOT NULL DEFAULT 0,
	`averageAccuracy` decimal(5,2) NOT NULL DEFAULT '0.00',
	`totalBallsPocketed` int NOT NULL DEFAULT 0,
	`rank` int NOT NULL DEFAULT 0,
	`lastUpdated` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `pool_shark_leaderboards_id` PRIMARY KEY(`id`),
	CONSTRAINT `pool_shark_leaderboards_userId_unique` UNIQUE(`userId`)
);
--> statement-breakpoint
CREATE TABLE `pool_shark_matches` (
	`id` int AUTO_INCREMENT NOT NULL,
	`tableId` int NOT NULL,
	`status` enum('waiting','in_progress','completed','cancelled') NOT NULL DEFAULT 'waiting',
	`winnerId` int,
	`winnerBreak` int,
	`totalPrizePool` decimal(10,2) NOT NULL,
	`startedAt` timestamp,
	`completedAt` timestamp,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `pool_shark_matches_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `pool_shark_participants` (
	`id` int AUTO_INCREMENT NOT NULL,
	`matchId` int NOT NULL,
	`userId` int NOT NULL,
	`entryFee` decimal(10,2) NOT NULL,
	`position` int,
	`prizeWon` decimal(10,2) NOT NULL DEFAULT '0.00',
	`ballsPocketed` int NOT NULL DEFAULT 0,
	`highestBreak` int NOT NULL DEFAULT 0,
	`shotsAttempted` int NOT NULL DEFAULT 0,
	`shotsMade` int NOT NULL DEFAULT 0,
	`accuracy` decimal(5,2) NOT NULL DEFAULT '0.00',
	`joinedAt` timestamp NOT NULL DEFAULT (now()),
	`leftAt` timestamp,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `pool_shark_participants_id` PRIMARY KEY(`id`),
	CONSTRAINT `idx_pool_shark_participants_unique` UNIQUE(`matchId`,`userId`)
);
--> statement-breakpoint
CREATE TABLE `pool_shark_tables` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(100) NOT NULL,
	`description` text,
	`entryFee` decimal(10,2) NOT NULL,
	`maxPlayers` int NOT NULL DEFAULT 2,
	`minPlayers` int NOT NULL DEFAULT 2,
	`prizeMultiplier` decimal(5,2) NOT NULL DEFAULT '2.00',
	`status` enum('active','inactive','archived') NOT NULL DEFAULT 'active',
	`sortOrder` int NOT NULL DEFAULT 0,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `pool_shark_tables_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `pool_shark_waiting_lists` (
	`id` int AUTO_INCREMENT NOT NULL,
	`tableId` int NOT NULL,
	`userId` int NOT NULL,
	`position` int NOT NULL,
	`joinedAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `pool_shark_waiting_lists_id` PRIMARY KEY(`id`),
	CONSTRAINT `idx_pool_shark_waiting_lists_unique` UNIQUE(`tableId`,`userId`)
);
--> statement-breakpoint
CREATE INDEX `idx_pool_shark_achievements_userId` ON `pool_shark_achievements` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_achievements_type` ON `pool_shark_achievements` (`achievementType`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_leaderboards_userId` ON `pool_shark_leaderboards` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_leaderboards_rank` ON `pool_shark_leaderboards` (`rank`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_leaderboards_totalWins` ON `pool_shark_leaderboards` (`totalWins`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_matches_tableId` ON `pool_shark_matches` (`tableId`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_matches_status` ON `pool_shark_matches` (`status`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_matches_winnerId` ON `pool_shark_matches` (`winnerId`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_matches_createdAt` ON `pool_shark_matches` (`createdAt`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_participants_matchId` ON `pool_shark_participants` (`matchId`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_participants_userId` ON `pool_shark_participants` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_tables_status` ON `pool_shark_tables` (`status`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_tables_sortOrder` ON `pool_shark_tables` (`sortOrder`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_waiting_lists_tableId` ON `pool_shark_waiting_lists` (`tableId`);--> statement-breakpoint
CREATE INDEX `idx_pool_shark_waiting_lists_userId` ON `pool_shark_waiting_lists` (`userId`);