using ErsatzTV.Application;
using ErsatzTV.Application.MediaCollections;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Tests.Support;
using LanguageExt;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using Shouldly;
using static LanguageExt.Prelude;

namespace ErsatzTV.Tests.Application.MediaCollections;

/// <summary>
///     Contract tests for the #253 optimistic-concurrency mechanic on the Playlist aggregate (mirrors
///     ReplaceBlockItemsHandlerConcurrencyTests, the Block reference implementation): the handler
///     pre-check (stale If-Match → 412), the force-write path (no If-Match), the unconditional Version
///     bump on every save, and the EF concurrency-token backstop that catches a writer that lost the
///     load→save race. The backstop test is non-vacuous by construction — remove the
///     <c>IsConcurrencyToken()</c> config on Playlist and the losing save silently succeeds instead of
///     mapping to a <see cref="PreconditionFailedError" />.
/// </summary>
[TestFixture]
public class ReplacePlaylistItemsHandlerConcurrencyTests
{
    private InMemoryTvContext _db = null!;

    [SetUp]
    public async Task SetUp() => _db = await InMemoryTvContext.CreateAsync();

    [TearDown]
    public async Task TearDown() => await _db.DisposeAsync();

    private async Task SeedPlaylistAsync(int version)
    {
        await using TvContext ctx = _db.CreateContext();
        ctx.Playlists.Add(
            new Playlist
            {
                Id = 1,
                PlaylistGroupId = 1,
                Name = "Kids",
                IsSystem = false,
                Version = version,
                Items = new List<PlaylistItem>()
            });
        await ctx.SaveChangesAsync();
    }

    private static ReplacePlaylistItems Command(Option<int> expectedVersion) =>
        new(
            1,
            "Kids",
            new List<ReplacePlaylistItem>
            {
                new(0, CollectionType.Movie, null, null, null, 55, PlaybackOrder.Shuffle, null, false, true)
            },
            expectedVersion.Map(v => new[] { v }.ToSeq()));

    private async Task<int> ReadVersionAsync()
    {
        await using TvContext ctx = _db.CreateContext();
        return await ctx.Playlists.Where(p => p.Id == 1).Select(p => p.Version).SingleAsync();
    }

    private static BaseError? LeftOrNull<T>(Either<BaseError, T> result) =>
        result.Match<BaseError?>(Right: _ => null, Left: e => e);

    [Test]
    public async Task Stale_If_Match_Should_Fail_Precondition_And_Not_Mutate()
    {
        await SeedPlaylistAsync(version: 2);
        var handler = new ReplacePlaylistItemsHandler(_db.Factory);

        Either<BaseError, List<PlaylistItemViewModel>> result =
            await handler.Handle(Command(Some(1)), CancellationToken.None);

        LeftOrNull(result).ShouldBeOfType<PreconditionFailedError>();

        // The pre-check runs before any mutation: version unchanged, no items written.
        (await ReadVersionAsync()).ShouldBe(2);
        await using TvContext ctx = _db.CreateContext();
        (await ctx.PlaylistItems.CountAsync(i => i.PlaylistId == 1)).ShouldBe(0);
    }

    [Test]
    public async Task Matching_If_Match_Should_Succeed_And_Bump_Version()
    {
        await SeedPlaylistAsync(version: 2);
        var handler = new ReplacePlaylistItemsHandler(_db.Factory);

        Either<BaseError, List<PlaylistItemViewModel>> result =
            await handler.Handle(Command(Some(2)), CancellationToken.None);

        result.IsRight.ShouldBeTrue();
        (await ReadVersionAsync()).ShouldBe(3);
    }

    [Test]
    public async Task Absent_If_Match_Should_Force_Write_And_Bump_Version()
    {
        await SeedPlaylistAsync(version: 2);
        var handler = new ReplacePlaylistItemsHandler(_db.Factory);

        // None expected version = Phase-1 force-write regardless of the stored version.
        Either<BaseError, List<PlaylistItemViewModel>> result =
            await handler.Handle(Command(None), CancellationToken.None);

        result.IsRight.ShouldBeTrue();
        (await ReadVersionAsync()).ShouldBe(3);
    }

    [Test]
    public async Task Save_Should_Bump_Version_Even_When_Content_Is_Unchanged()
    {
        await SeedPlaylistAsync(version: 5);
        var handler = new ReplacePlaylistItemsHandler(_db.Factory);

        // Same content twice: the unconditional bump (M1) must still rotate the version each time,
        // otherwise a no-op PUT-back would not fire the token or rotate other clients' ETags.
        (await handler.Handle(Command(Some(5)), CancellationToken.None)).IsRight.ShouldBeTrue();
        (await ReadVersionAsync()).ShouldBe(6);
        (await handler.Handle(Command(Some(6)), CancellationToken.None)).IsRight.ShouldBeTrue();
        (await ReadVersionAsync()).ShouldBe(7);
    }

    [Test]
    public async Task Racing_Save_Should_Map_Concurrency_Failure_To_412()
    {
        await SeedPlaylistAsync(version: 1);

        // Two writers load the same version, then both bump-and-save. The IsConcurrencyToken guard on
        // Playlist makes the second UPDATE key on the original version; it matches zero rows and throws
        // DbUpdateConcurrencyException, which the shared save helper maps to a PreconditionFailedError.
        await using TvContext ctxWinner = _db.CreateContext();
        await using TvContext ctxLoser = _db.CreateContext();

        Playlist winner = await ctxWinner.Playlists.SingleAsync(p => p.Id == 1);
        Playlist loser = await ctxLoser.Playlists.SingleAsync(p => p.Id == 1);

        winner.Version++;
        Either<BaseError, Unit> winnerResult = await ctxWinner.SaveChangesWithConcurrencyGuard(CancellationToken.None);
        winnerResult.IsRight.ShouldBeTrue();

        loser.Version++;
        Either<BaseError, Unit> loserResult = await ctxLoser.SaveChangesWithConcurrencyGuard(CancellationToken.None);

        LeftOrNull(loserResult).ShouldBeOfType<PreconditionFailedError>();

        // Non-vacuous: the winner's write stuck at exactly its +1; the loser did not overwrite it.
        (await ReadVersionAsync()).ShouldBe(2);
    }
}
