//! Milestone tracking handlers

use crate::error::{ApiError, ApiResult};
use crate::state::AppState;
use axum::{
    extract::{Path, State},
    http::StatusCode,
    Json,
};
use serde::{Deserialize, Serialize};
use syntaxis_core::persistence::Database;

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

/// Milestone DTO
#[derive(Debug, Serialize)]
pub 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
    pub due_date: String,
    /// Completed status
    pub completed: bool,
}

/// Create a milestone
pub async fn create_milestone(
    State(state): State<AppState>,
    Path(project_id): Path<String>,
    Json(_req): Json<CreateMilestoneRequest>,
) -> ApiResult<(StatusCode, Json<MilestoneDto>)> {
    // Verify project exists
    let _ = state
        .db
        .get_project(&project_id)
        .await?
        .ok_or_else(|| ApiError::NotFound(format!("Project {} not found", project_id)))?;

    // NOTE: MilestoneEntity not yet migrated to Database trait
    Err(ApiError::Internal(
        "Milestones not yet implemented with new Database trait".to_string(),
    ))
}

/// Get milestones for a project
pub async fn get_milestones(
    State(state): State<AppState>,
    Path(project_id): Path<String>,
) -> ApiResult<Json<Vec<MilestoneDto>>> {
    // Verify project exists
    let _ = state
        .db
        .get_project(&project_id)
        .await?
        .ok_or_else(|| ApiError::NotFound(format!("Project {} not found", project_id)))?;

    // NOTE: MilestoneEntity not yet migrated
    Ok(Json(Vec::new()))
}

/// Update a milestone
pub async fn update_milestone(
    State(_state): State<AppState>,
    Path((_project_id, _milestone_id)): Path<(String, String)>,
    Json(_req): Json<CreateMilestoneRequest>,
) -> ApiResult<Json<MilestoneDto>> {
    // NOTE: MilestoneEntity not yet migrated
    Err(ApiError::Internal(
        "Milestones not yet implemented with new Database trait".to_string(),
    ))
}

/// List milestones (alias for get_milestones)
pub async fn list_milestones(
    state: State<AppState>,
    path: Path<String>,
) -> ApiResult<Json<Vec<MilestoneDto>>> {
    get_milestones(state, path).await
}

/// Mark a milestone as complete
pub async fn complete_milestone(
    State(_state): State<AppState>,
    Path((_project_id, _milestone_id)): Path<(String, String)>,
) -> ApiResult<Json<MilestoneDto>> {
    // NOTE: MilestoneEntity not yet migrated
    Err(ApiError::Internal(
        "Milestones not yet implemented with new Database trait".to_string(),
    ))
}
