<?php
/**
 * For the full copyright and license information, please view the
 * docs/licenses/LICENSE.txt file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Tests\Integration\PrestaShopBundle\Controller\Admin\Improve;

use Context;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShop\PrestaShop\Core\Module\ModuleCollection;
use PrestaShop\PrestaShop\Core\Module\ModuleRepository;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Translation\Translator;
use Tests\Integration\Utility\LoginTrait;
use Tests\Unit\Core\Configuration\MockConfigurationTrait;

class ModuleControllerTest extends WebTestCase
{
    use MockConfigurationTrait;
    use LoginTrait;

    /**
     * @var KernelBrowser
     */
    protected $client;
    /**
     * @var Router
     */
    protected $router;
    /**
     * @var Translator
     */
    protected $translator;
    /**
     * @var LegacyContext
     */
    protected $context;

    protected function setUp(): void
    {
        parent::setUp();

        $this->client = self::createClient();
        $this->loginUser($this->client);
        $this->router = self::$kernel->getContainer()->get('router');
        $this->context = self::$kernel->getContainer()->get('prestashop.adapter.legacy.context');

        Context::setInstanceForTesting($this->context->getContext());

        // Enable debug mode
        $configurationMock = $this->mockConfiguration([
            '_PS_MODE_DEMO_' => true,
            '_PS_ROOT_DIR_' => _PS_ROOT_DIR_,
            '_PS_MODULE_DIR_' => _PS_ROOT_DIR_ . '/tests/Resources/modules/',
            '_PS_ALL_THEMES_DIR_' => dirname(__DIR__, 6) . '/themes/',
            'PS_SHOP_DEFAULT' => '1',
            'PS_COOKIE_CHECKIP' => '1',
            'PS_LANG_DEFAULT' => '1',
            'PS_SSL_ENABLED' => '0',
            'PS_CURRENCY_DEFAULT' => '1',
            'PS_COUNTRY_DEFAULT' => '1',
        ], null, Configuration::class);

        self::$kernel->getContainer()->set('prestashop.adapter.legacy.configuration', $configurationMock);
        self::$kernel->getContainer()->set(ConfigurationInterface::class, $configurationMock);
        self::$kernel->getContainer()->set(Configuration::class, $configurationMock);

        $moduleRepository = $this->createMock(ModuleRepository::class);
        $moduleRepository->method('getList')->willReturn(new ModuleCollection());
        self::$kernel->getContainer()->set(ModuleRepository::class, $moduleRepository);
    }

    public function testModuleAction(): void
    {
        $moduleName = 'test-module';

        $installModuleRoute = $this->router->generate('admin_module_manage_action', [
            'action' => 'install',
            'module_name' => $moduleName,
        ]);
        $this->client->request('POST', $installModuleRoute);

        $response = $this->client->getResponse();
        $responseContent = $response->getContent();

        $decodedContent = json_decode($responseContent, true);

        $this->assertArrayHasKey($moduleName, $decodedContent);

        $this->assertArrayHasKey('status', $decodedContent[$moduleName]);
        $this->assertFalse($decodedContent[$moduleName]['status']);

        $this->assertArrayHasKey('msg', $decodedContent[$moduleName]);

        $this->assertEquals('This functionality has been disabled.', $decodedContent[$moduleName]['msg']);
    }

    public function testImportModuleAction(): void
    {
        $importModuleRoute = $this->router->generate('admin_module_import');
        $this->client->request('POST', $importModuleRoute);

        $response = $this->client->getResponse();
        $responseContent = $response->getContent();

        $decodedContent = json_decode($responseContent, true);

        $this->assertArrayHasKey('msg', $decodedContent);
        $this->assertEquals('This functionality has been disabled.', $decodedContent['msg']);
    }
}
