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

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