// EN: Repository implementation for Appointment aggregate.
// VI: Implementation repository cho aggregate Appointment.

using BookingService.Domain.AggregatesModel.AppointmentAggregate;
using BookingService.Domain.SeedWork;
using Microsoft.EntityFrameworkCore;

namespace BookingService.Infrastructure.Repositories;

/// <summary>
/// EN: Repository implementation for managing Appointment aggregate persistence.
/// VI: Implementation repository để quản lý persistence của aggregate Appointment.
/// </summary>
public class AppointmentRepository : IAppointmentRepository
{
    private readonly BookingContext _context;

    public IUnitOfWork UnitOfWork => _context;

    public AppointmentRepository(BookingContext context)
    {
        _context = context ?? throw new ArgumentNullException(nameof(context));
    }

    public Appointment Add(Appointment appointment)
    {
        return _context.Appointments.Add(appointment).Entity;
    }

    public void Update(Appointment appointment)
    {
        _context.Entry(appointment).State = EntityState.Modified;
    }

    public async Task<Appointment?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
    {
        return await _context.Appointments
            .FirstOrDefaultAsync(a => a.Id == id, cancellationToken);
    }

    public async Task<List<Appointment>> GetByShopIdAsync(Guid shopId, CancellationToken cancellationToken = default)
    {
        return await _context.Appointments
            .Where(a => a.ShopId == shopId)
            .ToListAsync(cancellationToken);
    }
}
