<?php

namespace common\logic\Feature\Feature;

use common\logic\AbstractBuilder;

class FeatureBuilder extends AbstractBuilder
{
    protected FeatureRepository $featureRepository;

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

    public function instanciateFeature(
        string $alias,
        string $name,
        string $description,
        bool $status = true,
        bool $isPaidFeature = false,
        float $price = null
    ): Feature
    {
        $feature = new Feature();
        $feature->alias = $alias;
        $feature->name = $name;
        $feature->description = $description;
        $feature->status = $status;
        $feature->is_paid_feature = $isPaidFeature;
        $feature->price = $price;

        return $feature;
    }

    public function createFeature(
        string $alias,
        string $name,
        string $description = '',
        bool $status = true,
        bool $isPaidFeature = false,
        float $price = null
    ): Feature
    {
        $feature = $this->featureRepository->findOneFeatureByAlias($alias);
        if(!$feature) {
            $feature = $this->instanciateFeature($alias, $name, $description, $status, $isPaidFeature, $price);
            $this->create($feature);
        }
        return $feature;
    }

    public function updateStatus(Feature $feature, bool $status)
    {
        $feature->status = $status;
        $this->update($feature);
    }
}