<?php

namespace NoahVet\Reef\RabbitMQ\Producer\Factory;

use NoahVet\Reef\RabbitMQ\Producer\ReefMQProducer;
use NoahVet\Reef\RabbitMQ\Producer\ReefMQProducerInterface;
use NoahVet\Reef\RabbitMQ\RabbitMQConnectionFactoryInterface;
use Symfony\Component\Serializer\SerializerInterface;

class ReefMQProducerFactory implements ReefMQProducerFactoryInterface
{
    protected ?ReefMQProducerInterface $producer = null;

    public function __construct(
        private readonly RabbitMQConnectionFactoryInterface $rabbitMQConnectionFactory,
        private readonly string $reefAppName,
        private readonly SerializerInterface $serializer,
    ) {
    }

    #[\Override]
    public function createAppProducer(string $appName): ReefMQProducerInterface
    {
        return new ReefMQProducer(
            $this->rabbitMQConnectionFactory->get(),
            $appName,
            $this->serializer,
        );
    }

    #[\Override]
    public function getProducer(): ReefMQProducerInterface
    {
        if (!$this->producer) {
            $this->producer = $this->createAppProducer($this->reefAppName);
        }

        return $this->producer;
    }
}
