<?php

declare(strict_types=1);

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

use App\Framework\Core\ValueObjects\PhoneNumber;
use App\Framework\HttpClient\CurlHttpClient;
use App\Framework\Notification\Channels\WhatsApp\WhatsAppClient;
use App\Framework\Notification\Channels\WhatsApp\WhatsAppConfig;
use App\Framework\Notification\Channels\WhatsApp\ValueObjects\WhatsAppTemplateId;

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

try {
    // 1. Create WhatsApp configuration
    echo "1️⃣ Creating WhatsApp configuration...\n";
    $config = WhatsAppConfig::createDefault();
    echo "   ✅ Config created\n";
    echo "   📞 Phone Number ID: {$config->phoneNumberId}\n";
    echo "   🔗 API URL: {$config->getApiUrl()}\n\n";

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

    // 3. Test phone number
    $testPhoneNumber = PhoneNumber::fromString('+4917941122213');
    echo "3️⃣ Test recipient: {$testPhoneNumber->toDisplayFormat()}\n\n";

    // 4. Send text message
    echo "4️⃣ Sending text message...\n";
    try {
        $response = $whatsappClient->sendTextMessage(
            to: $testPhoneNumber,
            message: "🎉 Test message from Custom PHP Framework!\n\nThis is a test notification via WhatsApp Business API."
        );

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

    // 5. Send template message (hello_world template)
    echo "5️⃣ Sending template message...\n";
    try {
        $templateResponse = $whatsappClient->sendTemplateMessage(
            to: $testPhoneNumber,
            templateId: WhatsAppTemplateId::fromString('hello_world'),
            languageCode: 'en_US'
        );

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

    // 6. Test with parameters (if you have a template with parameters)
    echo "6️⃣ Sending template with parameters...\n";
    try {
        $paramResponse = $whatsappClient->sendTemplateMessage(
            to: $testPhoneNumber,
            templateId: WhatsAppTemplateId::fromString('sample_template'), // Replace with your template
            languageCode: 'en',
            parameters: ['John Doe', '2024-12-20']
        );

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

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

    echo "📝 Notes:\n";
    echo "   - Replace test phone number with your WhatsApp number\n";
    echo "   - Phone number must be in E.164 format (+country code + number)\n";
    echo "   - Make sure the number is registered with your WhatsApp Business account\n";
    echo "   - Template names must be approved in your WhatsApp Business account\n";
} catch (\Throwable $e) {
    echo "\n❌ Test failed: {$e->getMessage()}\n";
    echo "Stack trace:\n{$e->getTraceAsString()}\n";
    exit(1);
}
