<?php

declare(strict_types=1);

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

use App\Framework\Core\AppBootstrapper;
use App\Framework\Notification\Channels\TelegramChannel;
use App\Framework\Notification\ValueObjects\NotificationChannel;
use App\Framework\Notification\Notification;

/**
 * Practical Telegram Media Sending Example
 *
 * This script demonstrates actual media sending via Telegram
 * using the MediaManager system
 */

echo "📱 Telegram Rich Media Sending Example\n";
echo "=======================================\n\n";

// Bootstrap application
$app = AppBootstrapper::bootstrap();
$container = $app->getContainer();

// Get Telegram channel with MediaManager
$telegramChannel = $container->get(TelegramChannel::class);
$mediaManager = $telegramChannel->mediaManager;

// Check Telegram capabilities
echo "📋 Telegram Media Capabilities:\n";
$capabilities = $mediaManager->getCapabilities(NotificationChannel::TELEGRAM);
echo "   Photos: " . ($capabilities->supportsPhoto ? '✅' : '❌') . "\n";
echo "   Videos: " . ($capabilities->supportsVideo ? '✅' : '❌') . "\n";
echo "   Audio: " . ($capabilities->supportsAudio ? '✅' : '❌') . "\n";
echo "   Documents: " . ($capabilities->supportsDocument ? '✅' : '❌') . "\n";
echo "   Location: " . ($capabilities->supportsLocation ? '✅' : '❌') . "\n\n";

// Create notification
$notification = new Notification(
    userId: 'user_123',
    title: 'Media Test',
    body: 'Testing Telegram media capabilities',
    channel: NotificationChannel::TELEGRAM,
    type: 'media_demo'
);

// Example 1: Send Photo
echo "1️⃣  Sending photo...\n";
try {
    if ($mediaManager->supportsPhoto(NotificationChannel::TELEGRAM)) {
        // Using Telegram's sample photo URL for testing
        // In production, use local file paths or previously uploaded file_id
        $photoUrl = 'https://api.telegram.org/file/bot<token>/photos/file_0.jpg';

        $photoNotification = new Notification(
            userId: 'user_123',
            title: 'Photo Notification',
            body: 'This is a test photo',
            channel: NotificationChannel::TELEGRAM,
            type: 'photo'
        );

        $mediaManager->sendPhoto(
            NotificationChannel::TELEGRAM,
            $photoNotification,
            photoPath: $photoUrl, // Can be URL, file path, or file_id
            caption: '📸 Test photo from MediaManager'
        );

        echo "   ✅ Photo sent successfully\n";
    } else {
        echo "   ❌ Photo not supported\n";
    }
} catch (\Exception $e) {
    echo "   ⚠️  Error: {$e->getMessage()}\n";
}
echo "\n";

// Example 2: Send Location
echo "2️⃣  Sending location...\n";
try {
    if ($mediaManager->supportsLocation(NotificationChannel::TELEGRAM)) {
        $locationNotification = new Notification(
            userId: 'user_123',
            title: 'Location Share',
            body: 'Meeting point',
            channel: NotificationChannel::TELEGRAM,
            type: 'location'
        );

        $mediaManager->sendLocation(
            NotificationChannel::TELEGRAM,
            $locationNotification,
            latitude: 52.5200,  // Berlin
            longitude: 13.4050,
            title: 'Brandenburger Tor',
            address: '10117 Berlin, Germany'
        );

        echo "   ✅ Location sent successfully\n";
    } else {
        echo "   ❌ Location not supported\n";
    }
} catch (\Exception $e) {
    echo "   ⚠️  Error: {$e->getMessage()}\n";
}
echo "\n";

// Example 3: Send Document
echo "3️⃣  Sending document...\n";
try {
    if ($mediaManager->supportsDocument(NotificationChannel::TELEGRAM)) {
        $documentNotification = new Notification(
            userId: 'user_123',
            title: 'Document Share',
            body: 'Important document',
            channel: NotificationChannel::TELEGRAM,
            type: 'document'
        );

        // In production, use actual file path
        // $mediaManager->sendDocument(
        //     NotificationChannel::TELEGRAM,
        //     $documentNotification,
        //     documentPath: '/path/to/document.pdf',
        //     caption: '📄 Monthly Report',
        //     filename: 'report_2024.pdf'
        // );

        echo "   ℹ️  Document example (requires actual file path)\n";
    } else {
        echo "   ❌ Document not supported\n";
    }
} catch (\Exception $e) {
    echo "   ⚠️  Error: {$e->getMessage()}\n";
}
echo "\n";

// Example 4: Graceful fallback to text-only
echo "4️⃣  Demonstrating graceful fallback...\n";
try {
    $fallbackNotification = new Notification(
        userId: 'user_123',
        title: 'Fallback Test',
        body: 'This notification tries to send media, but falls back to text if unsupported',
        channel: NotificationChannel::TELEGRAM,
        type: 'fallback'
    );

    // Try to send with photo, fallback to text
    if ($mediaManager->supportsPhoto(NotificationChannel::TELEGRAM)) {
        echo "   Attempting to send with photo...\n";
        // $mediaManager->sendPhoto(...);
        echo "   ✅ Would send photo if file path provided\n";
    } else {
        echo "   Photo not supported, falling back to text notification...\n";
        $telegramChannel->send($fallbackNotification);
        echo "   ✅ Text notification sent as fallback\n";
    }
} catch (\Exception $e) {
    echo "   ⚠️  Error: {$e->getMessage()}\n";
    echo "   Falling back to text notification...\n";
    $telegramChannel->send($fallbackNotification);
    echo "   ✅ Fallback successful\n";
}
echo "\n";

// Example 5: Using MediaCapabilities for multi-media notifications
echo "5️⃣  Smart multi-media notification...\n";

$multiMediaNotification = new Notification(
    userId: 'user_123',
    title: 'Order Confirmed',
    body: 'Your order #12345 has been confirmed',
    channel: NotificationChannel::TELEGRAM,
    type: 'order_confirmed'
);

$capabilities = $mediaManager->getCapabilities(NotificationChannel::TELEGRAM);

if ($capabilities->supportsPhoto) {
    echo "   📸 Could attach product photo\n";
}

if ($capabilities->supportsDocument) {
    echo "   📄 Could attach order receipt PDF\n";
}

if ($capabilities->supportsLocation) {
    echo "   📍 Could share delivery location\n";
}

echo "   ✅ Multi-media notification planned\n\n";

// Summary
echo "✨ Summary\n";
echo "=========\n\n";

echo "MediaManager provides:\n";
echo "- Runtime capability checking before sending\n";
echo "- Type-safe media sending methods\n";
echo "- Graceful fallback support\n";
echo "- Unified API across all channels\n\n";

echo "Best Practices:\n";
echo "1. Always check capabilities before sending media\n";
echo "2. Provide fallback to text notifications\n";
echo "3. Handle exceptions gracefully\n";
echo "4. Use appropriate media types for context\n\n";

echo "✅ Example completed!\n";
