<?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(
        protected readonly RabbitMQConnectionFactoryInterface $rabbitMQConnectionFactory,
        protected readonly string $reefAppName,
        protected 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;
    }
}
