using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Identity.Commands.ArchivePatient;
using Baya.Application.Features.Identity.Commands.CreatePatient;
using Baya.Application.Features.Identity.Commands.UpdatePatient;
using Baya.Application.Features.Identity.Queries.GetPatient;
using Baya.Application.Features.Identity.Queries.ListPatients;
using Baya.Application.Models.Common;
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 customer's patients (care recipients)")]
public sealed class PatientsController(ISender sender) : BaseController
{
    [HttpPost("[action]")]
    [ProducesOkApiResponseType<PatientDto>]
    public async Task<IActionResult> Create(CreatePatientCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));

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

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

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

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