<?php

declare(strict_types=1);

namespace App\Framework\Notification\Media\Drivers;

use App\Framework\Notification\Channels\Telegram\TelegramClient;
use App\Framework\Notification\Channels\Telegram\UserChatIdResolver;
use App\Framework\Notification\Interfaces\{
    SupportsPhotoAttachments,
    SupportsVideoAttachments,
    SupportsAudioAttachments,
    SupportsDocumentAttachments,
    SupportsLocationSharing
};
use App\Framework\Notification\Media\MediaDriver;
use App\Framework\Notification\Notification;

/**
 * Telegram Media Driver
 *
 * Implements all media capabilities for Telegram
 */
final readonly class TelegramMediaDriver implements
    MediaDriver,
    SupportsPhotoAttachments,
    SupportsVideoAttachments,
    SupportsAudioAttachments,
    SupportsDocumentAttachments,
    SupportsLocationSharing
{
    public function __construct(
        private TelegramClient $client,
        private UserChatIdResolver $chatIdResolver
    ) {
    }

    public function getName(): string
    {
        return 'telegram';
    }

    /**
     * Send notification with photo attachment
     */
    public function sendWithPhoto(
        Notification $notification,
        string $photoPath,
        ?string $caption = null
    ): void {
        $chatId = $this->chatIdResolver->resolve($notification->getUserId());

        $this->client->sendPhoto(
            chatId: $chatId,
            photo: $photoPath,
            caption: $caption ?? $notification->getMessage()
        );
    }

    /**
     * Send notification with video attachment
     */
    public function sendWithVideo(
        Notification $notification,
        string $videoPath,
        ?string $caption = null,
        ?string $thumbnailPath = null
    ): void {
        $chatId = $this->chatIdResolver->resolve($notification->getUserId());

        $this->client->sendVideo(
            chatId: $chatId,
            video: $videoPath,
            caption: $caption ?? $notification->getMessage()
        );
    }

    /**
     * Send notification with audio attachment
     */
    public function sendWithAudio(
        Notification $notification,
        string $audioPath,
        ?string $caption = null,
        ?int $duration = null
    ): void {
        $chatId = $this->chatIdResolver->resolve($notification->getUserId());

        $this->client->sendAudio(
            chatId: $chatId,
            audio: $audioPath,
            caption: $caption ?? $notification->getMessage(),
            duration: $duration
        );
    }

    /**
     * Send notification with document attachment
     */
    public function sendWithDocument(
        Notification $notification,
        string $documentPath,
        ?string $caption = null,
        ?string $filename = null
    ): void {
        $chatId = $this->chatIdResolver->resolve($notification->getUserId());

        $this->client->sendDocument(
            chatId: $chatId,
            document: $documentPath,
            caption: $caption ?? $notification->getMessage(),
            filename: $filename
        );
    }

    /**
     * Send notification with location
     */
    public function sendWithLocation(
        Notification $notification,
        float $latitude,
        float $longitude,
        ?string $title = null,
        ?string $address = null
    ): void {
        $chatId = $this->chatIdResolver->resolve($notification->getUserId());

        // Send location
        $this->client->sendLocation(
            chatId: $chatId,
            latitude: $latitude,
            longitude: $longitude
        );

        // If title or address provided, send as separate message
        if ($title !== null || $address !== null) {
            $text = $notification->getMessage() . "\n\n";
            if ($title !== null) {
                $text .= "📍 {$title}\n";
            }
            if ($address !== null) {
                $text .= "📫 {$address}";
            }

            $this->client->sendMessage(
                chatId: $chatId,
                text: $text
            );
        }
    }
}
