<?php

declare(strict_types=1);

namespace App\Framework\Notification\Channels\Telegram;

use App\Framework\Attributes\Initializer;
use App\Framework\DI\Container;
use App\Framework\HttpClient\HttpClient;
use App\Framework\Notification\Channels\TelegramChannel;
use App\Framework\Notification\Channels\Telegram\Webhook\{CallbackRouter, TelegramWebhookEventHandler};
use App\Framework\Notification\Channels\Telegram\Webhook\Examples\{ApproveOrderHandler, RejectOrderHandler};
use App\Framework\Notification\Media\{MediaManager, Drivers\TelegramMediaDriver};
use App\Framework\Notification\ValueObjects\NotificationChannel;
use App\Framework\Logging\Logger;

/**
 * Telegram Notification Channel Initializer
 *
 * Registers Telegram notification components in the DI container
 */
final readonly class TelegramNotificationInitializer
{
    public function __construct(
        private Container $container
    ) {
    }

    #[Initializer]
    public function initialize(): void
    {
        // Register Telegram Config
        $this->container->singleton(
            TelegramConfig::class,
            fn () => TelegramConfig::createDefault()
        );

        // Register Chat ID Resolver
        $this->container->singleton(
            UserChatIdResolver::class,
            fn () => FixedChatIdResolver::createDefault()
        );

        // Register Telegram Client
        $this->container->singleton(
            TelegramClient::class,
            fn (Container $c) => new TelegramClient(
                httpClient: $c->get(HttpClient::class),
                config: $c->get(TelegramConfig::class)
            )
        );

        // Register MediaManager (needs to be registered before TelegramChannel)
        $this->container->singleton(
            MediaManager::class,
            function (Container $c) {
                $mediaManager = new MediaManager();

                // Register TelegramMediaDriver for Telegram channel
                $telegramDriver = new TelegramMediaDriver(
                    client: $c->get(TelegramClient::class),
                    chatIdResolver: $c->get(UserChatIdResolver::class)
                );

                $mediaManager->registerDriver(
                    NotificationChannel::TELEGRAM,
                    $telegramDriver
                );

                return $mediaManager;
            }
        );

        // Register Telegram Channel
        $this->container->singleton(
            TelegramChannel::class,
            fn (Container $c) => new TelegramChannel(
                client: $c->get(TelegramClient::class),
                chatIdResolver: $c->get(UserChatIdResolver::class),
                mediaManager: $c->get(MediaManager::class)
            )
        );

        // Register Callback Router with example handlers
        $this->container->singleton(
            CallbackRouter::class,
            function () {
                $router = new CallbackRouter();

                // Register example handlers
                $router->register(new ApproveOrderHandler());
                $router->register(new RejectOrderHandler());

                // TODO: Register your custom handlers here
                // $router->register(new YourCustomHandler());

                return $router;
            }
        );

        // Register Webhook Event Handler
        $this->container->singleton(
            TelegramWebhookEventHandler::class,
            fn (Container $c) => new TelegramWebhookEventHandler(
                telegramClient: $c->get(TelegramClient::class),
                callbackRouter: $c->get(CallbackRouter::class),
                logger: $c->get(Logger::class)
            )
        );
    }
}
