use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use session::Session;

#[utoipa::path(
    get,
    path = "/api/users/me/profile",
    responses(
        (status = 200, description = "Get current user profile", body = ApiResponse<service::user::profile::ProfileResponse>),
        (status = 401, description = "Unauthorized"),
),
    tag = "User"
)]
pub async fn get_my_profile(
    service: web::Data<AppService>,
    session: Session,
) -> Result<HttpResponse, ApiError> {
    let resp = service.user_get_current_profile(&session).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/users/{username}",
    params(("username" = String, Path)),
    responses(
        (status = 200, description = "Get user profile", body = ApiResponse<service::user::profile::ProfileResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "User"
)]
pub async fn get_profile_by_username(
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let username = path.into_inner();
    let resp = service.user_get_profile_by_username(username).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/users/me/profile",
    request_body = service::user::profile::UpdateProfileParams,
    responses(
        (status = 200, description = "Update current user profile", body = ApiResponse<service::user::profile::ProfileResponse>),
        (status = 401, description = "Unauthorized"),
),
    tag = "User"
)]
pub async fn update_my_profile(
    service: web::Data<AppService>,
    session: Session,
    body: web::Json<service::user::profile::UpdateProfileParams>,
) -> Result<HttpResponse, ApiError> {
    let resp = service
        .user_update_profile(&session, body.into_inner())
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
