// EN: Repository implementation for leave requests.
// VI: Implementation repository cho yeu cau nghi phep.

using MerchantService.Domain.AggregatesModel.LeaveRequestAggregate;
using MerchantService.Domain.SeedWork;
using Microsoft.EntityFrameworkCore;

namespace MerchantService.Infrastructure.Repositories;

public class LeaveRequestRepository : ILeaveRequestRepository
{
    private readonly MerchantServiceContext _context;
    public IUnitOfWork UnitOfWork => _context;

    public LeaveRequestRepository(MerchantServiceContext context) => _context = context;

    public async Task<LeaveRequest?> GetByIdAsync(Guid id, CancellationToken ct = default)
        => await _context.LeaveRequests.FirstOrDefaultAsync(l => l.Id == id, ct);

    public async Task<List<LeaveRequest>> GetByStaffAsync(Guid staffId, CancellationToken ct = default)
        => await _context.LeaveRequests
            .Where(l => l.StaffId == staffId)
            .OrderByDescending(l => l.CreatedAt)
            .ToListAsync(ct);

    public async Task<List<LeaveRequest>> GetByShopAsync(Guid shopId, CancellationToken ct = default)
        => await _context.LeaveRequests
            .Where(l => l.ShopId == shopId)
            .OrderByDescending(l => l.CreatedAt)
            .ToListAsync(ct);

    public LeaveRequest Add(LeaveRequest request) => _context.LeaveRequests.Add(request).Entity;
    public void Update(LeaveRequest request) => _context.Entry(request).State = EntityState.Modified;
}
