using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Identity.Commands.SelectRole;
using Baya.Application.Features.Identity.Queries.GetMe;
using Baya.Application.Models.Identity;
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 user's identity, roles and role selection")]
public sealed class MeController(ISender sender) : BaseController
{
    [HttpGet]
    [ProducesOkApiResponseType<MeResult>]
    public async Task<IActionResult> GetMe(CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new GetMeQuery(), cancellationToken));

    /// <remarks>Role claims live inside the access token — after selecting a role the client should
    /// refresh its tokens to pick the new role up.</remarks>
    [HttpPost("[action]")]
    [ProducesOkApiResponseType<MeResult>]
    public async Task<IActionResult> SelectRole(SelectRoleCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));
}