using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Bookings.Commands.UpsertCancellationPolicy;
using Baya.Application.Features.Bookings.Queries.ListCancellationPolicies;
using Baya.Application.Models.Booking;
using Baya.Infrastructure.Identity.Identity.PermissionManager;
using Baya.WebFramework.Attributes;
using Baya.WebFramework.BaseController;
using Baya.WebFramework.ServiceConfiguration;
using Mediator;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;

namespace Baya.Web.Api.Controllers.V1;

/// <summary>
/// Admin management of the cancellation/refund tiers. Editing a policy never mutates an already-snapshotted
/// cancellation — past cancellations froze the code + percentage onto the booking.
/// </summary>
[ApiVersion("1")]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize(ConstantPolicies.DynamicPermission)]
[EnableRateLimiting(RateLimitingServiceExtension.SensitivePolicy)]
[Display(Description = "Admin cancellation-policy management (upsert + list)")]
public sealed class AdminCancellationPoliciesController(ISender sender) : BaseController
{
    [HttpPost("[action]")]
    [ProducesOkApiResponseType<CancellationPolicyDto>]
    public async Task<IActionResult> Upsert(UpsertCancellationPolicyCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));

    [HttpGet("[action]")]
    [ProducesOkApiResponseType<IReadOnlyList<CancellationPolicyDto>>]
    public async Task<IActionResult> List(CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new ListCancellationPoliciesQuery(), cancellationToken));
}
