using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;


/// <summary>
/// CRUD for clinical alert thresholds with Redis cache invalidation on writes.
/// </summary>
[ApiController]
[Route("api/v1/alert-thresholds")]
[Produces("application/json")]
[Authorize]
public class AlertThresholdsController : ControllerBase
{
    private readonly IAlertThresholdService _thresholds;

    public AlertThresholdsController(IAlertThresholdService thresholds) => _thresholds = thresholds;

    /// <summary>
    /// Creates a new alert threshold for an observation code.
    /// </summary>
    /// <param name="req">Threshold bounds and display metadata.</param>
    /// <returns>The created threshold.</returns>
    [HttpPost]
    [AuthorizePermission(ClinicalPermissions.ThresholdsWrite)]
    [ProducesResponseType(typeof(ApiResponse<AlertThreshold>), StatusCodes.Status201Created)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status409Conflict)]
    public async Task<IActionResult> Create([FromBody] AlertThresholdRequest req)
    {
        var threshold = await _thresholds.CreateAsync(req);
        return StatusCode(201, ApiResponse<AlertThreshold>.Created(threshold));
    }

    /// <summary>
    /// Lists all alert thresholds ordered by observation code.
    /// </summary>
    /// <returns>All configured thresholds.</returns>
    [HttpGet]
    [AuthorizePermission(ClinicalPermissions.ThresholdsRead)]
    [ProducesResponseType(typeof(ApiResponse<List<AlertThreshold>>), StatusCodes.Status200OK)]
    public async Task<IActionResult> List()
    {
        var thresholds = await _thresholds.ListAsync();
        return Ok(ApiResponse<List<AlertThreshold>>.Ok(thresholds));
    }

    /// <summary>
    /// Gets a single alert threshold by id.
    /// </summary>
    /// <param name="id">Threshold id.</param>
    /// <returns>The threshold record.</returns>
    [HttpGet("{id:guid}")]
    [AuthorizePermission(ClinicalPermissions.ThresholdsRead)]
    [ProducesResponseType(typeof(ApiResponse<AlertThreshold>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<IActionResult> Get(Guid id)
    {
        var threshold = await _thresholds.GetByIdAsync(id);
        return Ok(ApiResponse<AlertThreshold>.Ok(threshold));
    }

    /// <summary>
    /// Updates an existing alert threshold and invalidates the Redis cache entry.
    /// </summary>
    /// <param name="id">Threshold id.</param>
    /// <param name="req">Updated threshold bounds and display metadata.</param>
    /// <returns>The updated threshold.</returns>
    [HttpPut("{id:guid}")]
    [AuthorizePermission(ClinicalPermissions.ThresholdsWrite)]
    [ProducesResponseType(typeof(ApiResponse<AlertThreshold>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<IActionResult> Update(Guid id, [FromBody] AlertThresholdRequest req)
    {
        var threshold = await _thresholds.UpdateAsync(id, req);
        return Ok(ApiResponse<AlertThreshold>.Ok(threshold));
    }

    /// <summary>
    /// Deletes an alert threshold. Clinical entities (patients, encounters, observations,
    /// alerts, scores) are immutable by design and do not support deletion.
    /// </summary>
    /// <param name="id">Threshold id.</param>
    [HttpDelete("{id:guid}")]
    [AuthorizePermission(ClinicalPermissions.ThresholdsWrite)]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<IActionResult> Delete(Guid id)
    {
        await _thresholds.DeleteAsync(id);
        return NoContent();
    }
}
