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

#[utoipa::path(
    get,
    path = "/api/agents/versions/{model_version_id}/capabilities",
    params(("model_version_id" = i64, Path)),
    responses(
        (status = 200, body = Vec<service::agent::model_capability::ModelCapabilityResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    tag = "Agent"
)]
pub async fn model_capability_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<i64>,
) -> Result<HttpResponse, ApiError> {
    let model_version_id = path.into_inner();
    let resp = service
        .agent_model_capability_list(model_version_id, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/agents/capabilities/{id}",
    params(("id" = i64, Path)),
    responses(
        (status = 200, body = service::agent::model_capability::ModelCapabilityResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404),
    ),
    tag = "Agent"
)]
pub async fn model_capability_get(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<i64>,
) -> Result<HttpResponse, ApiError> {
    let id = path.into_inner();
    let resp = service.agent_model_capability_get(id, &session).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/agents/capabilities",
    request_body = CreateModelCapabilityRequest,
    responses(
        (status = 200, body = service::agent::model_capability::ModelCapabilityResponse),
        (status = 401),
        (status = 403),
    ),
    tag = "Agent"
)]
pub async fn model_capability_create(
    service: web::Data<AppService>,
    session: Session,
    body: web::Json<CreateModelCapabilityRequest>,
) -> Result<HttpResponse, ApiError> {
    let resp = service
        .agent_model_capability_create(body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/agents/capabilities/{id}",
    params(("id" = i64, Path)),
    request_body = UpdateModelCapabilityRequest,
    responses(
        (status = 200, body = service::agent::model_capability::ModelCapabilityResponse),
        (status = 401),
        (status = 403),
        (status = 404),
    ),
    tag = "Agent"
)]
pub async fn model_capability_update(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<i64>,
    body: web::Json<UpdateModelCapabilityRequest>,
) -> Result<HttpResponse, ApiError> {
    let id = path.into_inner();
    let resp = service
        .agent_model_capability_update(id, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/agents/capabilities/{id}",
    params(("id" = i64, Path)),
    responses(
        (status = 200),
        (status = 401),
        (status = 403),
        (status = 404),
    ),
    tag = "Agent"
)]
pub async fn model_capability_delete(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<i64>,
) -> Result<HttpResponse, ApiError> {
    let id = path.into_inner();
    service.agent_model_capability_delete(id, &session).await?;
    Ok(HttpResponse::Ok().json(serde_json::json!({ "success": true })))
}
