using Domain.Aggregates.Users;
using Domain.Aggregates.Users.Data;
using Microsoft.EntityFrameworkCore;

namespace Persistence.Repositories.Aggregates.Users;

public class UserRepository
    (DatabaseContext databaseContext) : IUserRepository
{
    public async Task AddAsync(User user)
    {
        await databaseContext.AddAsync(user);
    }

    public async Task<List<User>> GetAllAsync()
    {
        var users =
            await databaseContext.Users
            .Where(x => x.IsDeleted == false)
            .ToListAsync();

        return users;
    }

    public async Task<User?> GetAsync(Guid userId)
    {
        var user =
            await databaseContext.Users
            .Where(x => x.IsDeleted == false)
            .Where(x => x.Id == userId)
            .FirstOrDefaultAsync();

        return user;
    }

    public void Remove(User user)
    {
        user.Delete();
    }

    public async Task SaveChangesAsync()
    {
        await databaseContext.SaveChangesAsync();
    }
}
