using Dapper;
using Npgsql;
using Telegram.Bot;
using GmRelay.Shared.Domain;

namespace GmRelay.Bot.Features.Sessions.ListSessions;

public sealed record DeleteSessionCommand(
    Guid SessionId,
    long TelegramUserId,
    string CallbackQueryId,
    long ChatId,
    int MessageId);

internal sealed record DeleteSessionInfoDto(string Title, Guid BatchId, bool CanManage, int? ThreadId);

public sealed class DeleteSessionHandler(
    NpgsqlDataSource dataSource,
    ITelegramBotClient bot,
    ILogger<DeleteSessionHandler> logger)
{
    public async Task HandleAsync(DeleteSessionCommand command, CancellationToken ct)
    {
        await using var connection = await dataSource.OpenConnectionAsync(ct);
        await using var transaction = await connection.BeginTransactionAsync(ct);

        // 1. Fetch session and verify group manager.
        var session = await connection.QuerySingleOrDefaultAsync<DeleteSessionInfoDto>(
            """
            SELECT s.title AS Title,
                   s.batch_id AS BatchId,
                   s.thread_id AS ThreadId,
                   EXISTS (
                       SELECT 1
                       FROM group_managers gm
                       JOIN players p ON p.id = gm.player_id
                       WHERE gm.group_id = s.group_id
                         AND p.telegram_id = @TelegramUserId
                   ) AS CanManage
            FROM sessions s
            WHERE s.id = @SessionId
            """,
            new { command.SessionId, command.TelegramUserId }, transaction);

        if (session == null)
        {
            await bot.AnswerCallbackQuery(command.CallbackQueryId, "Сессия не найдена.", cancellationToken: ct);
            return;
        }

        if (!session.CanManage)
        {
            await bot.AnswerCallbackQuery(command.CallbackQueryId, "Только owner или co-GM может удалять сессию.", showAlert: true, cancellationToken: ct);
            return;
        }

        // 2. Delete session
        await connection.ExecuteAsync("DELETE FROM sessions WHERE id = @Id", new { Id = command.SessionId }, transaction);

        // 3. Check if any sessions are left in the batch
        var remainingInBatch = await connection.ExecuteScalarAsync<int>(
            "SELECT COUNT(*) FROM sessions WHERE batch_id = @BatchId",
            new { BatchId = session.BatchId }, transaction);

        await transaction.CommitAsync(ct);

        // 4. If no sessions left and we have a forum topic, delete the topic
        if (remainingInBatch == 0 && session.ThreadId.HasValue)
        {
            try
            {
                await bot.DeleteForumTopic(command.ChatId, session.ThreadId.Value, cancellationToken: ct);
                logger.LogInformation("Deleted forum topic {ThreadId} for batch {BatchId} as no sessions remained.", session.ThreadId.Value, session.BatchId);
            }
            catch (Exception ex)
            {
                logger.LogWarning(ex, "Failed to delete forum topic {ThreadId}", session.ThreadId.Value);
            }
        }

        await bot.AnswerCallbackQuery(command.CallbackQueryId, "Сессия удалена!", cancellationToken: ct);

        // 5. Update the /listsessions message (we delete the message or edit it to remove the button)
        // A simple way is to re-render the list:
        await using var readConnection = await dataSource.OpenConnectionAsync(ct);
        var sessions = await readConnection.QueryAsync<SessionListItemDto>(
            @"SELECT s.id as Id, s.title as Title, s.scheduled_at as ScheduledAt, s.status as Status, s.max_players as MaxPlayers,
                     COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Active) as PlayerCount,
                     COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Waitlisted) as WaitlistCount,
                     EXISTS (
                         SELECT 1
                         FROM group_managers gm
                         JOIN players manager_player ON manager_player.id = gm.player_id
                         WHERE gm.group_id = s.group_id
                           AND manager_player.telegram_id = @TelegramUserId
                     ) AS CanManage
              FROM sessions s
              JOIN game_groups g ON s.group_id = g.id
              LEFT JOIN session_participants sp ON s.id = sp.session_id
              WHERE g.telegram_chat_id = @ChatId AND s.status != @Cancelled AND s.scheduled_at > NOW()
              GROUP BY s.id, s.title, s.scheduled_at, s.status, s.max_players, s.group_id
              ORDER BY s.scheduled_at ASC",
            new
            {
                ChatId = command.ChatId,
                command.TelegramUserId,
                Cancelled = SessionStatus.Cancelled,
                Active = ParticipantRegistrationStatus.Active,
                Waitlisted = ParticipantRegistrationStatus.Waitlisted
            });

        var sessionsList = sessions.ToList();

        if (sessionsList.Count == 0)
        {
            try { await bot.EditMessageText(command.ChatId, command.MessageId, "📭 В этой группе нет предстоящих игр.", cancellationToken: ct); } catch {}
            return;
        }

        var renderResult = SessionListMessageRenderer.Render(sessionsList);

        try
        {
            await bot.EditMessageText(
                command.ChatId,
                command.MessageId,
                renderResult.Text,
                parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
                replyMarkup: renderResult.Markup,
                cancellationToken: ct);
        }
        catch (Exception ex)
        {
            logger.LogWarning(ex, "Failed to edit list sessions message");
        }
    }
}
