<?php
namespace WapplerSystems\Meilisearch\Report;

/***************************************************************
 *  Copyright notice
 *
 *  (c) 2010-2015 Ingo Renner <ingo@typo3.org>
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

use WapplerSystems\Meilisearch\ConnectionManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Reports\Status;

/**
 * Provides a status report about which meilisearchconfig version is used and checks
 * whether it fits the recommended version shipping with the extension.
 *
 * @author Ingo Renner <ingo@typo3.org>
 */
class MeilisearchConfigStatus extends AbstractMeilisearchStatus
{

    /**
     * The config name property is constructed as follows:
     *
     * tx_meilisearch    - The extension key
     * x-y-z    - The extension version this config is meant to work with
     * YYYYMMDD    - The date the config file was changed the last time
     *
     * Must be updated when changing the meilisearchconfig.
     *
     * @var string
     */
    const RECOMMENDED_SOLRCONFIG_VERSION = 'tx_meilisearch-11-0-0--20200415';

    /**
     * Compiles a collection of meilisearchconfig version checks against each configured
     * Meilisearch server. Only adds an entry if a meilisearchconfig other than the
     * recommended one was found.
     *
     */
    public function getStatus()
    {
        $reports = [];
        $meilisearchConnections = GeneralUtility::makeInstance(ConnectionManager::class)->getAllConnections();

        foreach ($meilisearchConnections as $meilisearchConnection) {
            $adminService = $meilisearchConnection->getAdminService();

            if ($adminService->ping() && $adminService->getMeilisearchconfigName() != self::RECOMMENDED_SOLRCONFIG_VERSION) {
                $variables = ['meilisearch' => $adminService, 'recommendedVersion' => self::RECOMMENDED_SOLRCONFIG_VERSION];
                $report = $this->getRenderedReport('MeilisearchConfigStatus.html', $variables);
                $status = GeneralUtility::makeInstance(
                    Status::class,
                    /** @scrutinizer ignore-type */ 'Meilisearchconfig Version',
                    /** @scrutinizer ignore-type */ 'Unsupported meilisearchconfig.xml',
                    /** @scrutinizer ignore-type */ $report,
                    /** @scrutinizer ignore-type */ Status::WARNING
                );

                $reports[] = $status;
            }
        }

        return $reports;
    }
}
