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

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

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