using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Dapper;
using Npgsql;

namespace GmRelay.DiscordBot.Features.Sessions.Wizard;

/// <summary>
/// Small lookup helper for Discord permission checks. The
/// <see cref="DiscordNewSessionHandler"/> already runs the same SQL
/// inline; this class is here so the wizard slash command can do the
/// same check without duplicating the query string.
/// </summary>
internal static class DiscordPermissionLookup
{
    public static async Task<IReadOnlyList<ulong>> LoadManagerUserIdsAsync(
        NpgsqlDataSource dataSource,
        ulong guildId,
        CancellationToken cancellationToken)
    {
        const string sql = """
            SELECT CAST(p.external_user_id AS BIGINT)
            FROM group_managers gm
            JOIN players p ON p.id = gm.player_id
            JOIN game_groups g ON g.id = gm.group_id
            WHERE g.platform = 'Discord'
              AND p.platform = 'Discord'
              AND g.external_group_id = @GuildId
            """;

        await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
        // NativeAOT: direct overload — see TelegramWizardMessenger.
        var rows = await connection.QueryAsync<ulong>(
            sql,
            new { GuildId = guildId.ToString() });
        return rows.ToList();
    }
}
