using ErsatzTV.Application.MediaCollections;
using ErsatzTV.Core;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Core.Search;
using LanguageExt;
using ErsatzTV.Tests.Support;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
using Unit = LanguageExt.Unit;

namespace ErsatzTV.Tests.Application.MediaCollections;

[TestFixture]
public class SmartCollectionHandlerTests : MediaCollectionHandlerTestBase
{
    [Test]
    public async Task Update_Should_Return_NotFoundError_When_SmartCollection_Missing()
    {
        var handler = new UpdateSmartCollectionHandler(
            Db.Factory,
            Substitute.For<IMediaCollectionRepository>(),
            Worker,
            SearchTargets,
            Substitute.For<ISmartCollectionCache>());

        Either<BaseError, UpdateSmartCollectionResult> result =
            await handler.Handle(new UpdateSmartCollection(999, "Updated", "tag:updated"), CancellationToken.None);

        LeftOf(result).ShouldBeOfType<NotFoundError>();
    }

    [Test]
    public async Task Delete_Should_Return_NotFoundError_When_SmartCollection_Missing()
    {
        var handler = new DeleteSmartCollectionHandler(
            Db.Factory,
            SearchTargets,
            Substitute.For<ISmartCollectionCache>());

        Either<BaseError, Unit> result =
            await handler.Handle(new DeleteSmartCollection(999), CancellationToken.None);

        LeftOf(result).ShouldBeOfType<NotFoundError>();
    }

    private static BaseError LeftOf<TR>(Either<BaseError, TR> either) =>
        either.Match(Left: e => e, Right: _ => throw new AssertionException("Expected a Left result"));
}
