<?php

declare(strict_types=1);

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

use App\Framework\Core\AppBootstrapper;
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 "🧪 Test Telegram Webhook with Callback Buttons\n";
echo str_repeat('=', 60) . "\n\n";

// Bootstrap application
$container = (new AppBootstrapper())->boot();
$client = $container->get(TelegramClient::class);
$config = $container->get(TelegramConfig::class);

// Get bot info
$botInfo = $client->getMe();
echo "🤖 Bot: {$botInfo['first_name']} (@{$botInfo['username']})\n\n";

// Chat ID (from FixedChatIdResolver)
$chatId = TelegramChatId::fromInt(8240973979);

echo "📤 Sending test message with callback buttons...\n\n";

try {
    // Create inline keyboard with callback buttons
    $keyboard = InlineKeyboard::multiRow(
        [
            InlineKeyboardButton::withCallback('✅ Approve Order #123', 'approve_order_123'),
            InlineKeyboardButton::withCallback('❌ Reject Order #123', 'reject_order_123'),
        ],
        [
            InlineKeyboardButton::withUrl('📄 View Details', 'https://example.com/order/123'),
        ]
    );

    $response = $client->sendMessage(
        chatId: $chatId,
        text: "*New Order Received* 🛒\n\n" .
              "Order ID: #123\n" .
              "Customer: John Doe\n" .
              "Total: €99.99\n\n" .
              "Please approve or reject this order:",
        parseMode: 'Markdown',
        keyboard: $keyboard
    );

    echo "✅ Message sent! (ID: {$response->messageId->value})\n\n";

    echo "📋 What happens next:\n";
    echo "   1. Check your Telegram bot for the message\n";
    echo "   2. Click on ✅ Approve or ❌ Reject button\n";
    echo "   3. The webhook will receive the callback query\n";
    echo "   4. TelegramWebhookEventHandler processes it\n";
    echo "   5. CallbackRouter routes to ApproveOrderHandler/RejectOrderHandler\n";
    echo "   6. You'll see a notification and the message will be updated\n\n";

    echo "🔍 Monitor webhook requests:\n";
    echo "   - Check your web server logs\n";
    echo "   - Check framework logs for webhook events\n\n";

    echo "💡 Tip: The buttons use callback data:\n";
    echo "   - approve_order_123 → command: 'approve_order', parameter: '123'\n";
    echo "   - reject_order_123 → command: 'reject_order', parameter: '123'\n\n";

} catch (\Exception $e) {
    echo "❌ Error: {$e->getMessage()}\n";
    exit(1);
}

echo "✨ Test complete!\n";
