using System.Linq.Expressions;
using MiGu.DB.Abstractions.Entities;

namespace MiGu.DB.Abstractions.Persistence;

public interface IRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>
{
    IQueryable<TEntity> Query(bool asNoTracking = true);
    Task<TEntity?> FindAsync(TKey id, CancellationToken ct = default);
    Task AddAsync(TEntity entity, CancellationToken ct = default);
    void Update(TEntity entity);
}

public interface IEditableRepository<TEntity> : IRepository<TEntity, Guid>
    where TEntity : class, IEntity<Guid>, ISoftDeletable, IVersioned, ILockable
{
    Task<TEntity> GetEditableAsync(Guid id, long? expectedVersion, CancellationToken ct = default);
    Task SoftDeleteAsync(Guid id, long? expectedVersion, CancellationToken ct = default);
    Task EnsureUniqueAsync(Expression<Func<TEntity, bool>> predicate, string errorMessage, CancellationToken ct = default);
}

public interface IHistoryRepository<TEntity> : IRepository<TEntity, Guid>
    where TEntity : class, IEntity<Guid>, IHistoryEntry
{
    Task AppendAsync(TEntity entry, CancellationToken ct = default);
}

public interface IUnitOfWork
{
    Task<int> SaveChangesAsync(CancellationToken ct = default);
    Task ExecuteInTransactionAsync(Func<CancellationToken, Task> action, CancellationToken ct = default);
}
