using AdsManagerService.Domain.AggregatesModel.AdAggregate;
using AdsManagerService.Domain.SeedWork;
using Microsoft.EntityFrameworkCore;

namespace AdsManagerService.Infrastructure.Repositories;

/// <summary>
/// EN: Repository implementation for Ad aggregate.
/// VI: Triển khai repository cho Ad aggregate.
/// </summary>
public class AdRepository : IAdRepository
{
    private readonly AdsManagerServiceContext _context;

    public IUnitOfWork UnitOfWork => _context;

    public AdRepository(AdsManagerServiceContext context)
    {
        _context = context ?? throw new ArgumentNullException(nameof(context));
    }

    public Ad Add(Ad ad)
    {
        return _context.Ads.Add(ad).Entity;
    }

    public void Update(Ad ad)
    {
        _context.Entry(ad).State = EntityState.Modified;
    }

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