use axum::{extract::Extension, Json};
use chrono::{DateTime, Utc};
use serde::Serialize;

use crate::app_state::AppState;

#[derive(Serialize)]
pub struct HealthResponse {
    pub status: &'static str,
    pub service_name: String,
    pub environment: String,
    pub version: String,
    pub uptime_ms: u128,
    pub server_time: DateTime<Utc>,
    pub database_ready: bool,
    pub redis_ready: bool,
}

pub async fn handler(Extension(state): Extension<AppState>) -> Json<HealthResponse> {
    Json(HealthResponse {
        status: "ok",
        service_name: state.config.service_name.clone(),
        environment: state.config.environment.clone(),
        version: state.config.app_version.clone(),
        uptime_ms: state.uptime_ms(),
        server_time: Utc::now(),
        database_ready: state.database_ready(),
        redis_ready: state.redis_ready(),
    })
}
