use actix_web::{HttpResponse, web};
use service::AppService;

use crate::error::ApiError;

#[utoipa::path(
    get,
    path = "/api/v1/users/avatar/{username}",
    params(
        ("username" = String, Path, description = "Username"),
    ),
    responses(
        (status = 302, description = "Redirect to avatar image URL"),
        (status = 404, description = "User or avatar not found"),
    ),
    tag = "users"
)]
pub async fn user_avatar(
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let username = path.into_inner();
    let url = service.users_get_avatar_url(&username).await?;
    Ok(HttpResponse::Found()
        .insert_header(("Location", url))
        .finish())
}
