// EN: Query to get leave requests.
// VI: Query lay yeu cau nghi phep.

using MediatR;
using MerchantService.Domain.AggregatesModel.LeaveRequestAggregate;

namespace MerchantService.API.Application.Queries.LeaveRequests;

public record GetLeaveRequestsByStaffQuery(Guid StaffId) : IRequest<List<LeaveRequestDto>>;
public record GetLeaveRequestsByShopQuery(Guid ShopId) : IRequest<List<LeaveRequestDto>>;

public record LeaveRequestDto(Guid Id, Guid StaffId, string LeaveType, DateTime StartDate, DateTime EndDate,
    string? Reason, string Status, string? ApprovedBy, DateTime CreatedAt);

public class GetLeaveRequestsByStaffHandler : IRequestHandler<GetLeaveRequestsByStaffQuery, List<LeaveRequestDto>>
{
    private readonly ILeaveRequestRepository _repo;

    public GetLeaveRequestsByStaffHandler(ILeaveRequestRepository repo) => _repo = repo;

    public async Task<List<LeaveRequestDto>> Handle(GetLeaveRequestsByStaffQuery request, CancellationToken ct)
    {
        var items = await _repo.GetByStaffAsync(request.StaffId, ct);
        return items.Select(l => new LeaveRequestDto(l.Id, l.StaffId, l.LeaveType, l.StartDate, l.EndDate,
            l.Reason, l.Status, l.ApprovedBy?.ToString(), l.CreatedAt)).ToList();
    }
}

public class GetLeaveRequestsByShopHandler : IRequestHandler<GetLeaveRequestsByShopQuery, List<LeaveRequestDto>>
{
    private readonly ILeaveRequestRepository _repo;

    public GetLeaveRequestsByShopHandler(ILeaveRequestRepository repo) => _repo = repo;

    public async Task<List<LeaveRequestDto>> Handle(GetLeaveRequestsByShopQuery request, CancellationToken ct)
    {
        var items = await _repo.GetByShopAsync(request.ShopId, ct);
        return items.Select(l => new LeaveRequestDto(l.Id, l.StaffId, l.LeaveType, l.StartDate, l.EndDate,
            l.Reason, l.Status, l.ApprovedBy?.ToString(), l.CreatedAt)).ToList();
    }
}
