using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Configuration.Commands.UpdatePlatformConfig;
using Baya.Application.Features.Configuration.Queries.GetConfigChangeHistory;
using Baya.Application.Features.Configuration.Queries.ListPlatformConfigs;
using Baya.Application.Models.Common;
using Baya.Application.Models.Configuration;
using Baya.Infrastructure.Identity.Identity.PermissionManager;
using Baya.WebFramework.Attributes;
using Baya.WebFramework.BaseController;
using Mediator;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Baya.Web.Api.Controllers.V1;

[ApiVersion("1")]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize(ConstantPolicies.DynamicPermission)]
[Display(Description = "Admin: typed runtime platform configuration and its audited change history")]
public sealed class PlatformConfigController(ISender sender) : BaseController
{
    [HttpGet("[action]")]
    [ProducesOkApiResponseType<PagedResult<PlatformConfigDto>>]
    public async Task<IActionResult> GetPlatformConfigs([FromQuery] ListPlatformConfigsQuery query, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(query, cancellationToken));

    [HttpPost("[action]")]
    [ProducesOkApiResponseType]
    public async Task<IActionResult> UpdatePlatformConfig(UpdatePlatformConfigCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));

    [HttpGet("[action]")]
    [ProducesOkApiResponseType<PagedResult<ConfigChangeDto>>]
    public async Task<IActionResult> GetConfigChangeHistory([FromQuery] GetConfigChangeHistoryQuery query, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(query, cancellationToken));
}
