using GmRelay.Shared.Domain;
using GmRelay.Shared.Platform;
using GmRelay.Shared.Features.Sessions.ListSessions;

namespace GmRelay.Bot.Features.Sessions.ListSessions;

internal static class SessionListMessageRenderer
{
    public static string RenderText(IReadOnlyList<SessionListItemDto> sessions)
    {
        var text = "📅 <b>Ближайшие игры:</b>\n\n";
        foreach (var session in sessions)
        {
            var seats = session.MaxPlayers.HasValue
                ? $"{session.PlayerCount}/{session.MaxPlayers.Value}"
                : session.PlayerCount.ToString(System.Globalization.CultureInfo.InvariantCulture);
            var waitlist = session.WaitlistCount > 0 ? $", ожидание: {session.WaitlistCount}" : string.Empty;
            text += $"🔹 <b>{session.ScheduledAt.FormatMoscow()}</b> — {System.Net.WebUtility.HtmlEncode(session.Title)} (Места: {seats}{waitlist})\n";
        }

        return text;
    }

    public static IReadOnlyList<PlatformMessageAction> RenderActions(IReadOnlyList<SessionListItemDto> sessions)
    {
        if (sessions.Count == 0 || !sessions.First().CanManage)
        {
            return [];
        }

        var actions = new List<PlatformMessageAction>();

        foreach (var session in sessions)
        {
            var dateTitle = session.ScheduledAt.FormatMoscowShort();

            actions.Add(new PlatformMessageAction(
                $"cancel_session:{session.Id}",
                $"❌ {dateTitle}",
                $"cancel_session:{session.Id}"));

            actions.Add(new PlatformMessageAction(
                $"reschedule_session:{session.Id}",
                $"⏰ {dateTitle}",
                $"reschedule_session:{session.Id}"));

            if (SessionCapacityRules.CanPromoteWaitlistedPlayer(session.MaxPlayers, session.PlayerCount, session.WaitlistCount))
            {
                actions.Add(new PlatformMessageAction(
                    $"promote_waitlist:{session.Id}",
                    $"⬆️ Из ожидания {dateTitle}",
                    $"promote_waitlist:{session.Id}"));
            }

            actions.Add(new PlatformMessageAction(
                $"delete_session:{session.Id}",
                $"🗑 Удалить {dateTitle}",
                $"delete_session:{session.Id}"));
        }

        return actions;
    }
}
