use axum::{
    extract::{Path, State},
    http::StatusCode,
    routing::get,
    Json, Router,
};

use crate::AppState;
use theseus_core::*;

pub(crate) fn router() -> Router<AppState> {
    Router::new().route("/api/projects/:project_id/timeline", get(get_timeline))
}

async fn get_timeline(
    State(state): State<AppState>,
    Path(project_id): Path<String>,
) -> Result<Json<ApiResponse<Vec<TimelineItem>>>, StatusCode> {
    let pid = crate::resolve_project_id(&state.db, &project_id).await?;
    let timeline = state
        .db
        .project_timeline(pid)
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    Ok(Json(ApiResponse::ok(timeline)))
}
