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

#[utoipa::path(
    post,
    path = "/api/users/{username}/follow",
    params(("username" = String, Path)),
    responses(
        (status = 200, description = "Follow user"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "User"
)]
pub async fn subscribe_target(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let target = path.into_inner();
    service.user_subscribe_target(session, target).await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/users/{username}/follow",
    params(("username" = String, Path)),
    responses(
        (status = 200, description = "Unfollow user"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "User"
)]
pub async fn unsubscribe_target(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let target = path.into_inner();
    service.user_unsubscribe_target(session, target).await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}

#[utoipa::path(
    get,
    path = "/api/users/{username}/follow",
    params(("username" = String, Path)),
    responses(
        (status = 200, description = "Check if following user"),
        (status = 401, description = "Unauthorized"),
),
    tag = "User"
)]
pub async fn is_subscribed_to_target(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let target = path.into_inner();
    let resp = service
        .user_is_subscribed_to_target(session, target)
        .await?;
    Ok(ApiResponse::ok(serde_json::json!({ "is_subscribed": resp })).to_response())
}

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

#[utoipa::path(
    get,
    path = "/api/users/{username}/following/count",
    params(("username" = String, Path)),
    responses(
        (status = 200, description = "Get following count"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "User"
)]
pub async fn get_subscription_count(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let username = path.into_inner();
    let resp = service
        .user_get_subscription_count(session, username)
        .await?;
    Ok(ApiResponse::ok(serde_json::json!({ "count": resp })).to_response())
}

#[utoipa::path(
    get,
    path = "/api/users/{username}/followers/count",
    params(("username" = String, Path)),
    responses(
        (status = 200, description = "Get follower count"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "User"
)]
pub async fn get_subscriber_count(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let username = path.into_inner();
    let resp = service.user_get_subscriber_count(session, username).await?;
    Ok(ApiResponse::ok(serde_json::json!({ "count": resp })).to_response())
}

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