using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Addresses.Commands.CreateAddress;
using Baya.Application.Features.Addresses.Commands.DeleteAddress;
using Baya.Application.Features.Addresses.Commands.SetPrimaryAddress;
using Baya.Application.Features.Addresses.Commands.UpdateAddress;
using Baya.Application.Features.Addresses.Queries.ListMyAddresses;
using Baya.Application.Models.Addresses;
using Baya.Application.Models.Common;
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]
[Display(Description = "The signed-in customer's saved service addresses")]
public sealed class CustomerAddressesController(ISender sender) : BaseController
{
    [HttpPost("[action]")]
    [ProducesOkApiResponseType<CustomerAddressDto>]
    public async Task<IActionResult> Create(CreateAddressCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));

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

    [HttpPost("[action]/{id}")]
    [ProducesOkApiResponseType]
    public async Task<IActionResult> SetPrimary(long id, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new SetPrimaryAddressCommand(id), cancellationToken));

    [HttpDelete("[action]/{id}")]
    [ProducesOkApiResponseType]
    public async Task<IActionResult> Delete(long id, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new DeleteAddressCommand(id), cancellationToken));

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