using System.IO;

namespace GmRelay.Bot.Tests.Discord;

public sealed class DiscordListSessionsHandlerTests
{
    private static string GetRepoRoot()
    {
        var dir = AppContext.BaseDirectory;
        while (!string.IsNullOrEmpty(dir) && !File.Exists(Path.Combine(dir, "Directory.Build.props")))
        {
            dir = Directory.GetParent(dir)?.FullName;
        }

        return dir ?? throw new InvalidOperationException("Could not find repo root");
    }

    [Fact]
    public void Handler_ShouldExist()
    {
        var repoRoot = GetRepoRoot();
        var handlerPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Features", "Sessions", "DiscordListSessionsHandler.cs");

        Assert.True(File.Exists(handlerPath), "DiscordListSessionsHandler should exist.");
    }

    [Fact]
    public void Handler_ShouldQueryByPlatformAndExternalGroupId()
    {
        var repoRoot = GetRepoRoot();
        var handlerPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Features", "Sessions", "DiscordListSessionsHandler.cs");
        var handler = File.ReadAllText(handlerPath);

        Assert.Contains("platform = 'Discord'", handler, StringComparison.Ordinal);
        Assert.Contains("external_group_id = @GuildId", handler, StringComparison.Ordinal);
        Assert.Contains("scheduled_at > now() - interval '4 hours'", handler, StringComparison.Ordinal);
    }

    [Fact]
    public void Handler_ShouldIncludeRecentlyStartedSessionsForCleanup()
    {
        var repoRoot = GetRepoRoot();
        var handlerPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Features", "Sessions", "DiscordListSessionsHandler.cs");
        var handler = File.ReadAllText(handlerPath);

        Assert.Contains("now() - interval '4 hours'", handler, StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public void Handler_ShouldNotContainTelegramSpecificColumns()
    {
        var repoRoot = GetRepoRoot();
        var handlerPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Features", "Sessions", "DiscordListSessionsHandler.cs");
        var handler = File.ReadAllText(handlerPath);

        Assert.DoesNotContain("telegram_chat_id", handler, StringComparison.Ordinal);
        Assert.DoesNotContain("telegram_id", handler, StringComparison.Ordinal);
    }

    [Fact]
    public void Handler_ShouldExposeDeleteActionForManagers()
    {
        var repoRoot = GetRepoRoot();
        var handlerPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Features", "Sessions", "DiscordListSessionsHandler.cs");
        var handler = File.ReadAllText(handlerPath);

        Assert.Contains("delete_session", handler, StringComparison.Ordinal);
        Assert.Contains("CanManageSchedule", handler, StringComparison.Ordinal);
    }

    [Fact]
    public void Command_ShouldExist()
    {
        var repoRoot = GetRepoRoot();
        var commandPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Features", "Sessions", "DiscordListSessionsCommand.cs");

        Assert.True(File.Exists(commandPath), "DiscordListSessionsCommand should exist.");
    }

    [Fact]
    public void Command_ShouldBeSlashCommandModule()
    {
        var repoRoot = GetRepoRoot();
        var commandPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Features", "Sessions", "DiscordListSessionsCommand.cs");
        var command = File.ReadAllText(commandPath);

        Assert.Contains("SlashCommand", command, StringComparison.Ordinal);
        Assert.Contains("listsessions", command, StringComparison.Ordinal);
    }

    [Fact]
    public void DeleteHandler_ShouldDeleteOnlySessionsFromTheInteractionGuild()
    {
        var repoRoot = GetRepoRoot();
        var handlerPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Features", "Sessions", "DiscordDeleteSessionHandler.cs");

        Assert.True(File.Exists(handlerPath), "DiscordDeleteSessionHandler should exist.");

        var handler = File.ReadAllText(handlerPath);
        Assert.Contains("DELETE FROM sessions", handler, StringComparison.Ordinal);
        Assert.Contains("external_group_id = @GuildId", handler, StringComparison.Ordinal);
        Assert.Contains("CanManageSchedule", handler, StringComparison.Ordinal);
    }
}
