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

#[utoipa::path(
    post,
    path = "/api/agents/code-review/{namespace}/{repo}",
    request_body = TriggerCodeReviewRequest,
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 200, body = TriggerCodeReviewResponse, description = "AI code review triggered"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Repository or PR not found"),
    ),
    tag = "Agent"
)]
pub async fn trigger_code_review(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    body: web::Json<TriggerCodeReviewRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .trigger_ai_code_review(
            namespace,
            repo_name,
            body.pr_number,
            body.model_id,
            &session,
        )
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
