use std::collections::HashMap;
use axum::{
    extract::State,
    response::Json,
    routing::get,
    Router,
};
use common_contracts::observability::{HealthStatus, ServiceStatus, TaskProgress};
use crate::state::AppState;

pub fn create_router(app_state: AppState) -> Router {
    Router::new()
        .route("/health", get(health_check))
        .route("/tasks", get(get_current_tasks))
        .with_state(app_state)
}

/// [GET /health]
/// Provides the current health status of the module.
async fn health_check(State(_state): State<AppState>) -> Json<HealthStatus> {
    let mut details = HashMap::new();
    // In a real scenario, we would check connections to the message bus, etc.
    details.insert("message_bus_connection".to_string(), "ok".to_string());
    
    let status = HealthStatus {
        module_id: "yfinance-provider-service".to_string(),
        status: ServiceStatus::Ok,
        version: env!("CARGO_PKG_VERSION").to_string(),
        details,
    };
    Json(status)
}

/// [GET /tasks]
/// Reports all currently processing tasks and their progress.
async fn get_current_tasks(State(state): State<AppState>) -> Json<Vec<TaskProgress>> {
    let tasks: Vec<TaskProgress> = state
        .tasks
        .iter()
        .map(|entry| entry.value().clone())
        .collect();
    Json(tasks)
}
