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

#[derive(serde::Serialize, utoipa::ToSchema)]
pub struct IsWatchResponse {
    pub is_watching: bool,
}

#[utoipa::path(
    post,
    path = "/api/projects/{project_name}/watch",
    params(("project_name" = String, Path)),
    responses(
        (status = 200, description = "Watch project"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_watch(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let project_name = path.into_inner();
    service.project_watch(&session, project_name).await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/projects/{project_name}/watch",
    params(("project_name" = String, Path)),
    responses(
        (status = 200, description = "Unwatch project"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_unwatch(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let project_name = path.into_inner();
    service.project_unwatch(&session, project_name).await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}

#[utoipa::path(
    get,
    path = "/api/projects/{project_name}/watch",
    params(("project_name" = String, Path)),
    responses(
        (status = 200, description = "Check if user watches project", body = ApiResponse<IsWatchResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_is_watch(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let project_name = path.into_inner();
    let resp = service.project_is_watch(&session, project_name).await?;
    Ok(ApiResponse::ok(IsWatchResponse { is_watching: resp }).to_response())
}

#[utoipa::path(
    get,
    path = "/api/projects/{project_name}/watches/count",
    params(("project_name" = String, Path)),
    responses(
        (status = 200, description = "Get watch count"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_watches_count(
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let project_name = path.into_inner();
    let resp = service.project_watches(project_name).await?;
    Ok(ApiResponse::ok(serde_json::json!({ "count": resp })).to_response())
}

#[utoipa::path(
    get,
    path = "/api/projects/{project_name}/watches/users",
    params(("project_name" = String, Path)),
    responses(
        (status = 200, description = "List users watching project", body = ApiResponse<Vec<service::project::watch::WatchUserInfo>>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_watch_users(
    service: web::Data<AppService>,
    path: web::Path<String>,
    query: web::Query<UserPagerQuery>,
) -> Result<HttpResponse, ApiError> {
    let project_name = path.into_inner();
    let resp = service
        .project_watch_user_list(project_name, query.into_inner().into())
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
