using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SolutionErp.Application.Office;

namespace SolutionErp.Api.Controllers;

[ApiController]
[Route("api/vehicle-bookings")]
[Authorize]
public class VehicleBookingsController(IMediator mediator) : ControllerBase
{
    [HttpGet]
    public async Task<IActionResult> GetList([FromQuery] int? status, [FromQuery] Guid? requesterUserId, [FromQuery] int page = 1, [FromQuery] int pageSize = 50)
        => Ok(await mediator.Send(new GetVehicleBookingsQuery(status, requesterUserId, page, pageSize)));

    [HttpGet("{id:guid}")]
    public async Task<IActionResult> GetById(Guid id)
    {
        var dto = await mediator.Send(new GetVehicleBookingByIdQuery(id));
        return dto is null ? NotFound() : Ok(dto);
    }

    [HttpPost]
    public async Task<IActionResult> Create([FromBody] CreateVehicleBookingCommand cmd)
    {
        var id = await mediator.Send(cmd);
        return CreatedAtAction(nameof(GetById), new { id }, new { id });
    }

    [HttpPut("{id:guid}")]
    public async Task<IActionResult> Update(Guid id, [FromBody] UpdateVehicleBookingBody body)
    {
        await mediator.Send(new UpdateVehicleBookingDraftCommand(id, body.VehicleLicense, body.VehicleName,
            body.StartAt, body.EndAt, body.Destination, body.Purpose, body.DriverName, body.ApprovalWorkflowId));
        return NoContent();
    }

    [HttpPost("{id:guid}/submit")]
    public async Task<IActionResult> Submit(Guid id)
    {
        await mediator.Send(new SubmitVehicleBookingCommand(id));
        return NoContent();
    }

    [HttpPost("{id:guid}/approve")]
    public async Task<IActionResult> Approve(Guid id, [FromBody] ApprovalActionBody body)
    {
        await mediator.Send(new ApproveVehicleBookingCommand(id, body.Comment));
        return NoContent();
    }

    [HttpPost("{id:guid}/reject")]
    public async Task<IActionResult> Reject(Guid id, [FromBody] ApprovalActionBody body)
    {
        await mediator.Send(new RejectVehicleBookingCommand(id, body.Comment));
        return NoContent();
    }

    [HttpPost("{id:guid}/return")]
    public async Task<IActionResult> Return(Guid id, [FromBody] ApprovalActionBody body)
    {
        await mediator.Send(new ReturnVehicleBookingCommand(id, body.Comment));
        return NoContent();
    }

    [HttpPut("{id:guid}/workflow")]
    public async Task<IActionResult> SetWorkflow(Guid id, [FromBody] SetWorkflowBody body)
    {
        await mediator.Send(new SetVehicleBookingWorkflowCommand(id, body.ApprovalWorkflowId));
        return NoContent();
    }

    public record SetWorkflowBody(Guid ApprovalWorkflowId);

    public record UpdateVehicleBookingBody(
        string VehicleLicense,
        string? VehicleName,
        DateTime StartAt,
        DateTime EndAt,
        string Destination,
        string Purpose,
        string? DriverName,
        Guid? ApprovalWorkflowId);

    public record ApprovalActionBody(string? Comment);
}
