using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Search.Commands.RebuildSearchIndex;
using Baya.Application.Models.Search;
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 maintenance for the search index. The rebuild is the idempotent convergence/reconciliation path —
/// its result must match the incrementally-maintained index.
/// </summary>
[ApiVersion("1")]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize(ConstantPolicies.DynamicPermission)]
[EnableRateLimiting(RateLimitingServiceExtension.SensitivePolicy)]
[Display(Description = "Admin search-index maintenance (full rebuild / reconciliation)")]
public sealed class AdminSearchController(ISender sender) : BaseController
{
    [HttpPost("[action]")]
    [ProducesOkApiResponseType<SearchIndexRebuildResult>]
    public async Task<IActionResult> RebuildIndex(CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new RebuildSearchIndexCommand(), cancellationToken));
}
