use crate::{
    error::ErrorResponse,
    infra::middleware::Language,
    utils::i18n::{message, ZH_CN},
};
use async_trait::async_trait;
use axum::extract::FromRequestParts;

#[derive(Debug, Clone)]
pub struct UserId(pub String);

#[async_trait]
impl<S: Send + Sync> FromRequestParts<S> for UserId {
    type Rejection = ErrorResponse;
    async fn from_request_parts(
        parts: &mut axum::http::request::Parts,
        _: &S,
    ) -> Result<Self, Self::Rejection> {
        let lang = parts
            .extensions
            .get::<Language>()
            .map(|v| v.0.as_str())
            .unwrap_or(ZH_CN);
        parts.extensions.get::<UserId>().cloned().ok_or_else(|| {
            ErrorResponse::unauthorized(message(lang, "未找到用户身份", "User identity not found"))
        })
    }
}
