<?php

namespace common\logic\User\UserProducer\Service;

use common\helpers\MeanPayment;
use common\logic\AbstractBuilder;
use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Repository\OrderRepository;
use common\logic\Order\Order\Service\OrderSolver;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\Producer\Producer\Repository\ProducerRepository;
use common\logic\User\CreditHistory\Model\CreditHistory;
use common\logic\User\CreditHistory\Service\CreditHistorySolver;
use common\logic\User\User\Model\User;
use common\logic\User\UserProducer\Model\UserProducer;
use common\logic\User\UserProducer\Repository\UserProducerRepository;

class UserProducerBuilder extends AbstractBuilder
{
    protected CreditHistorySolver $creditHistorySolver;
    protected UserProducerRepository $userProducerRepository;
    protected OrderRepository $orderRepository;
    protected ProducerRepository $producerRepository;
    protected OrderSolver $orderSolver;

    public function loadDependencies(): void
    {
        $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
        $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
        $this->orderRepository = $this->loadService(OrderRepository::class);
        $this->producerRepository = $this->loadService(ProducerRepository::class);
        $this->orderSolver = $this->loadService(OrderSolver::class);
    }

    public function instanciateUserProducer(User $user, Producer $producer, int $bookmark = 1)
    {
        $userProducer = new UserProducer();

        $userProducer->populateUser($user);
        $userProducer->populateproducer($producer);
        $userProducer->setCredit(0);
        $userProducer->setActive(1);
        $userProducer->setBookmark($bookmark);

        return $userProducer;
    }

    public function createUserProducer(User $user, Producer $producer, int $bookmark = 1): UserProducer
    {
        $userProducer = $this->instanciateUserProducer($user, $producer, $bookmark);
        $this->saveCreate($userProducer);
        return $userProducer;
    }

    public function createUserProducerIfNotExist(User $user, Producer $producer, int $bookmark = 1): UserProducer
    {
        return $this->userProducerRepository->findOneUserProducer($user, $producer)
            ?? $this->createUserProducer($user, $producer, $bookmark);
    }

    public function updateCredit(CreditHistory $creditHistory): void
    {
        $userProducer = $this->userProducerRepository->findOneUserProducer($creditHistory->user, $creditHistory->producer);

        if ($userProducer) {
            $oldCredit = $userProducer->getCredit();

            $this->deductCredit($userProducer, $creditHistory);
            $this->initMeanPaymentOrder($creditHistory);
            $this->sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit);
        }
    }

    public function deductCredit(UserProducer $userProducer, CreditHistory $creditHistory)
    {
        if ($this->creditHistorySolver->isTypeCredit($creditHistory)) {
            $userProducer->setCredit($userProducer->getCredit() + $creditHistory->getAmount());
        } elseif ($this->creditHistorySolver->isTypeDebit($creditHistory)) {
            $userProducer->setCredit($userProducer->getCredit() - $creditHistory->getAmount());
        }

        $this->saveUpdate($userProducer);
    }

    public function initMeanPaymentOrder($creditHistory)
    {
        if ($creditHistory->id_order && $creditHistory->id_order > 0) {
            $order = $this->orderRepository->findOneOrderById((int) $creditHistory->id_order);

            if ($order) {
                $paymentStatus = $this->orderSolver->getPaymentStatus($order);
                if ($paymentStatus == Order::PAYMENT_PAID
                    || $paymentStatus == Order::PAYMENT_SURPLUS) {

                    $order->mean_payment = MeanPayment::CREDIT;

                    $this->saveUpdate($order);
                }
            }
        }
    }

    public function sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit)
    {
        $userRepository = \Yii::$app->logic->getUserContainer()->getRepository();
        $producerRepository = \Yii::$app->logic->getProducerContainer()->getRepository();
        $newCredit = $userProducer->credit;

        if ($this->isCreditLimitCrossed($oldCredit, $newCredit)) {

            $user = $userRepository->findOneUserById($creditHistory->id_user);
            $producer = $producerRepository->findOneProducerById($creditHistory->id_producer);

            \Yii::$app->mailer->compose(
                [
                    'html' => 'creditLimitReminder-html',
                    'text' => 'creditLimitReminder-text'
                ],
                [
                    'user' => $user,
                    'producer' => $producer,
                    'credit' => $newCredit
                ]
            )
                ->setTo($user->email)
                ->setFrom(['contact@opendistrib.net' => 'Opendistrib'])
                ->setSubject('[Opendistrib] Seuil limite de crédit dépassé')
                ->send();
        }
    }

    public function isCreditLimitCrossed($oldCredit, $newCredit)
    {
        $creditLimitReminder = $this->producerRepository->getConfig('credit_limit_reminder');

        return !is_null($creditLimitReminder) &&
            $oldCredit > $creditLimitReminder
            && $newCredit <= $creditLimitReminder;
    }

    public function updateActive(User $user, Producer $producer, bool $active): void
    {
        $userProducer = $this->createUserProducerIfNotExist(
            $user,
            $producer
        );

        $userProducer->active = $active;

        $this->saveUpdate($userProducer);
    }
}