using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Catalog.Commands.CreateServiceCategory;
using Baya.Application.Features.Catalog.Commands.CreateServiceOptionGroup;
using Baya.Application.Features.Catalog.Commands.CreateServiceOptionValue;
using Baya.Application.Features.Catalog.Commands.SetServiceCategoryActive;
using Baya.Application.Features.Catalog.Commands.UpdateServiceCategory;
using Baya.Application.Features.Catalog.Commands.UpdateServiceOptionGroup;
using Baya.Application.Features.Catalog.Commands.UpdateServiceOptionValue;
using Baya.Application.Models.Catalog;
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: curate the catalog skeleton (categories + option groups/values; no delete)")]
public sealed class AdminCatalogController(ISender sender) : BaseController
{
    [HttpPost("[action]")]
    [ProducesOkApiResponseType<ServiceCategoryDto>]
    public async Task<IActionResult> CreateCategory(CreateServiceCategoryCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));

    [HttpPost("[action]/{id}")]
    [ProducesOkApiResponseType<ServiceCategoryDto>]
    public async Task<IActionResult> UpdateCategory(long id, UpdateServiceCategoryCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command with { Id = id }, cancellationToken));

    [HttpPost("[action]/{id}")]
    [ProducesOkApiResponseType]
    public async Task<IActionResult> SetCategoryActive(long id, SetServiceCategoryActiveCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command with { Id = id }, cancellationToken));

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

    [HttpPost("[action]/{id}")]
    [ProducesOkApiResponseType<OptionGroupDto>]
    public async Task<IActionResult> UpdateOptionGroup(long id, UpdateServiceOptionGroupCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command with { Id = id }, cancellationToken));

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

    [HttpPost("[action]/{id}")]
    [ProducesOkApiResponseType<OptionValueDto>]
    public async Task<IActionResult> UpdateOptionValue(long id, UpdateServiceOptionValueCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command with { Id = id }, cancellationToken));
}
