<?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\{TelegramBotToken, TelegramChatId};

echo "🔷 Telegram Notification Test\n";
echo str_repeat('=', 50) . "\n\n";

try {
    // 1. Create Telegram configuration
    echo "1️⃣ Creating Telegram configuration...\n";

    $config = TelegramConfig::createDefault();

    echo "   ✅ Config created\n";
    echo "   🔗 API URL: {$config->getApiUrl()}\n\n";

    // 2. Create HTTP client and Telegram client
    echo "2️⃣ Creating Telegram client...\n";
    $httpClient = new CurlHttpClient();
    $telegramClient = new TelegramClient($httpClient, $config);
    echo "   ✅ Client created\n\n";

    // 3. Test bot info
    echo "3️⃣ Testing bot connection (getMe)...\n";
    try {
        $botInfo = $telegramClient->getMe();
        echo "   ✅ Bot connected successfully!\n";
        echo "   🤖 Bot Name: {$botInfo['first_name']}\n";
        echo "   📛 Username: @{$botInfo['username']}\n";
        echo "   🆔 Bot ID: {$botInfo['id']}\n\n";
    } catch (\Throwable $e) {
        echo "   ❌ Bot connection failed: {$e->getMessage()}\n\n";
    }

    // 4. Test chat ID
    $testChatId = TelegramChatId::fromString('8240973979');
    echo "4️⃣ Test recipient: {$testChatId->toString()}\n\n";

    // 5. Send text message
    echo "5️⃣ Sending text message...\n";
    try {
        $response = $telegramClient->sendMessage(
            chatId: $testChatId,
            text: "🎉 Test message from Custom PHP Framework!\n\nThis is a test notification via Telegram Bot API.",
            parseMode: 'Markdown'
        );

        echo "   ✅ Message sent successfully!\n";
        echo "   📨 Message ID: {$response->messageId->toString()}\n\n";
    } catch (\Throwable $e) {
        echo "   ❌ Text message failed: {$e->getMessage()}\n\n";
    }

    // 6. Send formatted message
    echo "6️⃣ Sending formatted message with Markdown...\n";
    try {
        $formattedText = "*Bold Title*\n\n" .
                        "_Italic text_\n\n" .
                        "`Code block`\n\n" .
                        "[Click here](https://example.com)";

        $response = $telegramClient->sendMessage(
            chatId: $testChatId,
            text: $formattedText,
            parseMode: 'Markdown'
        );

        echo "   ✅ Formatted message sent!\n";
        echo "   📨 Message ID: {$response->messageId->toString()}\n\n";
    } catch (\Throwable $e) {
        echo "   ℹ️ Formatted message skipped: {$e->getMessage()}\n\n";
    }

    echo "✅ Telegram notification test completed!\n\n";

    echo "📝 Notes:\n";
    echo "   - Create a bot via @BotFather on Telegram\n";
    echo "   - Get your chat ID by messaging the bot and checking /getUpdates\n";
    echo "   - Replace YOUR_BOT_TOKEN_HERE with actual bot token\n";
    echo "   - Replace YOUR_CHAT_ID_HERE with your actual chat ID\n";
    echo "   - Bot token format: 123456789:ABCdefGHIjklMNOpqrsTUVwxyz\n";
} catch (\Throwable $e) {
    echo "\n❌ Test failed: {$e->getMessage()}\n";
    echo "Stack trace:\n{$e->getTraceAsString()}\n";
    exit(1);
}
