using Managing.Application.Abstractions.Repositories;
using Managing.Domain.Accounts;
using Managing.Infrastructure.Databases.MongoDb;
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
using Managing.Infrastructure.Databases.MongoDb.Collections;

namespace Managing.Infrastructure.Databases;

public class AccountRepository : IAccountRepository
{
    private readonly IMongoRepository<AccountDto> _accountRepository;

    public AccountRepository(IMongoRepository<AccountDto> accountRepository)
    {
        _accountRepository = accountRepository;
    }

    public void DeleteAccountByName(string name)
    {
        var account = _accountRepository.FindOne(a => a.Name == name);
        _accountRepository.DeleteById(account.Id.ToString());
    }

    public async Task<Account> GetAccountByKeyAsync(string key)
    {
        var account = await _accountRepository.FindOneAsync(a => a.Key == key);
        return MongoMappers.Map(account);
    }

    public async Task<Account> GetAccountByNameAsync(string name)
    {
        var account = await _accountRepository.FindOneAsync(a => a.Name == name);
        return MongoMappers.Map(account);
    }

    public IEnumerable<Account> GetAccounts()
    {
        var accounts = _accountRepository.FindAll();
        return MongoMappers.Map(accounts);
    }

    public async Task InsertAccountAsync(Account account)
    {
        await _accountRepository.InsertOneAsync(MongoMappers.Map(account));
    }
}
