using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Variants.Commands.CreateVariant;
using Baya.Application.Features.Variants.Commands.SetVariantActive;
using Baya.Application.Features.Variants.Commands.UpdateVariant;
using Baya.Application.Features.Variants.Queries.GetVariant;
using Baya.Application.Features.Variants.Queries.ListMyVariants;
using Baya.Application.Models.Catalog;
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 nurse's priced service variants (the bookable unit)")]
public sealed class NurseVariantsController(ISender sender) : BaseController
{
    [HttpPost("[action]")]
    [ProducesOkApiResponseType<VariantDto>]
    public async Task<IActionResult> Create(CreateVariantCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));

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

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

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

    // Owner/admin get the full view; any other caller (public nurse-profile view) gets active-only.
    [AllowAnonymous]
    [HttpGet("[action]/{id}")]
    [ProducesOkApiResponseType<VariantDto>]
    public async Task<IActionResult> Get(long id, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new GetVariantQuery(id), cancellationToken));
}
