<?php

namespace domain\Producer\Producer;

use common\components\DolibarrApi;
use domain\Producer\ProducerPriceRange\ProducerPriceRangeRepository;
use domain\_\AbstractManager;
use yii\base\ErrorException;

class DolibarrProducerUtils extends AbstractManager
{
    protected DolibarrApi $dolibarrApi;
    protected ProducerPriceRangeRepository $producerPriceRangeRepository;
    protected ProducerRepository $producerRepository;

    public function loadDependencies(): void
    {
        $this->dolibarrApi = \Yii::$app->dolibarrApi;
        $this->producerPriceRangeRepository = $this->loadService(ProducerPriceRangeRepository::class);
        $this->producerRepository = $this->loadService(ProducerRepository::class);
    }

    public function getDolibarrProducerInvoices(Producer $producer): array
    {
        $invoicesArray = [];
        if($producer->dolibarr_socid) {
            $invoicesArray = $this->dolibarrApi->getInvoicesByThirParty($producer->dolibarr_socid);
        }

        return $invoicesArray;
    }

    public function getDolibarrProducerInvoicesUnpaid(Producer $producer): array
    {
        $unpaidInvoicesArray = [];
        $invoicesArray = $this->getDolibarrProducerInvoices($producer);

        foreach($invoicesArray as $invoice) {
            if(isset($invoice['remaintopay']) && $invoice['remaintopay'] > 0) {
                $unpaidInvoicesArray[] = $invoice;
            }
        }

        return $unpaidInvoicesArray;
    }

    public function countDolibarrProducerInvoicesUnpaid(Producer $producer): int
    {
        return count($this->getDolibarrProducerInvoicesUnpaid($producer));
    }

    public function getDolibarrInvoicesUnpaid()
    {
        $unpaidInvoicesArray = [];
        $producersArray = $this->producerRepository->findProducersActive();

        foreach($producersArray as $producer) {
            $unpaidInvoicesArray = array_merge(
                $unpaidInvoicesArray,
                $this->getDolibarrProducerInvoicesUnpaid($producer)
            );
        }

        //die('test : '.count($unpaidInvoicesArray));

        return $unpaidInvoicesArray;
    }

    public function getAmountProducerInvoicesUnpaid()
    {
        $amount = 0;
        $unpaidInvoicesArray = $this->getDolibarrInvoicesUnpaid();

        foreach($unpaidInvoicesArray as $unpaidInvoice) {
            if(isset($unpaidInvoice['remaintopay'])) {
                $amount += $unpaidInvoice['remaintopay'];
            }
        }

        return $amount;
    }

    public function generateDolibarrProducerInvoice(Producer $producer)
    {
        $idProduct = $this->getDolibarrProductId($producer);
        if(!$idProduct) {
            throw new ErrorException("Dolibarr : pas d'id product trouvé pour le producteur ".$producer->name);
        }

        $idInvoice = $this->dolibarrApi->createInvoice($producer->dolibarr_socid);
        $this->dolibarrApi->addInvoiceLine($idInvoice, $idProduct);
        $responseValidate = $this->dolibarrApi->validateInvoice($idInvoice);
        $this->dolibarrApi->generateInvoicePdf($responseValidate['ref']);
    }

    private function getDolibarrProductId(Producer $producer)
    {
        if($producer->dolibarr_product_id) {
            $idProduct = $producer->dolibarr_product_id;
        }
        else {
            $producerPriceRange = $this->producerPriceRangeRepository->getProducerPriceRangeByTurnover($this->producerRepository->getTurnoverLastMonth($producer));
            if($producerPriceRange && $producerPriceRange->dolibarr_product_id) {
                $idProduct = $producerPriceRange->dolibarr_product_id;
            }
        }

        return $idProduct;
    }
}