<?php

namespace Openguru\OpenCartFramework\TeleCartPulse;

use Openguru\OpenCartFramework\Cache\CacheInterface;
use Openguru\OpenCartFramework\Container\Container;
use Openguru\OpenCartFramework\Container\ServiceProvider;
use Openguru\OpenCartFramework\Telegram\TelegramInitDataDecoder;
use Openguru\OpenCartFramework\Telegram\TelegramService;
use Psr\Log\LoggerInterface;

class TeleCartPulseServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->container->singleton(PayloadSigner::class, function (Container $app) {
            return new PayloadSigner(
                $app->getConfigValue('pulse.api_key'),
            );
        });

        $this->container->singleton(TeleCartPulseService::class, function (Container $app) {
            $heartbeatSecret = $app->getConfigValue('pulse.heartbeat_secret') ?? env('PULSE_HEARTBEAT_SECRET');
            $heartbeatSigner = $heartbeatSecret ? new PayloadSigner($heartbeatSecret) : null;

            return new TeleCartPulseService(
                $app->get(TelegramInitDataDecoder::class),
                $app->get(TelegramService::class),
                $app->get(CacheInterface::class),
                $app->get(LoggerInterface::class),
                $app->get(TeleCartEvent::class),
                $app->getConfigValue('pulse.api_key'),
                $heartbeatSigner,
            );
        });

        $this->container->singleton(TeleCartPulseEventsSender::class, function (Container $app) {
            return new TeleCartPulseEventsSender(
                $app->get(PayloadSigner::class),
                $app->get(LoggerInterface::class),
                $app->getConfigValue('pulse.api_key'),
            );
        });
    }
}
