<?php

namespace domain\Feature\FeatureProducer;

use domain\Feature\Feature\Feature;
use domain\_\AbstractBuilder;

class FeatureProducerBuilder extends AbstractBuilder
{
    protected FeatureProducerRepository $featureProducerRepository;

    public function loadDependencies(): void
    {
        $this->featureProducerRepository = $this->loadService(FeatureProducerRepository::class);
    }

    public function instanciateFeatureProducer(
        Feature $feature,
        bool $status = null
    ): FeatureProducer
    {
        $featureProducer = new FeatureProducer();
        $featureProducer->populateFeature($feature);
        $featureProducer->populateProducer($this->getProducerContext());
        $featureProducer->status = $status;

        return $featureProducer;
    }

    public function createFeatureProducer(
        Feature $feature,
        bool $status = null
    ): FeatureProducer
    {
        $featureProducer = $this->featureProducerRepository->findOneFeatureProducer($feature);
        if(!$featureProducer) {
            $featureProducer = $this->instanciateFeatureProducer($feature, $status);
            $this->create($featureProducer);
        }

        return $featureProducer;
    }

    public function updateStatus(FeatureProducer $featureProducer, bool $status = null)
    {
        $featureProducer->status = $status;
        $this->update($featureProducer);
    }

    public function updateStatusByFeature(Feature $feature, bool $status = null)
    {
        $featureProducer = $this->createFeatureProducer($feature, $status);
        $this->updateStatus($featureProducer, $status);
    }
}