CREATE TABLE `audit_logs` (
	`id` bigint AUTO_INCREMENT NOT NULL,
	`actorId` int,
	`actorRole` varchar(32),
	`targetUserId` int,
	`action` varchar(128) NOT NULL,
	`category` enum('auth','wallet','game','admin','kyc','staff','system','fraud') NOT NULL,
	`details` json,
	`ipAddress` varchar(64),
	`userAgent` text,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `audit_logs_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `bingo_cards` (
	`id` bigint AUTO_INCREMENT NOT NULL,
	`sessionId` int NOT NULL,
	`userId` int NOT NULL,
	`roomId` int NOT NULL,
	`numbers` json NOT NULL,
	`daubed` json NOT NULL,
	`hasBingo` boolean NOT NULL DEFAULT false,
	`winAmount` decimal(18,2) DEFAULT '0.00',
	`purchasedAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `bingo_cards_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `bingo_rooms` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(128) NOT NULL,
	`currency` enum('GC','SC') NOT NULL,
	`ticketPrice` decimal(10,2) NOT NULL,
	`maxCards` int NOT NULL DEFAULT 4,
	`jackpot` decimal(18,2) NOT NULL DEFAULT '0.00',
	`jackpotBallLimit` int NOT NULL DEFAULT 76,
	`status` enum('open','calling','finished','paused') NOT NULL DEFAULT 'open',
	`currentSession` int,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `bingo_rooms_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `bingo_sessions` (
	`id` int AUTO_INCREMENT NOT NULL,
	`roomId` int NOT NULL,
	`calledBalls` json NOT NULL,
	`ballCount` int NOT NULL DEFAULT 0,
	`prizePool` decimal(18,2) NOT NULL DEFAULT '0.00',
	`jackpotWon` boolean NOT NULL DEFAULT false,
	`winners` json,
	`status` enum('active','finished') NOT NULL DEFAULT 'active',
	`startedAt` timestamp NOT NULL DEFAULT (now()),
	`finishedAt` timestamp,
	CONSTRAINT `bingo_sessions_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `casino_games` (
	`id` int AUTO_INCREMENT NOT NULL,
	`slug` varchar(128) NOT NULL,
	`title` varchar(255) NOT NULL,
	`provider` varchar(128) NOT NULL,
	`category` enum('slots','table','live','jackpot','new','popular','crash','instant') NOT NULL,
	`tags` json,
	`thumbnailUrl` text,
	`bannerUrl` text,
	`rtp` float NOT NULL DEFAULT 96,
	`volatility` enum('low','medium','high','very_high') NOT NULL DEFAULT 'medium',
	`minBetGc` decimal(10,2) NOT NULL DEFAULT '1.00',
	`maxBetGc` decimal(10,2) NOT NULL DEFAULT '1000.00',
	`minBetSc` decimal(10,2) NOT NULL DEFAULT '0.10',
	`maxBetSc` decimal(10,2) NOT NULL DEFAULT '100.00',
	`isActive` boolean NOT NULL DEFAULT true,
	`isFeatured` boolean NOT NULL DEFAULT false,
	`isNew` boolean NOT NULL DEFAULT false,
	`playCount` bigint NOT NULL DEFAULT 0,
	`description` text,
	`features` json,
	`paylines` int,
	`reels` int,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `casino_games_id` PRIMARY KEY(`id`),
	CONSTRAINT `casino_games_slug_unique` UNIQUE(`slug`)
);
--> statement-breakpoint
CREATE TABLE `chat_messages` (
	`id` bigint AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`channel` varchar(64) NOT NULL DEFAULT 'global',
	`message` text NOT NULL,
	`isDeleted` boolean NOT NULL DEFAULT false,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `chat_messages_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `coin_packages` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(128) NOT NULL,
	`gcAmount` decimal(18,2) NOT NULL,
	`scBonusAmount` decimal(18,2) NOT NULL DEFAULT '0.00',
	`priceUsd` decimal(10,2) NOT NULL,
	`isPopular` boolean NOT NULL DEFAULT false,
	`isBestValue` boolean NOT NULL DEFAULT false,
	`isActive` boolean NOT NULL DEFAULT true,
	`sortOrder` int NOT NULL DEFAULT 0,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `coin_packages_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `daily_bonuses` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`gcAmount` decimal(10,2) NOT NULL,
	`scAmount` decimal(10,2) NOT NULL,
	`spinResult` int NOT NULL,
	`claimedAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `daily_bonuses_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `fraud_alerts` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`alertType` enum('abnormal_rtp','rapid_betting','multi_account','bonus_abuse','unusual_withdrawal','velocity','pattern') NOT NULL,
	`severity` enum('low','medium','high','critical') NOT NULL DEFAULT 'medium',
	`description` text NOT NULL,
	`details` json,
	`status` enum('open','investigating','resolved','dismissed') NOT NULL DEFAULT 'open',
	`resolvedBy` int,
	`resolvedAt` timestamp,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `fraud_alerts_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `game_sessions` (
	`id` bigint AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`gameId` int NOT NULL,
	`currency` enum('GC','SC') NOT NULL,
	`betAmount` decimal(18,2) NOT NULL,
	`winAmount` decimal(18,2) NOT NULL DEFAULT '0.00',
	`multiplier` float NOT NULL DEFAULT 0,
	`outcome` json,
	`rtpApplied` float,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `game_sessions_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `kyc_documents` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`docType` enum('passport','drivers_license','national_id','utility_bill','bank_statement','selfie') NOT NULL,
	`fileUrl` text NOT NULL,
	`fileKey` text NOT NULL,
	`status` enum('pending','approved','rejected') NOT NULL DEFAULT 'pending',
	`reviewedBy` int,
	`reviewNote` text,
	`submittedAt` timestamp NOT NULL DEFAULT (now()),
	`reviewedAt` timestamp,
	CONSTRAINT `kyc_documents_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `mini_game_rounds` (
	`id` bigint AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`gameType` enum('plinko','mines','dice','crash','wheel') NOT NULL,
	`currency` enum('GC','SC') NOT NULL,
	`betAmount` decimal(18,2) NOT NULL,
	`winAmount` decimal(18,2) NOT NULL DEFAULT '0.00',
	`multiplier` float NOT NULL DEFAULT 0,
	`serverSeed` varchar(128) NOT NULL,
	`clientSeed` varchar(128),
	`nonce` int NOT NULL DEFAULT 0,
	`outcome` json NOT NULL,
	`isVerified` boolean NOT NULL DEFAULT false,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `mini_game_rounds_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `poker_hands` (
	`id` bigint AUTO_INCREMENT NOT NULL,
	`tableId` int NOT NULL,
	`handNumber` int NOT NULL,
	`players` json NOT NULL,
	`communityCards` json,
	`pot` decimal(18,2) NOT NULL,
	`winners` json,
	`actions` json,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `poker_hands_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `poker_tables` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(128) NOT NULL,
	`gameType` enum('holdem','omaha') NOT NULL DEFAULT 'holdem',
	`currency` enum('GC','SC') NOT NULL,
	`smallBlind` decimal(10,2) NOT NULL,
	`bigBlind` decimal(10,2) NOT NULL,
	`minBuyIn` decimal(10,2) NOT NULL,
	`maxBuyIn` decimal(10,2) NOT NULL,
	`maxPlayers` int NOT NULL DEFAULT 9,
	`currentPlayers` int NOT NULL DEFAULT 0,
	`status` enum('waiting','active','paused','closed') NOT NULL DEFAULT 'waiting',
	`tournamentId` int,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `poker_tables_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `poker_tournaments` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(255) NOT NULL,
	`currency` enum('GC','SC') NOT NULL,
	`buyIn` decimal(10,2) NOT NULL,
	`prizePool` decimal(18,2) NOT NULL DEFAULT '0.00',
	`maxPlayers` int NOT NULL,
	`registeredPlayers` int NOT NULL DEFAULT 0,
	`startTime` timestamp NOT NULL,
	`status` enum('registering','running','finished','cancelled') NOT NULL DEFAULT 'registering',
	`structure` json,
	`payoutStructure` json,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `poker_tournaments_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `recently_played` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`gameId` int NOT NULL,
	`playedAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `recently_played_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `restricted_states` (
	`id` int AUTO_INCREMENT NOT NULL,
	`stateCode` varchar(4) NOT NULL,
	`stateName` varchar(128) NOT NULL,
	`reason` text,
	`isActive` boolean NOT NULL DEFAULT true,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `restricted_states_id` PRIMARY KEY(`id`),
	CONSTRAINT `restricted_states_stateCode_unique` UNIQUE(`stateCode`)
);
--> statement-breakpoint
CREATE TABLE `rtp_overrides` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int,
	`gameId` int,
	`rtpOverride` float NOT NULL,
	`setByAdminId` int NOT NULL,
	`reason` text,
	`expiresAt` timestamp,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `rtp_overrides_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `sc_redemptions` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`scAmount` decimal(18,2) NOT NULL,
	`usdAmount` decimal(18,2) NOT NULL,
	`method` enum('bank_transfer','check','paypal','crypto') NOT NULL,
	`paymentDetails` json,
	`status` enum('pending','approved','processing','completed','rejected') NOT NULL DEFAULT 'pending',
	`reviewedBy` int,
	`reviewNote` text,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`processedAt` timestamp,
	CONSTRAINT `sc_redemptions_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `sport_bets` (
	`id` bigint AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`currency` enum('GC','SC') NOT NULL,
	`betType` enum('single','parlay','teaser') NOT NULL DEFAULT 'single',
	`totalStake` decimal(18,2) NOT NULL,
	`totalOdds` float NOT NULL,
	`potentialPayout` decimal(18,2) NOT NULL,
	`actualPayout` decimal(18,2) DEFAULT '0.00',
	`status` enum('pending','won','lost','cancelled','partial_win','void') NOT NULL DEFAULT 'pending',
	`selections` json NOT NULL,
	`settledAt` timestamp,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `sport_bets_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `sport_events` (
	`id` int AUTO_INCREMENT NOT NULL,
	`sport` varchar(64) NOT NULL,
	`league` varchar(128) NOT NULL,
	`homeTeam` varchar(128) NOT NULL,
	`awayTeam` varchar(128) NOT NULL,
	`startTime` timestamp NOT NULL,
	`status` enum('upcoming','live','finished','cancelled','suspended') NOT NULL DEFAULT 'upcoming',
	`homeScore` int DEFAULT 0,
	`awayScore` int DEFAULT 0,
	`period` varchar(32),
	`odds` json,
	`metadata` json,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `sport_events_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `staff` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`staffId` varchar(32) NOT NULL,
	`pin` varchar(255) NOT NULL,
	`position` varchar(128) NOT NULL,
	`department` varchar(128),
	`hourlyRate` decimal(10,2),
	`isActive` boolean NOT NULL DEFAULT true,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `staff_id` PRIMARY KEY(`id`),
	CONSTRAINT `staff_userId_unique` UNIQUE(`userId`),
	CONSTRAINT `staff_staffId_unique` UNIQUE(`staffId`)
);
--> statement-breakpoint
CREATE TABLE `support_tickets` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`assignedStaffId` int,
	`subject` varchar(255) NOT NULL,
	`category` enum('account','payment','game','bonus','technical','kyc','other') NOT NULL,
	`priority` enum('low','medium','high','urgent') NOT NULL DEFAULT 'medium',
	`status` enum('open','in_progress','waiting_user','resolved','closed') NOT NULL DEFAULT 'open',
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	`resolvedAt` timestamp,
	CONSTRAINT `support_tickets_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `ticket_messages` (
	`id` int AUTO_INCREMENT NOT NULL,
	`ticketId` int NOT NULL,
	`senderId` int NOT NULL,
	`senderRole` enum('user','staff','admin') NOT NULL,
	`message` text NOT NULL,
	`attachmentUrl` text,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `ticket_messages_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `time_clock` (
	`id` int AUTO_INCREMENT NOT NULL,
	`staffId` int NOT NULL,
	`clockIn` timestamp NOT NULL,
	`clockOut` timestamp,
	`hoursWorked` float,
	`notes` text,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `time_clock_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `transactions` (
	`id` bigint AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`type` enum('gc_purchase','gc_bonus','sc_bonus','sc_redeem','game_bet','game_win','sport_bet','sport_win','sport_refund','poker_buyin','poker_win','poker_rake','bingo_purchase','bingo_win','minigame_bet','minigame_win','daily_bonus','admin_credit','admin_debit','referral_bonus','promo_credit') NOT NULL,
	`currency` enum('GC','SC') NOT NULL,
	`amount` decimal(18,2) NOT NULL,
	`balanceBefore` decimal(18,2) NOT NULL,
	`balanceAfter` decimal(18,2) NOT NULL,
	`referenceId` varchar(128),
	`referenceType` varchar(64),
	`description` text,
	`metadata` json,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `transactions_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `wallets` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`gcBalance` decimal(18,2) NOT NULL DEFAULT '0.00',
	`scBalance` decimal(18,2) NOT NULL DEFAULT '0.00',
	`lifetimeGcEarned` decimal(18,2) NOT NULL DEFAULT '0.00',
	`lifetimeScEarned` decimal(18,2) NOT NULL DEFAULT '0.00',
	`lifetimeScRedeemed` decimal(18,2) NOT NULL DEFAULT '0.00',
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `wallets_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
ALTER TABLE `users` MODIFY COLUMN `role` enum('user','staff','admin') NOT NULL DEFAULT 'user';--> statement-breakpoint
ALTER TABLE `users` ADD `username` varchar(64);--> statement-breakpoint
ALTER TABLE `users` ADD `passwordHash` varchar(255);--> statement-breakpoint
ALTER TABLE `users` ADD `kycStatus` enum('none','pending','approved','rejected') DEFAULT 'none' NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `kycLevel` int DEFAULT 0 NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `isBanned` boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `banReason` text;--> statement-breakpoint
ALTER TABLE `users` ADD `isAgeVerified` boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `dateOfBirth` varchar(10);--> statement-breakpoint
ALTER TABLE `users` ADD `stateCode` varchar(4);--> statement-breakpoint
ALTER TABLE `users` ADD `country` varchar(4) DEFAULT 'US';--> statement-breakpoint
ALTER TABLE `users` ADD `phone` varchar(32);--> statement-breakpoint
ALTER TABLE `users` ADD `avatarUrl` text;--> statement-breakpoint
ALTER TABLE `users` ADD `lastIp` varchar(64);--> statement-breakpoint
ALTER TABLE `users` ADD `twoFactorEnabled` boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `twoFactorSecret` varchar(64);--> statement-breakpoint
ALTER TABLE `users` ADD CONSTRAINT `users_username_unique` UNIQUE(`username`);--> statement-breakpoint
CREATE INDEX `idx_audit_actorId` ON `audit_logs` (`actorId`);--> statement-breakpoint
CREATE INDEX `idx_audit_createdAt` ON `audit_logs` (`createdAt`);--> statement-breakpoint
CREATE INDEX `idx_audit_category` ON `audit_logs` (`category`);--> statement-breakpoint
CREATE INDEX `idx_cards_sessionId` ON `bingo_cards` (`sessionId`);--> statement-breakpoint
CREATE INDEX `idx_cards_userId` ON `bingo_cards` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_games_provider` ON `casino_games` (`provider`);--> statement-breakpoint
CREATE INDEX `idx_games_category` ON `casino_games` (`category`);--> statement-breakpoint
CREATE INDEX `idx_chat_channel` ON `chat_messages` (`channel`);--> statement-breakpoint
CREATE INDEX `idx_daily_userId` ON `daily_bonuses` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_fraud_userId` ON `fraud_alerts` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_fraud_status` ON `fraud_alerts` (`status`);--> statement-breakpoint
CREATE INDEX `idx_sessions_userId` ON `game_sessions` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_sessions_gameId` ON `game_sessions` (`gameId`);--> statement-breakpoint
CREATE INDEX `idx_minigame_userId` ON `mini_game_rounds` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_hands_tableId` ON `poker_hands` (`tableId`);--> statement-breakpoint
CREATE INDEX `idx_recent_userId` ON `recently_played` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_redemptions_userId` ON `sc_redemptions` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_bets_userId` ON `sport_bets` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_bets_status` ON `sport_bets` (`status`);--> statement-breakpoint
CREATE INDEX `idx_events_sport` ON `sport_events` (`sport`);--> statement-breakpoint
CREATE INDEX `idx_events_status` ON `sport_events` (`status`);--> statement-breakpoint
CREATE INDEX `idx_tickets_userId` ON `support_tickets` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_tickets_status` ON `support_tickets` (`status`);--> statement-breakpoint
CREATE INDEX `idx_messages_ticketId` ON `ticket_messages` (`ticketId`);--> statement-breakpoint
CREATE INDEX `idx_timeclock_staffId` ON `time_clock` (`staffId`);--> statement-breakpoint
CREATE INDEX `idx_transactions_userId` ON `transactions` (`userId`);--> statement-breakpoint
CREATE INDEX `idx_transactions_createdAt` ON `transactions` (`createdAt`);