<?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\Media\MediaManager;
use App\Framework\Notification\ValueObjects\NotificationChannel;
use App\Framework\Notification\Notification;

/**
 * Rich Media Notification Example
 *
 * Demonstrates the MediaManager system for sending notifications with:
 * - Photos
 * - Videos
 * - Audio files
 * - Documents
 * - Location data
 */

echo "🎨 Rich Media Notification System Example\n";
echo "==========================================\n\n";

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

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

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

echo "📋 Testing MediaManager Capabilities\n";
echo "------------------------------------\n\n";

// 1. Check Telegram capabilities
echo "1️⃣  Checking Telegram channel capabilities...\n";

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

echo "   Supported media types:\n";
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";
echo "   - Voice: " . ($capabilities->supportsVoice ? '✅' : '❌') . "\n\n";

// 2. Test photo support
echo "2️⃣  Testing photo support...\n";

if ($mediaManager->supportsPhoto(NotificationChannel::TELEGRAM)) {
    echo "   ✅ Telegram supports photos\n";

    // Example: Send photo with caption
    try {
        $photoNotification = new Notification(
            userId: 'user_123',
            title: 'Photo Notification',
            body: 'Check out this image!',
            channel: NotificationChannel::TELEGRAM,
            type: 'photo_test'
        );

        // Note: In real usage, you would provide a valid file path or Telegram file_id
        // $mediaManager->sendPhoto(
        //     NotificationChannel::TELEGRAM,
        //     $photoNotification,
        //     photoPath: '/path/to/image.jpg',
        //     caption: 'Beautiful landscape photo'
        // );

        echo "   📸 Photo sending method available\n";
    } catch (\Exception $e) {
        echo "   ⚠️  Photo test skipped: {$e->getMessage()}\n";
    }
} else {
    echo "   ❌ Telegram does not support photos\n";
}
echo "\n";

// 3. Test video support
echo "3️⃣  Testing video support...\n";

if ($mediaManager->supportsVideo(NotificationChannel::TELEGRAM)) {
    echo "   ✅ Telegram supports videos\n";

    // Example: Send video with thumbnail
    try {
        $videoNotification = new Notification(
            userId: 'user_123',
            title: 'Video Notification',
            body: 'Watch this video!',
            channel: NotificationChannel::TELEGRAM,
            type: 'video_test'
        );

        // Note: In real usage, you would provide valid file paths
        // $mediaManager->sendVideo(
        //     NotificationChannel::TELEGRAM,
        //     $videoNotification,
        //     videoPath: '/path/to/video.mp4',
        //     caption: 'Tutorial video',
        //     thumbnailPath: '/path/to/thumbnail.jpg'
        // );

        echo "   🎥 Video sending method available\n";
    } catch (\Exception $e) {
        echo "   ⚠️  Video test skipped: {$e->getMessage()}\n";
    }
} else {
    echo "   ❌ Telegram does not support videos\n";
}
echo "\n";

// 4. Test audio support
echo "4️⃣  Testing audio support...\n";

if ($mediaManager->supportsAudio(NotificationChannel::TELEGRAM)) {
    echo "   ✅ Telegram supports audio\n";

    // Example: Send audio file
    try {
        $audioNotification = new Notification(
            userId: 'user_123',
            title: 'Audio Notification',
            body: 'Listen to this audio!',
            channel: NotificationChannel::TELEGRAM,
            type: 'audio_test'
        );

        // Note: In real usage, you would provide a valid audio file
        // $mediaManager->sendAudio(
        //     NotificationChannel::TELEGRAM,
        //     $audioNotification,
        //     audioPath: '/path/to/audio.mp3',
        //     caption: 'Podcast episode',
        //     duration: 300 // 5 minutes
        // );

        echo "   🎵 Audio sending method available\n";
    } catch (\Exception $e) {
        echo "   ⚠️  Audio test skipped: {$e->getMessage()}\n";
    }
} else {
    echo "   ❌ Telegram does not support audio\n";
}
echo "\n";

// 5. Test document support
echo "5️⃣  Testing document support...\n";

if ($mediaManager->supportsDocument(NotificationChannel::TELEGRAM)) {
    echo "   ✅ Telegram supports documents\n";

    // Example: Send document
    try {
        $documentNotification = new Notification(
            userId: 'user_123',
            title: 'Document Notification',
            body: 'Here is your document!',
            channel: NotificationChannel::TELEGRAM,
            type: 'document_test'
        );

        // Note: In real usage, you would provide a valid document
        // $mediaManager->sendDocument(
        //     NotificationChannel::TELEGRAM,
        //     $documentNotification,
        //     documentPath: '/path/to/document.pdf',
        //     caption: 'Monthly report',
        //     filename: 'Report_2024.pdf'
        // );

        echo "   📄 Document sending method available\n";
    } catch (\Exception $e) {
        echo "   ⚠️  Document test skipped: {$e->getMessage()}\n";
    }
} else {
    echo "   ❌ Telegram does not support documents\n";
}
echo "\n";

// 6. Test location support
echo "6️⃣  Testing location support...\n";

if ($mediaManager->supportsLocation(NotificationChannel::TELEGRAM)) {
    echo "   ✅ Telegram supports location sharing\n";

    // Example: Send location
    try {
        $locationNotification = new Notification(
            userId: 'user_123',
            title: 'Location Notification',
            body: 'Meet me here!',
            channel: NotificationChannel::TELEGRAM,
            type: 'location_test'
        );

        // Note: In real usage, you would provide actual coordinates
        // $mediaManager->sendLocation(
        //     NotificationChannel::TELEGRAM,
        //     $locationNotification,
        //     latitude: 52.5200,  // Berlin
        //     longitude: 13.4050,
        //     title: 'Meeting Point',
        //     address: 'Brandenburger Tor, Berlin'
        // );

        echo "   📍 Location sending method available\n";
    } catch (\Exception $e) {
        echo "   ⚠️  Location test skipped: {$e->getMessage()}\n";
    }
} else {
    echo "   ❌ Telegram does not support location sharing\n";
}
echo "\n";

// 7. Test error handling for unsupported channel
echo "7️⃣  Testing error handling for unsupported channel...\n";

try {
    // Try to check capabilities for a channel without registered driver
    $emailCapabilities = $mediaManager->getCapabilities(NotificationChannel::EMAIL);

    if (!$emailCapabilities->hasAnyMediaSupport()) {
        echo "   ✅ Email channel has no media support (as expected)\n";
    }
} catch (\Exception $e) {
    echo "   ⚠️  Expected behavior: {$e->getMessage()}\n";
}
echo "\n";

// 8. Demonstrate runtime capability checking
echo "8️⃣  Runtime capability checking pattern...\n";

$testChannel = NotificationChannel::TELEGRAM;

echo "   Example: Sending media with runtime checks\n";
echo "   \n";
echo "   if (\$mediaManager->supportsPhoto(\$channel)) {\n";
echo "       \$mediaManager->sendPhoto(\$channel, \$notification, \$photoPath);\n";
echo "   } else {\n";
echo "       // Fallback to text-only notification\n";
echo "       \$channel->send(\$notification);\n";
echo "   }\n";
echo "\n";

// Summary
echo "✅ Rich Media System Summary\n";
echo "============================\n\n";

echo "Architecture:\n";
echo "- MediaManager: Central management with driver registration\n";
echo "- MediaDriver: Marker interface with atomic capability interfaces\n";
echo "- Atomic Interfaces: SupportsPhotoAttachments, SupportsVideoAttachments, etc.\n";
echo "- TelegramMediaDriver: Full media support implementation\n\n";

echo "Key Features:\n";
echo "- ✅ Runtime capability detection via instanceof\n";
echo "- ✅ Type-safe media sending with validation\n";
echo "- ✅ Optional media support per channel\n";
echo "- ✅ Public MediaManager property on channels\n";
echo "- ✅ Graceful degradation for unsupported features\n\n";

echo "Usage:\n";
echo "1. Access MediaManager via channel: \$channel->mediaManager\n";
echo "2. Check capabilities before sending: \$mediaManager->supportsPhoto(\$channel)\n";
echo "3. Send media with validation: \$mediaManager->sendPhoto(...)\n";
echo "4. Handle unsupported media gracefully with fallbacks\n\n";

echo "✨ Example completed successfully!\n";
