"""Glass dashboard components."""

from __future__ import annotations

from bot.core.metrics import snapshot
from bot.services.persona import current_persona
from bot.services.ai_state import AI_STATE
from bot.services.runtime import recent_pipeline_stats
from telegram import InlineKeyboardButton, InlineKeyboardMarkup


GLASS_BAR = "◈" * 12


def glass_header(icon: str, title: str, subtitle: str) -> str:
    return (
        f"<blockquote>{GLASS_BAR}</blockquote>\n"
        f"{icon} <b>{title}</b>\n"
        f"<i>{subtitle}</i>\n"
        f"<blockquote>{GLASS_BAR}</blockquote>"
    )


def dashboard_keyboard() -> InlineKeyboardMarkup:
    return InlineKeyboardMarkup(
        [
            [
                InlineKeyboardButton("🛡 Moderation", callback_data="glass:moderation"),
                InlineKeyboardButton("🔒 Security", callback_data="glass:locks"),
            ],
            [
                InlineKeyboardButton("🤖 AI Core", callback_data="glass:ai"),
                InlineKeyboardButton("📊 Runtime", callback_data="glass:runtime"),
            ],
            [
                InlineKeyboardButton("🎛 Control Center", callback_data="glass:settings"),
                InlineKeyboardButton("📚 Commands", callback_data="glass:help"),
            ],
            [
                InlineKeyboardButton("⚡ Smart Tools", callback_data="glass:tools"),
            ],
            [
                InlineKeyboardButton("➕ Add To Group", url="https://t.me/bobi4bot?startgroup=true"),
            ],
        ]
    )


def section_keyboard(section: str) -> InlineKeyboardMarkup:
    actions: dict[str, list[list[InlineKeyboardButton]]] = {
        "moderation": [
            [
                InlineKeyboardButton("🚫 Ban", callback_data="quick:ban"),
                InlineKeyboardButton("🔇 Mute", callback_data="quick:mute"),
            ],
            [
                InlineKeyboardButton("⚠️ Warn", callback_data="quick:warn"),
                InlineKeyboardButton("🧹 Purge", callback_data="quick:purge"),
            ],
        ],
        "locks": [
            [
                InlineKeyboardButton("🔗 Links", callback_data="toggle:links"),
                InlineKeyboardButton("📷 Media", callback_data="toggle:media"),
            ],
            [
                InlineKeyboardButton("🤖 Bots", callback_data="toggle:bots"),
                InlineKeyboardButton("🌊 Flood", callback_data="toggle:flood"),
            ],
        ],
        "runtime": [
            [
                InlineKeyboardButton("♻️ Refresh", callback_data="runtime:refresh"),
                InlineKeyboardButton("🧠 Memory", callback_data="runtime:memory"),
            ],
            [
                InlineKeyboardButton("🧵 Tasks", callback_data="runtime:tasks"),
                InlineKeyboardButton("📊 Metrics", callback_data="runtime:metrics"),
            ],
        ],
        "ai": [
            [
                InlineKeyboardButton(
                    f"{'🟢' if AI_STATE['enabled'] else '🔴'} AI",
                    callback_data="ai:toggle",
                ),
                InlineKeyboardButton(
                    f"🎭 {current_persona()}",
                    callback_data="persona:panel",
                ),
            ],
            [
                InlineKeyboardButton("🥷 Low", callback_data="ai:reason:low"),
                InlineKeyboardButton("🧩 Medium", callback_data="ai:reason:medium"),
                InlineKeyboardButton("🚀 High", callback_data="ai:reason:high"),
            ],
            [
                InlineKeyboardButton(
                    f"{'💬' if AI_STATE['contextual'] else '🔕'} Context",
                    callback_data="ai:contextual",
                ),
            ],
        ],
    }

    rows = actions.get(section, [])

    rows.append([
        InlineKeyboardButton("🏠 Dashboard", callback_data="mm_main"),
    ])

    return InlineKeyboardMarkup(rows)


def runtime_widget() -> str:
    metrics = snapshot()
    pipeline = recent_pipeline_stats()

    total_events = sum(metrics.values()) if metrics else 0

    return (
        "<blockquote>"
        f"⚡ Pipeline: {pipeline['avg_ms']}ms avg\n"
        f"🚨 Peak: {pipeline['max_ms']}ms\n"
        f"📊 Events: {total_events}\n"
        f"🧠 Metrics: {len(metrics)}"
        "</blockquote>"
    )
