<?php

namespace FiloBlu\ActiveCampaign\Plugin\ConsentsManager;

use FiloBlu\ActiveCampaign\Handler\ConsentsSubscriptionHandler;
use FiloBlu\ConsentsManager\Api\Data\ConsentSubscriptionInterface;
use FiloBlu\ConsentsManager\Model\ConsentManagement;
use FiloBlu\ActiveCampaign\Helper\ConsentsProfilingHelper;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Math\Random;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory;
use Magento\Newsletter\Model\Subscriber;
use Magento\Newsletter\Model\SubscriberFactory;
use Psr\Log\LoggerInterface;

class SendSubscriptionDataIfProfileEnabled
{
    /**
     * @var ConsentsProfilingHelper
     */
    protected $consentsProfilingHelper;
    /**
     * @var CollectionFactory
     */
    private $subscriberCollectionFactory;
    /**
     * @var SubscriberFactory
     */
    private $subscriberFactory;
    /**
     * @var Random
     */
    private $random;
    /**
     * @var Request
     */
    private $webapiRequest;
    /**
     * @var LoggerInterface
     */
    private $logger;
    /**
     * @var \FiloBlu\ActiveCampaign\Model\Entity\Export\Subscriber
     */
    private $subscriberExportModel;
    /**
     * @var ConsentsSubscriptionHandler
     */
    private $consentsSubscriptionHandler;

    /**
     * @param ConsentsProfilingHelper $consentsProfilingHelper
     * @param CollectionFactory $subscriberCollectionFactory
     * @param SubscriberFactory $subscriberFactory
     * @param Random $random
     * @param LoggerInterface $logger
     * @param ConsentsSubscriptionHandler $consentsSubscriptionHandler
     */
    public function __construct(
        ConsentsProfilingHelper $consentsProfilingHelper,
        CollectionFactory $subscriberCollectionFactory,
        SubscriberFactory $subscriberFactory,
        Random $random,
        LoggerInterface $logger,
        ConsentsSubscriptionHandler $consentsSubscriptionHandler
    )
    {
        $this->consentsProfilingHelper = $consentsProfilingHelper;
        $this->subscriberCollectionFactory = $subscriberCollectionFactory;
        $this->subscriberFactory = $subscriberFactory;
        $this->random = $random;
        $this->logger = $logger;
        $this->consentsSubscriptionHandler = $consentsSubscriptionHandler;
    }

    /**
     * @param ConsentManagement $subject
     * @param ConsentSubscriptionInterface $result
     * @param Http $request
     * @param bool $subscribe
     * @param string $email
     * @param int $typeId
     * @return ConsentSubscriptionInterface
     * @throws LocalizedException
     */
    public function afterSetSubscription(
        ConsentManagement $subject,
        ConsentSubscriptionInterface $result,
        bool $subscribe,
        string $email,
        int $typeId
    ) {

        if (!$subscribe) {
            return $result;
        }

        if (!$this->consentsProfilingHelper->isConsentsManagerEnabled()) {
            return $result;
        }

        if (!$this->consentsProfilingHelper->isMarketingConsentsEnabled()) {
            return $result;
        }

        $this->consentsSubscriptionHandler->sendSubscriberData($email, $typeId, $result->getStoreId());

        return $result;
    }
}
