-- Create admin_notifications table
CREATE TABLE IF NOT EXISTS `admin_notifications` (
  `id` int NOT NULL AUTO_INCREMENT,
  `type` enum('fraud_alert','large_win','kyc_pending','kyc_approved','kyc_rejected','system_alert','user_report','payment_issue','game_error') NOT NULL,
  `title` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `severity` enum('low','medium','high','critical') NOT NULL DEFAULT 'medium',
  `relatedUserId` int,
  `relatedEntityId` varchar(255),
  `relatedEntityType` varchar(64),
  `data` json,
  `isRead` tinyint NOT NULL DEFAULT 0,
  `readAt` timestamp NULL,
  `actionUrl` text,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_admin_notifications_type` (`type`),
  KEY `idx_admin_notifications_severity` (`severity`),
  KEY `idx_admin_notifications_isRead` (`isRead`),
  KEY `idx_admin_notifications_createdAt` (`createdAt`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create webhook_configs table
CREATE TABLE IF NOT EXISTS `webhook_configs` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `url` text NOT NULL,
  `events` json NOT NULL,
  `headers` json,
  `isActive` tinyint NOT NULL DEFAULT 1,
  `secret` varchar(255) NOT NULL,
  `retryCount` int NOT NULL DEFAULT 3,
  `retryDelay` int NOT NULL DEFAULT 300,
  `timeout` int NOT NULL DEFAULT 30,
  `createdBy` int NOT NULL,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_webhook_configs_isActive` (`isActive`),
  KEY `idx_webhook_configs_createdBy` (`createdBy`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create webhook_deliveries table
CREATE TABLE IF NOT EXISTS `webhook_deliveries` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `webhookConfigId` int NOT NULL,
  `eventType` varchar(128) NOT NULL,
  `payload` json NOT NULL,
  `statusCode` int,
  `responseBody` text,
  `error` text,
  `attemptNumber` int NOT NULL DEFAULT 1,
  `nextRetryAt` timestamp NULL,
  `deliveredAt` timestamp NULL,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_webhook_deliveries_webhookConfigId` (`webhookConfigId`),
  KEY `idx_webhook_deliveries_eventType` (`eventType`),
  KEY `idx_webhook_deliveries_createdAt` (`createdAt`),
  KEY `idx_webhook_deliveries_nextRetryAt` (`nextRetryAt`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create admin_notification_preferences table
CREATE TABLE IF NOT EXISTS `admin_notification_preferences` (
  `id` int NOT NULL AUTO_INCREMENT,
  `adminId` int NOT NULL,
  `notificationType` varchar(128) NOT NULL,
  `enabled` tinyint NOT NULL DEFAULT 1,
  `channels` json NOT NULL,
  `quietHoursStart` varchar(5),
  `quietHoursEnd` varchar(5),
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_admin_notification_preferences_adminId` (`adminId`),
  KEY `idx_admin_notification_preferences_notificationType` (`notificationType`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
