// Corrosion Platform — TypeScript Interfaces

export interface User {
  id: string
  email: string
  username: string
  totp_enabled: boolean
  email_verified: boolean
  is_super_admin: boolean
}

export interface License {
  id: string
  license_key: string
  status: 'active' | 'suspended' | 'expired' | 'revoked'
  server_name: string | null
  subdomain: string | null
  custom_domain: string | null
  modules_enabled: string[]
  webstore_active: boolean
  created_at: string
  expires_at: string | null
}

export interface AuthResponse {
  access_token: string
  refresh_token: string
  requires_totp: boolean
  user: User
  license: License | null
}

export interface ServerConnection {
  id: string
  license_id: string
  connection_type: 'amp' | 'pterodactyl' | 'bare_metal'
  server_ip: string | null
  server_port: number | null
  game_port: number | null
  connection_status: 'connected' | 'degraded' | 'offline'
  plugin_last_seen: string | null
  companion_last_seen: string | null
}

export interface ServerConfig {
  id: string
  license_id: string
  server_name: string
  max_players: number | null
  world_size: number | null
  current_seed: number | null
  auto_restart_enabled: boolean
  auto_restart_cron: string | null
  crash_recovery_enabled: boolean
  force_wipe_eligible: boolean
  auto_update_on_force_wipe: boolean
  config_overrides: Record<string, string>
}

export interface ServerStats {
  player_count: number
  max_players: number
  fps: number
  entity_count: number
  uptime_seconds: number
  memory_usage_mb: number
  recorded_at: string
}

export interface WipeProfile {
  id: string
  license_id: string
  profile_name: string
  description: string | null
  pre_wipe_config: PreWipeConfig
  post_wipe_config: PostWipeConfig
}

export interface PreWipeConfig {
  enabled: boolean
  backup_before_wipe: boolean
  countdown_warnings: number[]
  countdown_unit: string
  countdown_messages: Record<string, string>
  kick_players_before_wipe: boolean
  kick_message: string
  run_final_save: boolean
  discord_pre_announce: boolean
  pushbullet_notify: boolean
  custom_commands_before: string[]
}

export interface PostWipeConfig {
  enabled: boolean
  verify_server_started: boolean
  verify_correct_map: boolean
  verify_plugins_loaded: boolean
  verify_player_slots_open: boolean
  max_restart_attempts: number
  health_check_timeout_seconds: number
  discord_post_announce: boolean
  pushbullet_notify: boolean
  rollback_on_failure: boolean
  post_wipe_commands: string[]
}

export interface WipeSchedule {
  id: string
  license_id: string
  wipe_profile_id: string
  schedule_name: string
  wipe_type: 'map' | 'blueprint' | 'full'
  cron_expression: string
  timezone: string
  wipe_blueprints: boolean
  is_active: boolean
  next_scheduled_run: string | null
}

export interface WipeHistory {
  id: string
  wipe_type: string
  trigger_type: 'scheduled' | 'manual' | 'force_wipe'
  status: 'pending' | 'pre_wipe' | 'wiping' | 'post_wipe' | 'success' | 'failed' | 'rolled_back'
  started_at: string | null
  completed_at: string | null
  map_used: string | null
  plugins_wiped: string[]
  plugins_preserved: string[]
  error_message: string | null
}

export interface MapEntry {
  id: string
  filename: string
  display_name: string
  file_size_bytes: number
  map_type: 'custom' | 'procedural'
  seed: number | null
  world_size: number | null
  thumbnail_path: string | null
  checksum: string
}

export interface PluginEntry {
  id: string
  plugin_name: string
  plugin_version: string | null
  source: 'umod' | 'corrosion_module' | 'manual'
  is_installed: boolean
  is_loaded: boolean
  wipe_on_map: boolean
  wipe_on_bp: boolean
  wipe_on_full: boolean
  never_wipe: boolean
}

export interface GameAdmin {
  id: string
  steam_id: string
  display_name: string
  admin_level: 'owner' | 'admin' | 'moderator'
}

export interface TeamMember {
  id: string
  user_id: string
  username: string
  email: string
  role_name: string
  accepted_at: string | null
}

export interface Role {
  id: string
  role_name: string
  is_system_default: boolean
  permissions: Record<string, boolean>
}

export interface ChatMessage {
  id: string
  steam_id: string
  player_name: string
  channel: 'global' | 'team' | 'server'
  message: string
  flagged: boolean
  created_at: string
}

export interface NotificationConfig {
  discord_webhook_url: string | null
  discord_enabled: boolean
  pushbullet_api_key: string | null
  pushbullet_enabled: boolean
  email_alerts_enabled: boolean
  notify_wipe_start: boolean
  notify_wipe_complete: boolean
  notify_wipe_failed: boolean
  notify_server_crash: boolean
  notify_server_offline: boolean
  notify_store_purchase: boolean
}

export interface WebstoreItem {
  id: string
  category_id: string
  item_name: string
  description: string | null
  price: number
  image_url: string | null
  item_type: 'kit' | 'rank' | 'currency' | 'custom_command'
  delivery_config: { commands: string[] }
  is_active: boolean
}

export interface WebstoreTransaction {
  id: string
  item_id: string
  buyer_steam_id: string
  buyer_name: string | null
  amount: number
  currency: string
  status: 'pending' | 'paid' | 'delivered' | 'failed' | 'refunded'
  delivered_at: string | null
  created_at: string
}

// Module Store types
export interface Module {
  id: string
  slug: string
  name: string
  description: string
  price: number
  category: string
  preview_image_url: string
  screenshots: string[]
  features: string[]
  version: string
  is_purchased: boolean
  is_installed: boolean
}

export interface PurchaseRequest {
  module_id: string
  license_id: string
}

// Analytics types
export interface AnalyticsSummary {
  peak_players: number
  avg_players: number
  uptime_percentage: number
  unique_players: number | null
}

export interface TimeseriesData {
  timestamps: string[]
  player_count: number[]
  fps: number[]
  entity_count: number[]
  memory_usage_mb: number[]
}

export interface HourlyStats {
  id: string
  license_id: string
  hour: string
  avg_players: number
  max_players: number
  avg_fps: number
  min_fps: number
  avg_entities: number
  uptime_percentage: number
}

// Wipe Analytics types
export interface WipeAnalyticsEntry {
  date: string
  duration_seconds: number
  peak_population: number
  hours_to_peak: number
  success: boolean
}

export interface PopulationCurve {
  day_1_avg: number
  day_2_avg: number
  day_3_avg: number
}

export interface WipePerformanceMetrics {
  total_wipes: number
  successful_wipes: number
  failed_wipes: number
  success_rate_percent: number
  avg_duration_seconds: number
  wipes: WipeAnalyticsEntry[]
  population_curve: PopulationCurve
  optimal_wipe_day: string
  optimal_wipe_hour: number
}

// Map Analytics types
export interface MapPerformanceMetrics {
  map_id: string
  map_name: string
  seed: number | null
  times_used: number
  avg_players: number
  peak_players: number
  unique_players: number | null
  effectiveness_score: number
}

export interface MapAnalyticsSummary {
  maps: MapPerformanceMetrics[]
  best_performing_map: string | null
  rotation_effectiveness: number
}

// Player Retention Analytics types — Phase 2.2
export interface WipeRetentionMetric {
  wipe_id: string
  wipe_date: string
  total_players_before_wipe: number
  returned_24h: number
  returned_48h: number
  returned_72h: number
  retention_24h_percent: number
  retention_48h_percent: number
  retention_72h_percent: number
}

export interface SessionSummary {
  unique_players: number
  total_sessions: number
  avg_session_duration_minutes: number
  new_players: number
  returning_players: number
  new_vs_returning_ratio: number
}

export interface RetentionResponse {
  wipe_metrics: WipeRetentionMetric[]
  summary: SessionSummary
}

// Store Configuration types — Phase 5
export interface StoreConfig {
  store_name: string
  description: string | null
  currency: string
  paypal_client_id: string | null
  sandbox_mode: boolean
  enabled: boolean
}

// Public Store types — Phase 5 Customer Frontend
export interface PublicStoreInfo {
  store_name: string
  description: string | null
  currency: string
  enabled: boolean
}

export interface PublicStoreItem {
  id: string
  category_name: string | null
  name: string
  description: string | null
  price: number
  image_url: string | null
  item_type: string
  limit_per_player: number | null
}

export interface StorePurchaseRequest {
  item_id: string
  steam_id: string
  player_name: string
}

export interface StorePurchaseResponse {
  order_id: string
  approval_url: string
}

// Store management types (admin interface)
export interface StoreCategory {
  id: string
  name: string
  slug: string
  description: string | null
  display_order: number
  visible: boolean
}

export interface StoreItem {
  id: string
  category_id: string | null
  name: string
  description: string | null
  price: number
  image_url: string | null
  item_type: 'kit' | 'rank' | 'currency' | 'command'
  delivery_commands: string[]
  limit_per_player: number | null
  enabled: boolean
}

export interface StoreTransaction {
  id: string
  item_id: string | null
  steam_id: string
  player_name: string | null
  paypal_order_id: string
  amount: number
  currency: string
  status: 'pending' | 'paid' | 'delivered' | 'failed' | 'refunded'
  delivered: boolean
  delivered_at: string | null
  payer_email: string | null
  created_at: string
}

// Deployment types
export interface DeploymentConfig {
  server_name: string
  max_players: number
  world_size: number
  seed: number
  server_port: number
  rcon_port: number
  rcon_password: string
}

export interface DeploymentStatus {
  stage: 'downloading_steamcmd' | 'installing_steamcmd' | 'downloading_rust' | 'configuring' | 'starting' | 'online' | 'failed'
  progress: number
  message: string
  error?: string
}

// Loot Builder types — BetterLoot integration
export interface LootProfileSummary {
  id: string
  profile_name: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface LootProfileFull {
  id: string
  license_id: string
  profile_name: string
  description: string | null
  loot_table: Record<string, PrefabLoot>
  loot_groups: Record<string, LootGroupProfile>
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface PrefabLoot {
  Enabled: boolean
  LootProfiles: LootProfileRef[]
  GuaranteedItems: Record<string, LootItemSettings>
  UngroupedItems: Record<string, LootRNG>
  ItemSettings: {
    ItemsMin: number
    ItemsMax: number
    MinScrap: number
    MaxScrap: number
  }
}

export interface LootProfileRef {
  Enabled: boolean
  LootProfileName: string
  LootProfileProbability: number
}

export interface LootItemSettings {
  Min: number
  Max: number
  SkinId: number
  DisplayName: string
}

export interface LootEntry extends LootItemSettings {
  DurabilitySettings: {
    MinDurability: number
    MaxDurability: number
  }
  ItemEntryModifications: {
    AmmoSettings: Record<string, any> | null
    AttachmentSettings: Record<string, any> | null
  }
  BonusItems: Record<string, LootItemSettings>
}

export interface LootRNG extends LootEntry {
  Probability: number
}

export interface LootGroupProfile {
  Enabled: boolean
  GuaranteedItems: Record<string, LootItemSettings>
  ItemList: Record<string, LootRNG>
}

export interface LootApplyResult {
  success: boolean
  message: string
  profile_name: string
  multiplier: number
}

// Teleport Config types — NTeleportation integration
export interface TeleportConfigSummary {
  id: string
  config_name: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface TeleportConfigFull {
  id: string
  license_id: string
  config_name: string
  description: string | null
  config_data: Record<string, any>
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface TeleportApplyResult {
  success: boolean
  message: string
  config_name: string
}

// GatherManager Config types
export interface GatherConfigSummary {
  id: string
  config_name: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface GatherConfigFull {
  id: string
  license_id: string
  config_name: string
  description: string | null
  config_data: Record<string, any>
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface GatherApplyResult {
  success: boolean
  message: string
  config_name: string
}

// AutoDoors Config types
export interface AutoDoorsConfigSummary {
  id: string
  config_name: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface AutoDoorsConfigFull {
  id: string
  license_id: string
  config_name: string
  description: string | null
  config_data: Record<string, any>
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface AutoDoorsApplyResult {
  success: boolean
  message: string
  config_name: string
}

// Kits Config types
export interface KitsConfigSummary {
  id: string
  config_name: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface KitsConfigFull {
  id: string
  license_id: string
  config_name: string
  description: string | null
  config_data: Record<string, any>
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface KitsApplyResult {
  success: boolean
  message: string
  config_name: string
}

// FurnaceSplitter Config types
export interface FurnaceSplitterConfigSummary {
  id: string
  config_name: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface FurnaceSplitterConfigFull {
  id: string
  license_id: string
  config_name: string
  description: string | null
  config_data: Record<string, any>
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface FurnaceSplitterApplyResult {
  success: boolean
  message: string
  config_name: string
}

// BetterChat Config types
export interface BetterChatConfigSummary {
  id: string
  config_name: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface BetterChatConfigFull {
  id: string
  license_id: string
  config_name: string
  description: string | null
  config_data: Record<string, any>
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface BetterChatApplyResult {
  success: boolean
  message: string
  config_name: string
}

// TimedExecute Config types
export interface TimedExecuteConfigSummary {
  id: string
  config_name: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface TimedExecuteConfigFull {
  id: string
  license_id: string
  config_name: string
  description: string | null
  config_data: Record<string, any>
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface TimedExecuteApplyResult {
  success: boolean
  message: string
  config_name: string
}

// RaidableBases Config types
export interface RaidableBasesConfigSummary {
  id: string
  config_name: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface RaidableBasesConfigFull {
  id: string
  license_id: string
  config_name: string
  description: string | null
  config_data: Record<string, any>
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface RaidableBasesApplyResult {
  success: boolean
  message: string
  config_name: string
}
