-- Create all_games table
CREATE TABLE IF NOT EXISTS `all_games` (
  `id` int NOT NULL AUTO_INCREMENT,
  `gameId` varchar(100) NOT NULL UNIQUE,
  `gameName` varchar(255) NOT NULL,
  `gameType` varchar(50) NOT NULL,
  `category` varchar(50) NOT NULL DEFAULT 'slots',
  `provider` varchar(100) NOT NULL,
  `rtp` decimal(5,2) NOT NULL DEFAULT 96.00,
  `volatility` varchar(20) NOT NULL DEFAULT 'medium',
  `paylines` int NOT NULL DEFAULT 20,
  `reels` int NOT NULL DEFAULT 5,
  `minBet` decimal(10,2) NOT NULL DEFAULT 0.01,
  `maxBet` decimal(10,2) NOT NULL DEFAULT 100.00,
  `jackpot` decimal(15,2) NOT NULL DEFAULT 0,
  `hasIcon` tinyint NOT NULL DEFAULT 0,
  `iconUrl` text,
  `isActive` tinyint NOT NULL DEFAULT 1,
  `isFeatured` tinyint NOT NULL DEFAULT 0,
  `playCount` int NOT NULL DEFAULT 0,
  `totalWinnings` decimal(15,2) NOT NULL DEFAULT 0,
  `averageWin` decimal(10,2) NOT NULL DEFAULT 0,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_all_games_gameId` (`gameId`),
  KEY `idx_all_games_provider` (`provider`),
  KEY `idx_all_games_category` (`category`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create rate_limit_log table
CREATE TABLE IF NOT EXISTS `rate_limit_log` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `ipAddress` varchar(64) NOT NULL,
  `endpoint` varchar(255) NOT NULL,
  `userId` int,
  `requestCount` int NOT NULL DEFAULT 1,
  `windowStart` timestamp NOT NULL,
  `windowEnd` timestamp NOT NULL,
  `isBlocked` tinyint NOT NULL DEFAULT 0,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_ratelimit_ip_endpoint` (`ipAddress`, `endpoint`),
  KEY `idx_ratelimit_isBlocked` (`isBlocked`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create game_library table if it doesn't exist
CREATE TABLE IF NOT EXISTS `game_library` (
  `id` int NOT NULL AUTO_INCREMENT,
  `userId` int NOT NULL,
  `gameId` varchar(100) NOT NULL,
  `lastPlayedAt` timestamp NULL,
  `favorited` tinyint NOT NULL DEFAULT 0,
  `playCount` int NOT NULL DEFAULT 0,
  `totalBet` decimal(15,2) NOT NULL DEFAULT 0,
  `totalWin` decimal(15,2) NOT NULL DEFAULT 0,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_game_library_userId` (`userId`),
  KEY `idx_game_library_gameId` (`gameId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create game_stats table if it doesn't exist
CREATE TABLE IF NOT EXISTS `game_stats` (
  `id` int NOT NULL AUTO_INCREMENT,
  `gameId` varchar(100) NOT NULL,
  `totalPlayers` int NOT NULL DEFAULT 0,
  `totalBets` decimal(15,2) NOT NULL DEFAULT 0,
  `totalWins` decimal(15,2) NOT NULL DEFAULT 0,
  `averageRtp` decimal(5,2) NOT NULL DEFAULT 96.00,
  `lastUpdated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_game_stats_gameId` (`gameId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create game_recommendations table if it doesn't exist
CREATE TABLE IF NOT EXISTS `game_recommendations` (
  `id` int NOT NULL AUTO_INCREMENT,
  `userId` int NOT NULL,
  `gameId` varchar(100) NOT NULL,
  `score` decimal(5,2) NOT NULL DEFAULT 0,
  `reason` varchar(255),
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_recommendations_userId` (`userId`),
  KEY `idx_recommendations_gameId` (`gameId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
