CREATE TABLE `achievements` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(128) NOT NULL,
	`description` text,
	`icon` text,
	`category` enum('gameplay','social','spending','milestone','special') NOT NULL,
	`condition` text NOT NULL,
	`rewardSc` int NOT NULL DEFAULT 0,
	`rewardGc` int NOT NULL DEFAULT 0,
	`isActive` boolean NOT NULL DEFAULT true,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `achievements_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `ai_generated_games` (
	`id` int AUTO_INCREMENT NOT NULL,
	`originalGameId` int,
	`originalGameUrl` text,
	`gameName` varchar(255) NOT NULL,
	`gameSlug` varchar(255) NOT NULL,
	`description` text,
	`aiAnalysis` json,
	`reelSymbols` json,
	`bonusFeatures` json,
	`thumbnailUrl` text,
	`reelGraphicsUrl` json,
	`symbolGraphicsUrl` json,
	`rtp` decimal(5,2) NOT NULL DEFAULT 96,
	`volatility` enum('low','medium','high') NOT NULL DEFAULT 'medium',
	`isActive` boolean NOT NULL DEFAULT true,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `ai_generated_games_id` PRIMARY KEY(`id`),
	CONSTRAINT `ai_generated_games_gameSlug_unique` UNIQUE(`gameSlug`)
);
--> statement-breakpoint
CREATE TABLE `ai_social_content` (
	`id` int AUTO_INCREMENT NOT NULL,
	`createdBy` int NOT NULL,
	`platform` enum('facebook','instagram','twitter','tiktok') NOT NULL,
	`prompt` text NOT NULL,
	`imageUrl` text NOT NULL,
	`caption` text,
	`hashtags` text,
	`isPublished` boolean NOT NULL DEFAULT false,
	`publishedAt` timestamp,
	`impressions` int NOT NULL DEFAULT 0,
	`clicks` int NOT NULL DEFAULT 0,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `ai_social_content_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `friends` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`friendId` int NOT NULL,
	`status` enum('pending','accepted','blocked') NOT NULL DEFAULT 'pending',
	`requestedAt` timestamp NOT NULL DEFAULT (now()),
	`acceptedAt` timestamp,
	CONSTRAINT `friends_id` PRIMARY KEY(`id`),
	CONSTRAINT `idx_friends_unique` UNIQUE(`userId`,`friendId`)
);
--> statement-breakpoint
CREATE TABLE `game_analytics` (
	`id` int AUTO_INCREMENT NOT NULL,
	`gameId` int NOT NULL,
	`totalSpins` int NOT NULL DEFAULT 0,
	`totalWagered` decimal(15,2) NOT NULL DEFAULT 0,
	`totalWon` decimal(15,2) NOT NULL DEFAULT 0,
	`totalRevenue` decimal(15,2) NOT NULL DEFAULT 0,
	`uniquePlayers` int NOT NULL DEFAULT 0,
	`avgBetSize` decimal(10,2) NOT NULL DEFAULT 0,
	`rtp` decimal(5,2) NOT NULL DEFAULT 96,
	`lastUpdated` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `game_analytics_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `game_chat` (
	`id` int AUTO_INCREMENT NOT NULL,
	`gameId` int,
	`userId` int NOT NULL,
	`message` text NOT NULL,
	`isPublic` boolean NOT NULL DEFAULT true,
	`recipientId` int,
	`isPinned` boolean NOT NULL DEFAULT false,
	`isModerated` boolean NOT NULL DEFAULT false,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `game_chat_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `player_profiles` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`bio` text,
	`avatarUrl` text,
	`bannerUrl` text,
	`totalSpins` int NOT NULL DEFAULT 0,
	`totalWins` decimal(15,2) NOT NULL DEFAULT 0,
	`favoriteGame` varchar(128),
	`level` int NOT NULL DEFAULT 1,
	`experience` int NOT NULL DEFAULT 0,
	`isPublic` boolean NOT NULL DEFAULT true,
	`facebookId` varchar(255),
	`instagramHandle` varchar(255),
	`twitterHandle` varchar(255),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `player_profiles_id` PRIMARY KEY(`id`),
	CONSTRAINT `player_profiles_userId_unique` UNIQUE(`userId`)
);
--> statement-breakpoint
CREATE TABLE `referrals` (
	`id` int AUTO_INCREMENT NOT NULL,
	`referrerId` int NOT NULL,
	`refereeId` int NOT NULL,
	`referralCode` varchar(32) NOT NULL,
	`referralLink` text NOT NULL,
	`source` enum('facebook','messenger','sms','twitter','instagram','email','other') NOT NULL,
	`status` enum('pending','completed','expired') NOT NULL DEFAULT 'pending',
	`referrerRewardGiven` boolean NOT NULL DEFAULT false,
	`refereeRewardGiven` boolean NOT NULL DEFAULT false,
	`rewardAmount` int NOT NULL DEFAULT 1,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`completedAt` timestamp,
	CONSTRAINT `referrals_id` PRIMARY KEY(`id`),
	CONSTRAINT `referrals_referralCode_unique` UNIQUE(`referralCode`)
);
--> statement-breakpoint
CREATE TABLE `user_achievements` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`achievementId` int NOT NULL,
	`unlockedAt` timestamp NOT NULL DEFAULT (now()),
	`rewardClaimed` boolean NOT NULL DEFAULT false,
	CONSTRAINT `user_achievements_id` PRIMARY KEY(`id`),
	CONSTRAINT `idx_user_achievements_unique` UNIQUE(`userId`,`achievementId`)
);
--> statement-breakpoint
CREATE TABLE `win_share_events` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`gameId` int NOT NULL,
	`winAmount` decimal(15,2) NOT NULL,
	`shareMessage` text NOT NULL,
	`platform` enum('facebook','messenger','sms','twitter') NOT NULL,
	`referralCode` varchar(32) NOT NULL,
	`sharedAt` timestamp NOT NULL DEFAULT (now()),
	`clickCount` int NOT NULL DEFAULT 0,
	CONSTRAINT `win_share_events_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE INDEX `idx_ai_games_slug` ON `ai_generated_games` (`gameSlug`);--> statement-breakpoint
CREATE INDEX `idx_social_content_platform` ON `ai_social_content` (`platform`);--> statement-breakpoint
CREATE INDEX `idx_friends_userId` ON `friends` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_friends_friendId` ON `friends` (`friendId`);--> statement-breakpoint
CREATE INDEX `idx_game_analytics_gameId` ON `game_analytics` (`gameId`);--> statement-breakpoint
CREATE INDEX `idx_game_chat_gameId` ON `game_chat` (`gameId`);--> statement-breakpoint
CREATE INDEX `idx_game_chat_userId` ON `game_chat` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_game_chat_createdAt` ON `game_chat` (`createdAt`);--> statement-breakpoint
CREATE INDEX `idx_player_profiles_userId` ON `player_profiles` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_referrals_referrerId` ON `referrals` (`referrerId`);--> statement-breakpoint
CREATE INDEX `idx_referrals_refereeId` ON `referrals` (`refereeId`);--> statement-breakpoint
CREATE INDEX `idx_referrals_code` ON `referrals` (`referralCode`);--> statement-breakpoint
CREATE INDEX `idx_user_achievements_userId` ON `user_achievements` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_user_achievements_achievementId` ON `user_achievements` (`achievementId`);--> statement-breakpoint
CREATE INDEX `idx_win_share_userId` ON `win_share_events` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_win_share_gameId` ON `win_share_events` (`gameId`);