use axum::extract::State;
use axum::Json;
use serde_json::Value;

use crate::database::verify_connection;
use crate::dto::{ApiResponse, HealthResponse};
use crate::AppState;

pub async fn health_check(State(state): State<AppState>) -> Json<Value> {
    let db_ok = verify_connection(&state.pool).await;
    let response = HealthResponse {
        status: "ok".to_string(),
        service: "crm-api".to_string(),
        database: if db_ok {
            "connected".to_string()
        } else {
            "disconnected".to_string()
        },
    };
    Json(serde_json::to_value(ApiResponse::success(
        response,
        "Service is healthy",
    ))
    .unwrap())
}
