using Microsoft.EntityFrameworkCore;
using Modules.Library.Database.Database;
using Modules.Library.Database.Database.Models.Anime;
using System.Linq.Expressions;

namespace Modules.Library.Database.Repositories;

public class AnimeEpisodeRepository(AnimeTitleRepository titleRepository, LibraryDbContext context)
{
    private readonly LibraryDbContext _context = context;

    internal async Task<Guid> AddAsync(Guid animeTitleId, Guid? seasonId, AnimeEpisode animeEpisode)
    {
        var dbTitle = await titleRepository.GetFirstWhere(q => q.Id == animeTitleId);
        if (seasonId.HasValue)
        {
            var season = dbTitle.Items.OfType<AnimeSeason>().First(q => q.Id == seasonId);
            season.Episodes.Add(animeEpisode);
        }
        else
        {
            dbTitle.Items.Add(animeEpisode);
        }
        await _context.SaveChangesAsync();
        return animeEpisode.Id;
    }

    private IQueryable<AnimeEpisode> QueryFullIncludes() => _context.AnimeEpisodes
        .Include(q => q.Title)
        .Include(q => q.Season)
        .Include(q => q.CommonProperties).ThenInclude(q => q.Names)
        .Include(q => q.CommonProperties).ThenInclude(q => q.Descriptions)
        .Include(q => q.CommonProperties).ThenInclude(q => q.RelatedContent)
        .Include(q => q.CommonProperties).ThenInclude(q => q.Genres)
        .Include(q => q.CommonProperties).ThenInclude(q => q.Preview)
        .AsQueryable();

    //internal async Task<IEnumerable<AnimeTitle>> GetAllAsync() => await QueryFullIncludes().ToListAsync();

    internal async Task<AnimeEpisode> GetFirstWhere(Expression<Func<AnimeEpisode, bool>> predicate) =>
        await QueryFullIncludes().FirstAsync(predicate);

    internal async Task UpdateAsync(Guid titleId, Guid? seasonId, AnimeEpisode animeEpisode)
    {
        var dbEpisodeQuery = QueryFullIncludes().Where(q => q.Title.Id == titleId && q.Id == animeEpisode.Id);
        if (seasonId.HasValue)
        {
            dbEpisodeQuery = dbEpisodeQuery.Where(q => q.Season.Id == seasonId);
        }

        var dbEpisode = await dbEpisodeQuery.FirstAsync();
        CommonPropertiesHelper.SyncCommonProperties(dbEpisode.CommonProperties, animeEpisode.CommonProperties);
        dbEpisode.ExpirationTime = animeEpisode.ExpirationTime;
        dbEpisode.Completed = animeEpisode.Completed;
        dbEpisode.Deleted = animeEpisode.Deleted;
        await _context.SaveChangesAsync();
    }

    internal async Task DeleteAsync(Guid titleId, Guid? seasonId, Guid animeEpisodeId)
    {
        var dbEpisodeQuery = QueryFullIncludes().Where(q => q.Title.Id == titleId && q.Id == animeEpisodeId);
        if (seasonId.HasValue)
        {
            dbEpisodeQuery = dbEpisodeQuery.Where(q => q.Season != null && q.Season.Id == seasonId);
        }
        var dbEpisode = await dbEpisodeQuery.FirstAsync();
        _context.AnimeEpisodes.Remove(dbEpisode);
        await _context.SaveChangesAsync();
    }



    //protected override async Task<bool> SoftDeleteAsync(AnimeTitle entity)
    //{
    //    entity.Deleted = true;
    //    return await UpdateAsync(entity);
    //}

    /*
    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();
    }
    */
}