// EN: Command to create a leave request.
// VI: Command tao yeu cau nghi phep.

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

namespace MerchantService.API.Application.Commands.LeaveRequests;

public record CreateLeaveRequestCommand(
    Guid StaffId, Guid ShopId, string LeaveType,
    DateTime StartDate, DateTime EndDate, string? Reason) : IRequest<Guid>;

public class CreateLeaveRequestCommandHandler : IRequestHandler<CreateLeaveRequestCommand, Guid>
{
    private readonly ILeaveRequestRepository _repo;
    private readonly ILogger<CreateLeaveRequestCommandHandler> _logger;

    public CreateLeaveRequestCommandHandler(ILeaveRequestRepository repo, ILogger<CreateLeaveRequestCommandHandler> logger)
    {
        _repo = repo;
        _logger = logger;
    }

    public async Task<Guid> Handle(CreateLeaveRequestCommand request, CancellationToken ct)
    {
        var leaveRequest = LeaveRequest.Create(
            request.StaffId, request.ShopId, request.LeaveType,
            request.StartDate, request.EndDate, request.Reason);

        _repo.Add(leaveRequest);
        await _repo.UnitOfWork.SaveEntitiesAsync(ct);

        _logger.LogInformation("EN: Leave request created / VI: Yeu cau nghi phep da tao: StaffId={StaffId}, Type={Type}",
            request.StaffId, request.LeaveType);
        return leaveRequest.Id;
    }
}
