using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Geography.Commands.CreateCity;
using Baya.Application.Features.Geography.Commands.CreateDistrict;
using Baya.Application.Features.Geography.Commands.CreateProvince;
using Baya.Application.Features.Geography.Commands.SetCityActive;
using Baya.Application.Features.Geography.Commands.SetDistrictActive;
using Baya.Application.Features.Geography.Commands.SetProvinceActive;
using Baya.Application.Features.Geography.Commands.UpdateCity;
using Baya.Application.Features.Geography.Commands.UpdateDistrict;
using Baya.Application.Features.Geography.Commands.UpdateProvince;
using Baya.Application.Models.Geography;
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 geo hierarchy (create/edit + activate/deactivate; no delete)")]
public sealed class AdminGeoController(ISender sender) : BaseController
{
    [HttpPost("[action]")]
    [ProducesOkApiResponseType<ProvinceDto>]
    public async Task<IActionResult> CreateProvince(CreateProvinceCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));

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

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

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

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

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

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

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

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