use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Server connection details
#[derive(Debug, Clone, sqlx::FromRow, Serialize)]
pub struct ServerConnection {
    pub id: Uuid,
    pub license_id: Uuid,
    pub connection_type: String,
    pub panel_api_endpoint: Option<String>,
    #[serde(skip_serializing)]
    pub panel_api_key_encrypted: Option<String>,
    pub panel_server_identifier: Option<String>,
    #[serde(skip_serializing)]
    pub companion_agent_token: Option<String>,
    pub companion_last_seen: Option<DateTime<Utc>>,
    pub plugin_last_seen: Option<DateTime<Utc>>,
    pub server_ip: Option<String>,
    pub server_port: Option<i32>,
    pub game_port: Option<i32>,
    pub connection_status: String,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

/// Server configuration
#[derive(Debug, Clone, sqlx::FromRow, Serialize)]
pub struct ServerConfig {
    pub id: Uuid,
    pub license_id: Uuid,
    pub server_name: String,
    pub max_players: Option<i32>,
    pub world_size: Option<i32>,
    pub current_seed: Option<i32>,
    pub current_map_id: Option<Uuid>,
    pub server_description: Option<String>,
    pub server_url: Option<String>,
    pub server_header_image: Option<String>,
    pub tags: Option<Vec<String>>,
    pub auto_restart_enabled: bool,
    pub auto_restart_cron: Option<String>,
    pub auto_restart_timezone: Option<String>,
    pub crash_recovery_enabled: bool,
    pub crash_recovery_max_attempts: i32,
    pub crash_recovery_cooldown_minutes: i32,
    pub force_wipe_eligible: bool,
    pub auto_update_on_force_wipe: bool,
    pub config_overrides: Option<serde_json::Value>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

/// Real-time server stats from plugin
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerStats {
    pub license_id: Uuid,
    pub player_count: i32,
    pub max_players: i32,
    pub fps: f64,
    pub entity_count: i32,
    pub uptime_seconds: i32,
    pub memory_usage_mb: i32,
    pub recorded_at: DateTime<Utc>,
}

/// Plugin registry entry
#[derive(Debug, Clone, sqlx::FromRow, Serialize)]
pub struct PluginEntry {
    pub id: Uuid,
    pub license_id: Uuid,
    pub plugin_name: String,
    pub plugin_version: Option<String>,
    pub source: String,
    pub umod_slug: Option<String>,
    pub is_installed: bool,
    pub is_loaded: bool,
    pub config_json: Option<serde_json::Value>,
    pub data_path: Option<String>,
    pub wipe_on_map: bool,
    pub wipe_on_bp: bool,
    pub wipe_on_full: bool,
    pub never_wipe: bool,
    pub installed_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

/// Game admin (in-game SteamID-based admin)
#[derive(Debug, Clone, sqlx::FromRow, Serialize)]
pub struct GameAdmin {
    pub id: Uuid,
    pub license_id: Uuid,
    pub steam_id: String,
    pub display_name: String,
    pub admin_level: String,
    pub permissions: Option<serde_json::Value>,
    pub added_by: Uuid,
    pub created_at: DateTime<Utc>,
}
