<?php

namespace common\logic\Document\Document\Service;

use common\logic\AbstractBuilder;
use common\logic\Document\Document\Model\Document;
use common\logic\Document\Document\Model\DocumentInterface;
use common\logic\Document\Document\Repository\DocumentRepository;
use common\logic\Producer\Producer\Service\ProducerSolver;

class DocumentBuilder extends AbstractBuilder
{
    protected DocumentSolver $documentSolver;
    protected DocumentRepository $documentRepository;
    protected ProducerSolver $producerSolver;

    public function loadDependencies(): void
    {
        $this->documentSolver = $this->loadService(DocumentSolver::class);
        $this->documentRepository = $this->loadService(DocumentRepository::class);
        $this->producerSolver = $this->loadService(ProducerSolver::class);
    }

    public function initDocumentProducer(Document $document): void
    {
        $document->id_producer = $this->getProducerContextId();
    }

    public function initTaxCalculationMethod(DocumentInterface $document): void
    {
        $producerTaxCalculationMethod = $this->producerSolver->getConfig('option_tax_calculation_method');

        if ($producerTaxCalculationMethod) {
            $document->tax_calculation_method = $producerTaxCalculationMethod;
        } else {
            $document->tax_calculation_method = Document::TAX_CALCULATION_METHOD_DEFAULT;
        }
    }

    public function updateDocumentIsSend(Document $document, bool $isSent)
    {
        $document->is_sent = $isSent;
        $this->update($document);
    }
}