namespace GmRelay.Bot.Tests.Features.Sessions.CreateSession;

public sealed class PlatformNeutralSessionInteractionSqlTests
{
    [Fact]
    public async Task JoinSessionHandler_ShouldPersistPlayersByPlatformIdentity()
    {
        var handler = await ReadRepositoryFileAsync("src/GmRelay.Shared/Features/Sessions/CreateSession/JoinSessionHandler.cs");

        Assert.Contains("platform, external_user_id", handler, StringComparison.Ordinal);
        Assert.Contains("ON CONFLICT (platform, external_user_id)", handler, StringComparison.Ordinal);
        Assert.Contains("ExternalUserId", handler, StringComparison.Ordinal);
        Assert.Contains("ExternalUsername", handler, StringComparison.Ordinal);
        Assert.DoesNotContain("TelegramPlatformIds.", handler, StringComparison.Ordinal);
        Assert.DoesNotContain("command.TelegramUserId", handler, StringComparison.Ordinal);
        Assert.DoesNotContain("command.TelegramUsername", handler, StringComparison.Ordinal);
    }

    [Fact]
    public async Task JoinSessionHandler_ShouldRejectCancelledSessionsBeforeInsert()
    {
        var handler = await ReadRepositoryFileAsync("src/GmRelay.Shared/Features/Sessions/CreateSession/JoinSessionHandler.cs");

        Assert.Contains("Status", handler, StringComparison.Ordinal);
        Assert.Contains("SessionStatus.IsCancelled(batchInfo.Status)", handler, StringComparison.Ordinal);
        Assert.Contains("Сессия уже отменена.", handler, StringComparison.Ordinal);
    }

    [Fact]
    public async Task LeaveSessionHandler_ShouldFindParticipantsByPlatformIdentity()
    {
        var handler = await ReadRepositoryFileAsync("src/GmRelay.Shared/Features/Sessions/CreateSession/LeaveSessionHandler.cs");

        Assert.Contains("p.platform = @Platform", handler, StringComparison.Ordinal);
        Assert.Contains("p.external_user_id = @ExternalUserId", handler, StringComparison.Ordinal);
        Assert.DoesNotContain("p.telegram_id = @TelegramUserId", handler, StringComparison.Ordinal);
        Assert.DoesNotContain("TelegramPlatformIds.", handler, StringComparison.Ordinal);
        Assert.DoesNotContain("command.TelegramUserId", handler, StringComparison.Ordinal);
    }

    [Fact]
    public async Task SessionInteractionHandlers_ShouldUpdateSchedulesThroughCommandMessageReference()
    {
        var joinHandler = await ReadRepositoryFileAsync("src/GmRelay.Shared/Features/Sessions/CreateSession/JoinSessionHandler.cs");
        var leaveHandler = await ReadRepositoryFileAsync("src/GmRelay.Shared/Features/Sessions/CreateSession/LeaveSessionHandler.cs");

        Assert.Contains("new PlatformScheduleMessage(", joinHandler, StringComparison.Ordinal);
        Assert.Contains("command.Group", joinHandler, StringComparison.Ordinal);
        Assert.Contains("command.ScheduleMessage", joinHandler, StringComparison.Ordinal);
        Assert.Contains("new PlatformScheduleMessage(", leaveHandler, StringComparison.Ordinal);
        Assert.Contains("command.Group", leaveHandler, StringComparison.Ordinal);
        Assert.Contains("command.ScheduleMessage", leaveHandler, StringComparison.Ordinal);
    }

    [Fact]
    public async Task SessionInteractionHandlers_ShouldSerializeScheduleMessageUpdates()
    {
        var joinHandler = await ReadRepositoryFileAsync("src/GmRelay.Shared/Features/Sessions/CreateSession/JoinSessionHandler.cs");
        var leaveHandler = await ReadRepositoryFileAsync("src/GmRelay.Shared/Features/Sessions/CreateSession/LeaveSessionHandler.cs");

        Assert.Contains("scheduleUpdateLock.AcquireAsync(command.ScheduleMessage", joinHandler, StringComparison.Ordinal);
        Assert.Contains("scheduleUpdateLock.AcquireAsync(command.ScheduleMessage", leaveHandler, StringComparison.Ordinal);
    }

    private static async Task<string> ReadRepositoryFileAsync(string relativePath)
    {
        var directory = new DirectoryInfo(AppContext.BaseDirectory);
        while (directory is not null)
        {
            var candidate = Path.Combine(directory.FullName, relativePath);
            if (File.Exists(candidate))
            {
                return await File.ReadAllTextAsync(candidate);
            }

            directory = directory.Parent;
        }

        throw new FileNotFoundException($"Could not locate repository file '{relativePath}'.");
    }
}
