using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nexus.Api.Models;
using Nexus.Api.Services;

namespace Nexus.Api.Controllers;

[Authorize(Roles = "owner")]
[ProducesResponseType(
    typeof(OpenClawAgentConfigurationErrorDto),
    StatusCodes.Status403Forbidden)]
[ProducesResponseType(
    typeof(OpenClawAgentConfigurationErrorDto),
    StatusCodes.Status409Conflict)]
[ProducesResponseType(
    typeof(OpenClawAgentConfigurationErrorDto),
    StatusCodes.Status502BadGateway)]
[ProducesResponseType(
    typeof(OpenClawAgentConfigurationErrorDto),
    StatusCodes.Status503ServiceUnavailable)]
[ProducesResponseType(
    typeof(OpenClawAgentConfigurationErrorDto),
    StatusCodes.Status504GatewayTimeout)]
[ApiController]
[Route("api/v1/incidents")]
public class IncidentsController(IIncidentService incidentService) : ControllerBase
{
    [HttpGet]
    [ProducesResponseType(typeof(IReadOnlyList<IncidentSummary>), StatusCodes.Status200OK)]
    public Task<IResult> GetAll(
        [FromQuery] string agentId = "iris",
        CancellationToken cancellationToken = default)
        => OpenClawContentReadEndpoint.ExecuteAsync(async () =>
            Results.Ok(await incidentService.GetAllAsync(
                agentId,
                cancellationToken)));

    [HttpGet("{name}")]
    [ProducesResponseType(typeof(IncidentDetail), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
    public Task<IResult> GetOne(
        string name,
        [FromQuery] string agentId = "iris",
        CancellationToken cancellationToken = default)
        => OpenClawContentReadEndpoint.ExecuteAsync(async () =>
        {
            var incident = await incidentService.GetByNameAsync(
                name,
                agentId,
                cancellationToken);
            return incident is null
                ? Results.NotFound()
                : Results.Ok(incident);
        });
}
