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

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

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