<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Faqs
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     http://mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\Faqs\Plugin;

use Magento\Framework\App\Request\Http;
use Magento\Framework\View\Page\Config\Renderer;
use Mageplaza\Faqs\Helper\Data;

/**
 * Class FaqSchemaMarkup
 *
 * @package Mageplaza\Faqs\Plugin
 */
class FaqSchemaMarkup
{
    /**
     * @var Data
     */
    protected $helperData;

    /**
     * @var Http
     */
    protected $request;

    /**
     * @param Data $helperData
     * @param Http $request
     */
    public function __construct(
        Data $helperData,
        Http $request
    ) {
        $this->helperData = $helperData;
        $this->request    = $request;
    }

    /**
     * @param Renderer $subject
     * @param $result
     *
     * @return mixed
     */
    public function afterRenderHeadContent(Renderer $subject, $result)
    {
        $storeId     = $this->helperData->getStoreId();
        $listArticle = $this->helperData->getArticleCollection($storeId);
        if ($this->helperData->isEnabled($storeId) && $this->helperData->getSchemaConfig('enabled')) {
            $fullActionName = $this->request->getFullActionName();
            switch ($fullActionName) {
                case 'catalog_product_view':
                    $productId = $this->request->getParam('id');
                    $listArticle->getSelect()
                        ->join(
                            [
                                'mfap' => $listArticle->getTable('mageplaza_faqs_article_product')
                            ],
                            'mfap.article_id = main_table.article_id AND mfap.entity_id = ' . $productId
                        );
                    $faqSchemaMarkup = $this->addFaqsSchema($listArticle);
                    $result          .= $faqSchemaMarkup;
                    break;
                case 'mpfaqs_article_index':
                    $faqSchemaMarkup = $this->addFaqsSchema($listArticle);
                    $result          .= $faqSchemaMarkup;
                    break;
            }
        }

        return $result;
    }

    /**
     * create FAQ Schema Markup
     *
     * @param $listArticle
     *
     * @return string
     */
    public function addFaqsSchema($listArticle)
    {
        $faqSchema    = [
            '@context'   => 'https://schema.org/',
            '@type'      => 'FAQPage',
            'mainEntity' => []
        ];
        $maxQuestions = $this->helperData->getSchemaConfig('number_question') ?? 5;
        $listArticle->setOrder('views', 'desc')->setPageSize($maxQuestions)->setCurPage(1);

        foreach ($listArticle as $article) {
            $faqSchema['mainEntity'][] = [
                '@type'          => 'Question',
                'name'           => $article->getName(),
                'acceptedAnswer' => [
                    '@type' => 'Answer',
                    'text'  => $article->getArticleContent()
                ]
            ];
        }

        return $this->helperData->createFaqSchema(
            $faqSchema,
            '<!-- Faqs Schema by Mageplaza Faqs -->'
        );
    }
}
