using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Booking.Commands.AcceptBookingRequest;
using Baya.Application.Features.Booking.Commands.CancelBookingRequest;
using Baya.Application.Features.Booking.Commands.CreateBookingRequest;
using Baya.Application.Features.Booking.Commands.RejectBookingRequest;
using Baya.Application.Features.Booking.Queries.GetBookingRequest;
using Baya.Application.Features.Booking.Queries.GetCheckoutSummary;
using Baya.Application.Features.Booking.Queries.ListBookingRequests;
using Baya.Application.Models.Booking;
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;

/// <summary>
/// The pre-payment booking-request lifecycle: a customer creates/cancels a request; the assigned nurse
/// accepts/rejects; both parties read their role-scoped inbox and a single request. No money and no
/// <c>bookings</c> row exist here — accept only opens the 30-minute payment window (converted in b9).
/// </summary>
[ApiVersion("1")]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize]
[Display(Description = "Pre-payment booking requests (create/accept/reject/cancel + role-scoped inbox)")]
public sealed class BookingRequestsController(ISender sender) : BaseController
{
    [HttpPost("[action]")]
    [ProducesOkApiResponseType<BookingRequestDto>]
    public async Task<IActionResult> Create(CreateBookingRequestCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));

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

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

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

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

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

    // The C6 money breakdown for an accepted-awaiting-payment request (owner-scoped, server-computed).
    [HttpGet("[action]/{id}")]
    [ProducesOkApiResponseType<CheckoutSummaryDto>]
    public async Task<IActionResult> CheckoutSummary(long id, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new GetCheckoutSummaryQuery(id), cancellationToken));
}
