-- Retention Campaigns Tables
CREATE TABLE IF NOT EXISTS retention_campaigns (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  target_segment ENUM('dormant', 'casual', 'vip', 'new', 'atrisk') NOT NULL,
  status ENUM('draft', 'scheduled', 'active', 'completed', 'paused') DEFAULT 'draft',
  start_date TIMESTAMP NOT NULL,
  end_date TIMESTAMP NOT NULL,
  target_audience INT DEFAULT 0,
  reached_audience INT DEFAULT 0,
  conversion_rate DECIMAL(5, 2) DEFAULT 0,
  revenue DECIMAL(12, 2) DEFAULT 0,
  message LONGTEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_status (status),
  INDEX idx_segment (target_segment),
  INDEX idx_created_at (created_at)
);

CREATE TABLE IF NOT EXISTS campaign_offers (
  id INT AUTO_INCREMENT PRIMARY KEY,
  campaign_id INT NOT NULL,
  offer_text VARCHAR(255) NOT NULL,
  offer_type ENUM('bonus', 'free_spins', 'cashback', 'deposit_match', 'tournament_entry') DEFAULT 'bonus',
  value DECIMAL(10, 2),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (campaign_id) REFERENCES retention_campaigns(id) ON DELETE CASCADE,
  INDEX idx_campaign_id (campaign_id)
);

CREATE TABLE IF NOT EXISTS campaign_channels (
  id INT AUTO_INCREMENT PRIMARY KEY,
  campaign_id INT NOT NULL,
  channel ENUM('email', 'sms', 'push', 'ingame', 'social') NOT NULL,
  sent_count INT DEFAULT 0,
  opened_count INT DEFAULT 0,
  clicked_count INT DEFAULT 0,
  converted_count INT DEFAULT 0,
  revenue DECIMAL(12, 2) DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (campaign_id) REFERENCES retention_campaigns(id) ON DELETE CASCADE,
  UNIQUE KEY unique_campaign_channel (campaign_id, channel),
  INDEX idx_campaign_id (campaign_id)
);

-- Activity Feed Tables
CREATE TABLE IF NOT EXISTS player_activities (
  id VARCHAR(50) PRIMARY KEY,
  user_id INT NOT NULL,
  event_type ENUM('login', 'logout', 'deposit', 'withdrawal', 'win', 'game_play') NOT NULL,
  game_id VARCHAR(100),
  game_name VARCHAR(255),
  amount DECIMAL(12, 2),
  ip_address VARCHAR(45),
  device_type VARCHAR(100),
  user_agent LONGTEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_user_id (user_id),
  INDEX idx_event_type (event_type),
  INDEX idx_created_at (created_at),
  INDEX idx_user_event (user_id, event_type)
);

CREATE TABLE IF NOT EXISTS activity_metrics (
  id INT AUTO_INCREMENT PRIMARY KEY,
  metric_hour TIMESTAMP NOT NULL UNIQUE,
  logins INT DEFAULT 0,
  deposits INT DEFAULT 0,
  withdrawals INT DEFAULT 0,
  wins INT DEFAULT 0,
  game_plays INT DEFAULT 0,
  total_revenue DECIMAL(12, 2) DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_metric_hour (metric_hour)
);

-- Revenue Forecasting Tables
CREATE TABLE IF NOT EXISTS revenue_forecasts (
  id INT AUTO_INCREMENT PRIMARY KEY,
  forecast_date DATE NOT NULL,
  actual_revenue DECIMAL(12, 2),
  forecasted_revenue DECIMAL(12, 2) NOT NULL,
  confidence_high DECIMAL(12, 2) NOT NULL,
  confidence_low DECIMAL(12, 2) NOT NULL,
  confidence_level INT DEFAULT 95,
  model_type ENUM('ml', 'arima', 'exponential') DEFAULT 'ml',
  accuracy DECIMAL(5, 2),
  trend ENUM('up', 'down', 'stable') DEFAULT 'stable',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY unique_forecast_date (forecast_date, model_type),
  INDEX idx_forecast_date (forecast_date),
  INDEX idx_model_type (model_type)
);

CREATE TABLE IF NOT EXISTS forecast_metrics (
  id INT AUTO_INCREMENT PRIMARY KEY,
  metric_date DATE NOT NULL UNIQUE,
  model_type ENUM('ml', 'arima', 'exponential') DEFAULT 'ml',
  accuracy DECIMAL(5, 2),
  mape DECIMAL(5, 2),
  rmse DECIMAL(12, 2),
  last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_metric_date (metric_date),
  INDEX idx_model_type (model_type)
);

CREATE TABLE IF NOT EXISTS forecast_insights (
  id INT AUTO_INCREMENT PRIMARY KEY,
  insight_type VARCHAR(50) NOT NULL,
  title VARCHAR(255) NOT NULL,
  description LONGTEXT NOT NULL,
  recommendation LONGTEXT NOT NULL,
  priority ENUM('low', 'medium', 'high', 'critical') DEFAULT 'medium',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_created_at (created_at),
  INDEX idx_priority (priority)
);
