// EN: Controller for staff attendance operations.
// VI: Controller cho thao tac cham cong nhan vien.

using MediatR;
using Microsoft.AspNetCore.Mvc;
using MerchantService.API.Application.Commands.Attendance;
using MerchantService.API.Application.Queries.Attendance;

namespace MerchantService.API.Controllers;

[ApiController]
[Route("api/v1/attendance")]
public class AttendanceController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<AttendanceController> _logger;

    public AttendanceController(IMediator mediator, ILogger<AttendanceController> logger)
    {
        _mediator = mediator;
        _logger = logger;
    }

    /// <summary>
    /// EN: Get attendance records by staff and month.
    /// VI: Lay ban ghi cham cong theo nhan vien va thang.
    /// </summary>
    [HttpGet("staff/{staffId:guid}")]
    public async Task<IActionResult> GetByStaff(Guid staffId, [FromQuery] int month = 0, [FromQuery] int year = 0, CancellationToken ct = default)
    {
        var now = DateTime.UtcNow;
        if (month <= 0) month = now.Month;
        if (year <= 0) year = now.Year;

        var result = await _mediator.Send(new GetAttendanceByStaffQuery(staffId, month, year), ct);
        return Ok(new { success = true, data = new { items = result } });
    }

    /// <summary>
    /// EN: Get attendance records by shop and month.
    /// VI: Lay ban ghi cham cong theo cua hang va thang.
    /// </summary>
    [HttpGet("shop/{shopId:guid}")]
    public async Task<IActionResult> GetByShop(Guid shopId, [FromQuery] int month = 0, [FromQuery] int year = 0, CancellationToken ct = default)
    {
        var now = DateTime.UtcNow;
        if (month <= 0) month = now.Month;
        if (year <= 0) year = now.Year;

        var result = await _mediator.Send(new GetAttendanceByShopQuery(shopId, month, year), ct);
        return Ok(new { success = true, data = new { items = result } });
    }

    /// <summary>
    /// EN: Check in for a staff member.
    /// VI: Cham cong vao cho nhan vien.
    /// </summary>
    [HttpPost("check-in")]
    public async Task<IActionResult> CheckIn([FromBody] CheckInRequest request, CancellationToken ct = default)
    {
        try
        {
            var result = await _mediator.Send(new CheckInCommand(request.StaffId, request.ShopId), ct);
            return Ok(new { success = true, data = result });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error checking in");
            return BadRequest(new { success = false, message = ex.Message });
        }
    }

    /// <summary>
    /// EN: Check out for a staff member.
    /// VI: Cham cong ra cho nhan vien.
    /// </summary>
    [HttpPost("check-out")]
    public async Task<IActionResult> CheckOut([FromBody] CheckOutRequest request, CancellationToken ct = default)
    {
        try
        {
            var result = await _mediator.Send(new CheckOutCommand(request.StaffId), ct);
            return Ok(new { success = true, data = result });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error checking out");
            return BadRequest(new { success = false, message = ex.Message });
        }
    }
}

public record CheckInRequest(Guid StaffId, Guid ShopId);
public record CheckOutRequest(Guid StaffId);
