//! Data models and DTOs for the API

use serde::{Deserialize, Serialize};

// Type aliases for complex sqlx query result tuples
/// Enhanced task tuple: (id, description, completed, assignee_id, deadline, priority, tags, estimated_hours, actual_hours, created_at, completed_at)
pub(crate) type EnhancedTaskTuple = (
    String,
    String,
    bool,
    Option<String>,
    Option<String>,
    Option<String>,
    Option<String>,
    Option<f64>,
    Option<f64>,
    String,
    Option<String>,
);

/// Attachment tuple: (id, task_id, filename, file_size, mime_type, storage_path, uploaded_by, uploaded_at)
pub(crate) type AttachmentTuple = (String, String, String, i64, String, String, String, String);

/// Simple assignment tuple: (task_id, assignee_id)
#[allow(dead_code)]
pub(crate) type AssignmentTuple = (String, Option<String>);

/// Checklist item tuple: (id, project_id, phase, task_id, description, completed, completed_at, created_at, task_type, task_priority, task_due, task_estimation, task_deps, task_note, task_name)
#[allow(dead_code)]
pub(crate) type ChecklistItemTuple = (
    String,
    String,
    String,
    String,
    String,
    bool,
    Option<String>,
    String,
    String,
    String,
    Option<String>,
    Option<String>,
    String,
    Option<String>,
    Option<String>,
);

/// Subtask tuple: (id, project_id, task_id, description, phase, completed, completed_at, created_at)
pub(crate) type SubtaskTuple = (
    String,
    String,
    String,
    String,
    String,
    bool,
    Option<String>,
    String,
);

/// Project DTO
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ProjectDto {
    /// Project ID
    pub id: String,
    /// Project name
    pub name: String,
    /// Project version
    pub version: String,
    /// Project description
    pub description: String,
    /// Project type
    pub project_type: String,
    /// Current phase
    pub current_phase: String,
    /// Creation timestamp
    pub created_at: String,
    /// Last update timestamp
    pub updated_at: String,
}

/// Create project request
#[derive(Debug, Deserialize)]
pub(crate) struct CreateProjectRequest {
    /// Project name
    pub name: String,
    /// Project description
    pub description: Option<String>,
    /// Project type
    pub project_type: Option<String>,
}

/// Update project request
#[derive(Debug, Deserialize)]
pub(crate) struct UpdateProjectRequest {
    /// Project description
    pub description: Option<String>,
    /// Project version
    pub version: Option<String>,
}

/// Checklist item DTO
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ChecklistItemDto {
    /// Item ID
    pub id: String,
    /// Task ID
    pub task_id: String,
    /// Description
    pub description: String,
    /// Completion status
    pub completed: bool,
    /// Completion timestamp
    pub completed_at: Option<String>,
    /// Creation timestamp
    pub created_at: String,
}

/// Add checklist item request
#[derive(Debug, Deserialize)]
pub(crate) struct AddChecklistItemRequest {
    /// Task ID
    pub task_id: String,
    /// Description
    pub description: String,
}

/// Update checklist item request
#[derive(Debug, Deserialize)]
pub(crate) struct UpdateChecklistItemRequest {
    /// Completion status
    pub completed: Option<bool>,
}

/// Phase transition request
#[derive(Debug, Deserialize)]
pub(crate) struct PhaseTransitionRequest {
    /// Target phase
    pub phase: String,
    /// Optional reason
    pub reason: Option<String>,
}

/// Security assessment DTO
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct SecurityAssessmentDto {
    /// Assessment ID
    pub id: String,
    /// Security profile
    pub profile: String,
    /// Risk level
    pub risk_level: String,
    /// Passed rules count
    pub passed_rules: i32,
    /// Failed rules count
    pub failed_rules: i32,
    /// Critical issues count
    pub critical_issues: i32,
    /// Assessment timestamp
    pub assessment_date: String,
}

/// Run security assessment request
#[derive(Debug, Deserialize)]
pub(crate) struct RunSecurityAssessmentRequest {
    /// Security profile
    pub profile: String,
}

/// Project status DTO
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ProjectStatusDto {
    /// Project ID
    pub id: String,
    /// Current phase
    pub current_phase: String,
    /// Checklist completion percentage
    pub checklist_completion: f64,
    /// Active tools count
    pub active_tools: usize,
    /// Latest security assessment
    pub latest_assessment: Option<SecurityAssessmentDto>,
}

/// Tool configuration DTO
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ToolDto {
    /// Tool name
    pub name: String,
    /// Tool enabled status
    pub enabled: bool,
    /// Tool description
    pub description: String,
}

/// Phase history entry DTO
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct PhaseHistoryDto {
    /// From phase
    pub from_phase: String,
    /// To phase
    pub to_phase: String,
    /// Timestamp
    pub timestamp: String,
    /// Optional reason
    pub reason: Option<String>,
}

// Phase 5 Models

/// Task dependency DTO
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub(crate) struct TaskDependencyDto {
    /// Dependency ID
    pub id: String,
    /// Task ID
    pub task_id: String,
    /// Depends on task ID
    pub depends_on_task_id: String,
    /// Dependency type (finish_to_start, start_to_start, etc.)
    pub dependency_type: String,
    /// Creation timestamp
    pub created_at: String,
}

/// Add dependency request
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub(crate) struct AddDependencyRequest {
    /// Task ID this depends on
    pub depends_on_task_id: String,
    /// Dependency type
    pub dependency_type: String,
}

/// Milestone DTO
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub(crate) struct MilestoneDto {
    /// Milestone ID
    pub id: String,
    /// Project ID
    pub project_id: String,
    /// Milestone name
    pub name: String,
    /// Description
    pub description: Option<String>,
    /// Due date (ISO 8601)
    pub due_date: String,
    /// Completion status
    pub completed: bool,
    /// Completion timestamp
    pub completed_at: Option<String>,
    /// Creation timestamp
    pub created_at: String,
}

/// Create milestone request
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub(crate) struct CreateMilestoneRequest {
    /// Milestone name
    pub name: String,
    /// Description
    pub description: Option<String>,
    /// Due date (ISO 8601)
    pub due_date: String,
}

/// Update milestone request
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub(crate) struct UpdateMilestoneRequest {
    /// Milestone name
    pub name: Option<String>,
    /// Description
    pub description: Option<String>,
    /// Due date
    pub due_date: Option<String>,
}

/// Burndown chart data
#[derive(Debug, Serialize)]
pub(crate) struct BurndownData {
    /// Date labels
    pub dates: Vec<String>,
    /// Ideal burndown line
    pub ideal: Vec<f64>,
    /// Actual burndown line
    pub actual: Vec<f64>,
    /// Total tasks
    pub total_tasks: u32,
    /// Completed tasks
    pub completed_tasks: u32,
}

/// Project statistics
#[derive(Debug, Serialize)]
pub(crate) struct ProjectStats {
    /// Total tasks
    pub total_tasks: usize,
    /// Completed tasks
    pub completed_tasks: usize,
    /// Pending tasks
    pub pending_tasks: usize,
    /// Completion percentage
    pub completion_percent: f64,
    /// Milestone completion percentage
    pub milestone_percent: f64,
    /// Total milestones
    pub total_milestones: usize,
}

/// Assign task request
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub(crate) struct AssignTaskRequest {
    /// Assignee user ID
    pub assignee_id: String,
}

/// User DTO
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub(crate) struct UserDto {
    /// User ID
    pub id: String,
    /// User name
    pub name: String,
    /// User email
    pub email: String,
    /// Creation timestamp
    pub created_at: String,
}

/// Recurrence rule
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub(crate) struct RecurrenceRule {
    /// Frequency (daily, weekly, monthly)
    pub frequency: String,
    /// Interval
    pub interval: u32,
    /// End date
    pub end_date: Option<String>,
}
