using Modules.Library.Application.Gateways;
using Modules.Library.Application.Models.Anime;
using Modules.Library.Database.GatewaysImplementations.Converters;
using Modules.Library.Database.Repositories;
using Modules.Library.Domain.Entities.MediaContent.Items.Anime;

namespace Modules.Library.Database.GatewaysImplementations;

public class AnimeTitleGateway(AnimeTitleRepository titleRepository, LanguageRepository languageRepository, GenreRepository genreRepository) : IAnimeTitleGateway
{
    private readonly AnimeTitleConverter _titleConverter = new(languageRepository, genreRepository);
    private readonly AnimeSeasonConverter _seasonConverter = new(languageRepository, genreRepository);

    public async Task<List<Title>> GetList()
    {
        var list = await titleRepository.GetAllAsync();
        var result = new List<Title>();
        foreach (var title in list)
        {
            result.Add(await _titleConverter.Convert(title));
        }

        return result;
    }

    public async Task<Title> GetDetail(Guid id)
    {
        var dbTitle = await titleRepository.GetFirstWhere(q => q.Id == id && !q.Deleted);
        dbTitle.Items = dbTitle.Items.Where(q => !q.Deleted).ToList();
        return await _titleConverter.Convert(dbTitle);

    }

    public Task<Guid> Create(AnimeTitle title)
    {
        return titleRepository.AddAsync(_titleConverter.Convert(title));
    }

    public async Task<AnimeTitle> GetById(Guid id)
    {
        var dbTitle = await titleRepository.GetFirstWhere(q => q.Id == id && !q.Deleted);
        return await _titleConverter.ConvertToDomain(dbTitle);
    }

    public Task Update(AnimeTitle title) => titleRepository.UpdateAsync(_titleConverter.Convert(title));


    /*
    public async Task AddAsync(Domain.Entities.MediaContent.Items.Anime.AnimeTitle entity, IUser user) =>
        await AddAsync(ToDbConverter.Title(entity));

    public Task<bool> AnyWhere(Expression<Func<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, bool>> predicate)
    {
        var p = predicate.
    }

    public Task<bool> DeleteAsync(Domain.Entities.MediaContent.Items.Anime.AnimeTitle entity, IUser user)
    {
        throw new NotImplementedException();
    }

    public Task<Domain.Entities.MediaContent.Items.Anime.AnimeTitle> GetByIdAsync(string id)
    {
        throw new NotImplementedException();
    }

    public Task<Domain.Entities.MediaContent.Items.Anime.AnimeTitle?> GetByIdOrDefaultAsync(string id)
    {
        throw new NotImplementedException();
    }

    public Task<Domain.Entities.MediaContent.Items.Anime.AnimeTitle?> GetFirstOrDefaultWhere(Expression<Func<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, bool>> predicate)
    {
        throw new NotImplementedException();
    }

    public Task<Domain.Entities.MediaContent.Items.Anime.AnimeTitle> GetFirstWhere(Expression<Func<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, bool>> predicate)
    {
        throw new NotImplementedException();
    }

    public Task<List<Domain.Entities.MediaContent.Items.Anime.AnimeTitle>> GetRangeByIdsAsync(List<string> ids)
    {
        throw new NotImplementedException();
    }

    public Task<List<Domain.Entities.MediaContent.Items.Anime.AnimeTitle>> GetWhere(Expression<Func<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, bool>> predicate)
    {
        throw new NotImplementedException();
    }

    public Task<bool> UpdateAsync(Domain.Entities.MediaContent.Items.Anime.AnimeTitle entity, IUser user)
    {
        throw new NotImplementedException();
    }

    Task<List<Domain.Entities.MediaContent.Items.Anime.AnimeTitle>> Application.Gateways.IRepository<Domain.Entities.MediaContent.Items.Anime.AnimeTitle, string>.GetAllAsync()
    {
        throw new NotImplementedException();
    }
    */
}