using MongoDB.Driver;
using SumaTube.Infra.MongoDB.Documents;

namespace SumaTube.Infra.MongoDB.Repositories
{
    public class PersonUserRepository
    {
        private readonly IMongoCollection<PersonUserDocument> _collection;

        public PersonUserRepository(IMongoDatabase database)
        {
            _collection = database.GetCollection<PersonUserDocument>("PersonUsers");
        }

        public async Task<PersonUserDocument> GetByIdAsync(string id)
        {
            return await _collection.Find(p => p.Id == id).FirstOrDefaultAsync();
        }

        public async Task<IEnumerable<PersonUserDocument>> GetAllAsync()
        {
            return await _collection.Find(_ => true).ToListAsync();
        }

        public async Task CreateAsync(PersonUserDocument PersonUserDocument)
        {
            await _collection.InsertOneAsync(PersonUserDocument);
        }

        public async Task UpdateAsync(PersonUserDocument PersonUserDocument)
        {
            await _collection.ReplaceOneAsync(p => p.Id == PersonUserDocument.Id, PersonUserDocument);
        }

        public async Task DeleteAsync(string id)
        {
            await _collection.DeleteOneAsync(p => p.Id == id);
        }
    }
}