namespace DocuMed.Repository.Repositories.UnitOfWork;

public class UnitOfWork : IUnitOfWork
{
    private readonly ApplicationContext _applicationContext;
    private IDbContextTransaction? _currentTransaction ;
    public UnitOfWork(ApplicationContext applicationContext)
    {
        _applicationContext = applicationContext;
    }

    public async Task RollBackAsync()
    {
        if (_currentTransaction == null)
            throw new ArgumentNullException(nameof(_currentTransaction));
        await _currentTransaction.RollbackAsync();
    }
    public async Task CommitAsync()
    {
        if (_currentTransaction == null)
            throw new ArgumentNullException(nameof(_currentTransaction));
        await _currentTransaction.CommitAsync();
    }
    public async Task BeginTransaction()
    {
        _currentTransaction = await _applicationContext.Database.BeginTransactionAsync();
    }
    public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        SetAuditables();
        await _applicationContext.SaveChangesAsync(cancellationToken);
    }

    private void SetAuditables()
    {
        IEnumerable<EntityEntry<IApiEntity>> entries = _applicationContext.ChangeTracker.Entries<IApiEntity>();
        foreach (EntityEntry<IApiEntity> entity in entries)
        {
            if (entity.State == EntityState.Added)
            {
                entity.Property(e=>e.CreatedAt)
                    .CurrentValue = DateTime.Now;
            }

            if (entity.State == EntityState.Modified)
            {
                entity.Property(e => e.ModifiedAt)
                    .CurrentValue = DateTime.Now;
            }

            if (entity.State == EntityState.Deleted)
            {
                entity.Property(e => e.RemovedAt)
                    .CurrentValue = DateTime.Now;
                entity.Property(e => e.IsRemoved)
                    .CurrentValue = true;
            }
        }
    }
}