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 IsLikeResponse {
    pub is_like: bool,
}

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

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

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

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

#[utoipa::path(
    get,
    path = "/api/projects/{project_name}/likes/users",
    params(("project_name" = String, Path)),
    responses(
        (status = 200, description = "List users who liked project", body = ApiResponse<Vec<service::project::like::LikeUserInfo>>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_like_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_like_user_list(project_name, query.into_inner().into())
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
