use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService, Pagination,
    git::repo::{RepoFilter, RepoResponse, TransferRepo, UpdateRepo},
};
use session::Session;

use crate::error::ApiError;

fn ok_json<T: Serialize>(data: T) -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().json(data))
}

fn ok() -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().finish())
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct WkRepoPath {
    pub wk: String,
    pub repo: String,
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/repos",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        RepoFilter,
        Pagination,
    ),
    responses(
        (status = 200, body = Vec<RepoResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(("session" = []))
)]
pub async fn list_repos(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    filter: web::Query<RepoFilter>,
    pagination: web::Query<Pagination>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .repo_list(&session, &wk, filter.into_inner(), pagination.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/repos/{repo}",
    params(WkRepoPath),
    responses(
        (status = 200, body = RepoResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Repo not found"),
    ),
    security(("session" = []))
)]
pub async fn get_repo(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    let data = service.repo_get(&session, &wk, &repo).await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/repos/{repo}",
    params(WkRepoPath),
    request_body = UpdateRepo,
    responses(
        (status = 200, body = RepoResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Repo not found"),
    ),
    security(("session" = []))
)]
pub async fn update_repo(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
    params: web::Json<UpdateRepo>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    let data = service
        .repo_update(&session, &wk, &repo, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/repos/{repo}/archive",
    params(WkRepoPath),
    responses(
        (status = 200, body = RepoResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(("session" = []))
)]
pub async fn archive_repo(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    let data = service.repo_archive(&session, &wk, &repo).await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/repos/{repo}",
    params(WkRepoPath),
    responses(
        (status = 200, description = "Repo deleted"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied - owner only"),
    ),
    security(("session" = []))
)]
pub async fn delete_repo(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    service.repo_delete(&session, &wk, &repo).await?;
    ok()
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/repos/{repo}/transfer",
    params(WkRepoPath),
    request_body = TransferRepo,
    responses(
        (status = 200, body = RepoResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied - owner only"),
        (status = 409, description = "Name conflict in target workspace"),
    ),
    security(("session" = []))
)]
pub async fn transfer_repo(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
    params: web::Json<TransferRepo>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    let data = service
        .repo_transfer(&session, &wk, &repo, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/repos/{repo}/topics",
    params(WkRepoPath),
    responses(
        (status = 200, body = Vec<String>),
        (status = 401, description = "Unauthorized"),
    ),
    security(("session" = []))
)]
pub async fn get_topics(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    let data = service.repo_topics(&session, &wk, &repo).await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/repos/{repo}/topics",
    params(WkRepoPath),
    request_body = Vec<String>,
    responses(
        (status = 200, body = Vec<String>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(("session" = []))
)]
pub async fn update_topics(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
    topics: web::Json<Vec<String>>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    let data = service
        .repo_update_topics(&session, &wk, &repo, topics.into_inner())
        .await?;
    ok_json(data)
}
