<?php

namespace common\logic\Distribution\ProductDistribution\Service;

use common\logic\AbstractBuilder;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\Distribution\Service\DistributionSolver;
use common\logic\Distribution\ProductDistribution\Model\ProductDistribution;
use common\logic\Distribution\ProductDistribution\Repository\ProductDistributionRepository;
use common\logic\Product\Product\Model\Product;
use common\logic\Product\Product\Service\ProductSolver;

class ProductDistributionBuilder extends AbstractBuilder
{
    protected ProductDistributionRepository $productDistributionRepository;
    protected DistributionSolver $distributionSolver;
    protected ProductSolver $productSolver;

    public function loadDependencies(): void
    {
        $this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class);
        $this->distributionSolver = $this->loadService(DistributionSolver::class);
        $this->productSolver = $this->loadService(ProductSolver::class);
    }

    public function instanciateProductDistribution(Distribution $distribution, Product $product): ProductDistribution
    {
        $productDistribution = new ProductDistribution();
        $productDistribution->populateFieldObject('id_distribution', 'distribution', $distribution);
        $productDistribution->populateFieldObject('id_product', 'product', $product);

        return $productDistribution;
    }

    public function createProductDistribution(Distribution $distribution, Product $product): ProductDistribution
    {
        $productDistribution = $this->instanciateProductDistribution($distribution, $product);

        $this->initActive($productDistribution);
        $this->initQuantityMax($productDistribution);

        $this->saveCreate($productDistribution);

        return $productDistribution;
    }

    public function createProductDistributionIfNotExist(Distribution $distribution, Product $product): ProductDistribution
    {
        return $this->productDistributionRepository->findOneProductDistribution($distribution, $product)
            ?? $this->createProductDistribution($distribution, $product);
    }

    public function initActive(ProductDistribution $productDistribution): ProductDistribution
    {
        $productDistribution->active = (int) $this->productSolver->isProductActiveByDay(
            $productDistribution->product,
            $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution)
        );

        return $productDistribution;
    }

    public function initQuantityMax(ProductDistribution $productDistribution): ProductDistribution
    {
        $productDistribution->quantity_max = $this->productSolver->getProductQuantityMaxByDay(
            $productDistribution->product,
            $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution)
        );

        return $productDistribution;
    }

}