using System;
using System.Collections.Generic;
using System.Linq;
using GmRelay.Shared.Features.Sessions.CreateSession.Wizard;
using Telegram.Bot.Types.ReplyMarkups;

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

/// <summary>
/// Telegram-side renderer for wizard keyboards. Acts as the adapter
/// between the platform-neutral <see cref="WizardAction"/> list
/// produced by <see cref="WizardStepViewBuilder"/> and Telegram's
/// <see cref="InlineKeyboardMarkup"/>. Each <see cref="WizardAction"/>
/// becomes its own row (matching the pre-refactor Telegram layout).
/// <see cref="WizardActionStyle"/> is currently ignored by Telegram
/// because the platform has no native primary/danger/success button
/// colours.
/// </summary>
public static class WizardStep
{
    public const int MaxTitleLength = WizardStepLimits.MaxTitleLength;
    public const int MaxDescriptionLength = WizardStepLimits.MaxDescriptionLength;
    public const int MaxSystemLength = WizardStepLimits.MaxSystemLength;
    public const int MaxCapacity = WizardStepLimits.MaxCapacity;
    public const int MinCapacity = WizardStepLimits.MinCapacity;
    public const int MinDurationHours = WizardStepLimits.MinDurationHours;
    public const int MaxDurationHours = WizardStepLimits.MaxDurationHours;

    /// <summary>
    /// Render the platform-neutral view into a (text, Telegram keyboard)
    /// pair. Used by the wizard's surrounding code (router, create
    /// handler) when it needs to send a fresh draft message or render
    /// the resume/reset menu.
    /// </summary>
    public static (string Text, InlineKeyboardMarkup Keyboard) Render(
        WizardDraft draft,
        WizardPayload payload,
        IReadOnlyList<WizardClubOption>? clubs = null)
    {
        var (text, actions) = WizardStepViewBuilder.Build(draft, payload, clubs);
        return (text, ToInlineKeyboard(actions));
    }

    /// <summary>
    /// Convert a flat list of <see cref="WizardAction"/>s into a
    /// Telegram keyboard. Each action is placed in its own row to
    /// preserve the pre-refactor visual layout.
    /// </summary>
    public static InlineKeyboardMarkup ToInlineKeyboard(IReadOnlyList<WizardAction> actions)
    {
        if (actions.Count == 0)
        {
            return new InlineKeyboardMarkup(Array.Empty<InlineKeyboardButton[]>());
        }
        var rows = new InlineKeyboardButton[actions.Count][];
        for (var i = 0; i < actions.Count; i++)
        {
            rows[i] = new[]
            {
                InlineKeyboardButton.WithCallbackData(actions[i].Label, actions[i].Payload),
            };
        }
        return new InlineKeyboardMarkup(rows);
    }
}
