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

namespace Managing.Infrastructure.Databases;

public class BotRepository : IBotRepository
{
    private readonly IMongoRepository<BotDto> _botRepository;

    public BotRepository(IMongoRepository<BotDto> botRepository)
    {
        _botRepository = botRepository;
    }

    public async Task InsertBotAsync(BotBackup bot)
    {
        await _botRepository.InsertOneAsync(MongoMappers.Map(bot));
    }

    public IEnumerable<BotBackup> GetBots()
    {
        var bots = _botRepository.FindAll();
        return bots.Select(b => MongoMappers.Map(b));
    }

    public async Task UpdateBackupBot(BotBackup bot)
    {
        var b = await _botRepository.FindOneAsync(b => b.Identifier == bot.Identifier);
        var dto = MongoMappers.Map(bot);
        dto.Id = b.Id;
        _botRepository.Update(dto);
    }

    public async Task DeleteBotBackup(string identifier)
    {
        var backup = await _botRepository.FindOneAsync(b => b.Identifier == identifier);
        await _botRepository.DeleteOneAsync(b => b.Id == backup.Id);
    }
}