//! i18n API routes — progressive translation loading for WASM clients

use std::collections::HashMap;

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

/// Return all parsed translations for `language` as a flat key→value map.
///
/// Reads from `FTL_REGISTRY`, which is populated at server startup by the
/// implementation's `ResourceContributor::contribute_ftl()`.  Returns 404 when
/// no FTL data is registered for the requested language.
async fn get_translations(
    Path(language): Path<String>,
) -> Result<Json<HashMap<String, String>>, StatusCode> {
    tracing::debug!(
        "i18n API: fetching translations for language '{}'",
        language
    );

    match rustelo_core_lib::registration::get_parsed_ftl_for_language(&language) {
        Some(translations) if !translations.is_empty() => {
            tracing::debug!(
                "i18n API: returning {} keys for language '{}'",
                translations.len(),
                language
            );
            Ok(Json(translations))
        }
        _ => {
            tracing::warn!(
                "i18n API: no translations found for language '{}'",
                language
            );
            Err(StatusCode::NOT_FOUND)
        }
    }
}

/// Create i18n API routes
pub fn create_i18n_routes() -> Router {
    Router::new().route("/i18n/{language}", get(get_translations))
}
