<?php

declare(strict_types=1);

require_once __DIR__ . '/../../vendor/autoload.php';

use App\Framework\HttpClient\CurlHttpClient;
use App\Framework\Notification\Channels\Telegram\TelegramClient;
use App\Framework\Notification\Channels\Telegram\TelegramConfig;
use App\Framework\Notification\Channels\Telegram\ValueObjects\{
    TelegramChatId,
    InlineKeyboard,
    InlineKeyboardButton
};

echo "⌨️  Telegram Inline Keyboards Test\n";
echo str_repeat('=', 60) . "\n\n";

try {
    // 1. Setup
    echo "1️⃣ Creating Telegram client...\n";
    $config = TelegramConfig::createDefault();
    $httpClient = new CurlHttpClient();
    $client = new TelegramClient($httpClient, $config);
    $chatId = TelegramChatId::fromString('8240973979');
    echo "   ✅ Client created\n\n";

    // 2. Single row with URL buttons
    echo "2️⃣ Sending message with URL buttons...\n";
    try {
        $keyboard = InlineKeyboard::singleRow(
            InlineKeyboardButton::withUrl('🌐 Visit Website', 'https://example.com'),
            InlineKeyboardButton::withUrl('📖 Documentation', 'https://docs.example.com')
        );

        $response = $client->sendMessage(
            chatId: $chatId,
            text: "Welcome! Check out these links:",
            parseMode: 'Markdown',
            keyboard: $keyboard
        );

        echo "   ✅ URL buttons sent! Message ID: {$response->messageId->toString()}\n\n";
    } catch (\Throwable $e) {
        echo "   ❌ Failed: {$e->getMessage()}\n\n";
    }

    // 3. Single row with callback buttons
    echo "3️⃣ Sending message with callback buttons...\n";
    try {
        $keyboard = InlineKeyboard::singleRow(
            InlineKeyboardButton::withCallback('✅ Approve', 'approve_order_123'),
            InlineKeyboardButton::withCallback('❌ Reject', 'reject_order_123')
        );

        $response = $client->sendMessage(
            chatId: $chatId,
            text: "*Order #123*\n\nCustomer ordered 3 items for 49.99€\n\nPlease review:",
            parseMode: 'Markdown',
            keyboard: $keyboard
        );

        echo "   ✅ Callback buttons sent! Message ID: {$response->messageId->toString()}\n\n";
    } catch (\Throwable $e) {
        echo "   ❌ Failed: {$e->getMessage()}\n\n";
    }

    // 4. Multi-row keyboard
    echo "4️⃣ Sending message with multi-row keyboard...\n";
    try {
        $keyboard = InlineKeyboard::multiRow([
            // Row 1: Main actions
            [
                InlineKeyboardButton::withCallback('✅ Confirm', 'confirm'),
                InlineKeyboardButton::withCallback('❌ Cancel', 'cancel'),
            ],
            // Row 2: Secondary actions
            [
                InlineKeyboardButton::withCallback('⏸️ Pause', 'pause'),
                InlineKeyboardButton::withCallback('📝 Edit', 'edit'),
            ],
            // Row 3: Help
            [
                InlineKeyboardButton::withUrl('❓ Help', 'https://help.example.com'),
            ]
        ]);

        $response = $client->sendMessage(
            chatId: $chatId,
            text: "*Payment Processing*\n\nAmount: 99.99€\nMethod: Credit Card\n\nChoose an action:",
            parseMode: 'Markdown',
            keyboard: $keyboard
        );

        echo "   ✅ Multi-row keyboard sent! Message ID: {$response->messageId->toString()}\n\n";
    } catch (\Throwable $e) {
        echo "   ❌ Failed: {$e->getMessage()}\n\n";
    }

    // 5. Complex action menu
    echo "5️⃣ Sending complex action menu...\n";
    try {
        $keyboard = InlineKeyboard::multiRow([
            [
                InlineKeyboardButton::withCallback('🎯 Quick Actions', 'menu_quick'),
            ],
            [
                InlineKeyboardButton::withCallback('📊 View Stats', 'stats'),
                InlineKeyboardButton::withCallback('⚙️ Settings', 'settings'),
            ],
            [
                InlineKeyboardButton::withCallback('👤 Profile', 'profile'),
                InlineKeyboardButton::withCallback('🔔 Notifications', 'notifications'),
            ],
            [
                InlineKeyboardButton::withUrl('🌐 Open Dashboard', 'https://dashboard.example.com'),
            ]
        ]);

        $response = $client->sendMessage(
            chatId: $chatId,
            text: "📱 *Main Menu*\n\nWhat would you like to do?",
            parseMode: 'Markdown',
            keyboard: $keyboard
        );

        echo "   ✅ Complex menu sent! Message ID: {$response->messageId->toString()}\n\n";
    } catch (\Throwable $e) {
        echo "   ❌ Failed: {$e->getMessage()}\n\n";
    }

    echo "✅ Inline Keyboards test completed!\n\n";

    echo "📝 Notes:\n";
    echo "   - URL buttons open links in browser\n";
    echo "   - Callback buttons send data back to bot (requires webhook setup)\n";
    echo "   - Max 64 bytes for callback_data\n";
    echo "   - Buttons are arranged in rows (max 8 buttons per row)\n";
    echo "   - Check your Telegram for the interactive messages!\n";
} catch (\Throwable $e) {
    echo "\n❌ Test failed: {$e->getMessage()}\n";
    echo "Stack trace:\n{$e->getTraceAsString()}\n";
    exit(1);
}
