//! 数据模型

use serde::{Deserialize, Serialize};
use sqlx::FromRow;

/// 数据库用户模型
#[derive(Debug, Clone, FromRow, Serialize)]
pub struct UserRow {
    pub id: String,
    pub username: String,
    #[serde(skip_serializing)]
    pub password_hash: String,
    #[serde(skip_serializing)]
    pub salt: String,
    pub email: Option<String>,
    pub role: String,
    pub created_at: String,
    pub last_login: Option<String>,
}

/// 数据库设备模型
#[derive(Debug, Clone, FromRow, Serialize)]
pub struct DeviceRow {
    pub id: String,
    pub device_id: String,
    pub name: String,
    pub os_type: String,
    pub os_version: String,
    pub user_id: Option<String>,
    pub is_online: bool,
    pub allow_remote: bool,
    pub verification_code: Option<String>,
    pub last_seen: String,
    pub created_at: String,
}

/// 数据库会话模型
#[derive(Debug, Clone, FromRow, Serialize)]
pub struct SessionRow {
    pub id: String,
    pub controller_device_id: String,
    pub controlled_device_id: String,
    pub started_at: String,
    pub ended_at: Option<String>,
    pub connection_type: String,
    pub status: String,
}

/// 数据库控制历史模型
#[derive(Debug, Clone, FromRow, Serialize)]
pub struct HistoryRow {
    pub id: String,
    pub user_id: Option<String>,
    pub controller_device_id: String,
    pub controlled_device_id: String,
    pub controlled_device_name: String,
    pub started_at: String,
    pub ended_at: Option<String>,
    pub duration_seconds: Option<i64>,
    pub connection_type: String,
}

// ==================== API 请求/响应模型 ====================

/// 注册请求
#[derive(Debug, Deserialize)]
pub struct RegisterRequest {
    pub username: String,
    pub password: String,
    pub email: Option<String>,
}

/// 登录请求
#[derive(Debug, Deserialize)]
pub struct LoginRequest {
    pub username: String,
    pub password: String,
    pub device_id: Option<String>,
}

/// 认证响应
#[derive(Debug, Serialize)]
pub struct AuthResponse {
    pub token: String,
    pub user: UserResponse,
}

/// 用户响应
#[derive(Debug, Serialize)]
pub struct UserResponse {
    pub id: String,
    pub username: String,
    pub email: Option<String>,
    pub role: String,
    pub created_at: String,
}

/// 设备注册请求
#[derive(Debug, Deserialize)]
pub struct DeviceRegisterRequest {
    pub device_id: String,
    pub name: String,
    pub os_type: String,
    pub os_version: String,
}

/// 设备响应
#[derive(Debug, Serialize)]
pub struct DeviceResponse {
    pub id: String,
    pub device_id: String,
    pub name: String,
    pub os_type: String,
    pub os_version: String,
    pub is_online: bool,
    pub allow_remote: bool,
    pub last_seen: String,
    pub user_id: Option<String>,
    pub username: Option<String>,
}

/// 会话响应
#[derive(Debug, Serialize)]
pub struct SessionResponse {
    pub id: String,
    pub controller_device_id: String,
    pub controlled_device_id: String,
    pub started_at: String,
    pub ended_at: Option<String>,
    pub connection_type: String,
    pub status: String,
}

/// 历史记录响应
#[derive(Debug, Serialize)]
pub struct HistoryResponse {
    pub id: String,
    pub controller_device_id: String,
    pub controlled_device_id: String,
    pub controlled_device_name: String,
    pub started_at: String,
    pub ended_at: Option<String>,
    pub duration: Option<String>,
    pub connection_type: String,
}

/// 统计响应
#[derive(Debug, Serialize)]
pub struct StatsResponse {
    pub total_users: i64,
    pub total_devices: i64,
    pub online_devices: i64,
    pub active_sessions: i64,
    pub total_sessions: i64,
}

/// 更新用户请求
#[derive(Debug, Deserialize)]
pub struct UpdateUserRequest {
    pub email: Option<String>,
    pub password: Option<String>,
}

/// 通用API响应
#[derive(Debug, Serialize)]
pub struct ApiResponse<T> {
    pub success: bool,
    pub data: Option<T>,
    pub error: Option<String>,
}

impl<T> ApiResponse<T> {
    pub fn ok(data: T) -> Self {
        Self {
            success: true,
            data: Some(data),
            error: None,
        }
    }

    pub fn err(error: impl ToString) -> Self {
        Self {
            success: false,
            data: None,
            error: Some(error.to_string()),
        }
    }
}

/// 分页请求参数
#[derive(Debug, Deserialize)]
pub struct PaginationParams {
    pub page: Option<u32>,
    pub limit: Option<u32>,
}

impl PaginationParams {
    pub fn offset(&self) -> u32 {
        let page = self.page.unwrap_or(1).max(1);
        let limit = self.limit();
        (page - 1) * limit
    }

    pub fn limit(&self) -> u32 {
        self.limit.unwrap_or(20).min(100)
    }
}

/// 分页响应
#[derive(Debug, Serialize)]
pub struct PaginatedResponse<T> {
    pub items: Vec<T>,
    pub total: i64,
    pub page: u32,
    pub limit: u32,
    pub total_pages: u32,
}
