<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class FrontendRuntimeEntrypointContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    public function testEntrypointsUseRuntimeRegistrationWithoutSideeffectComponentImports(): void
    {
        $defaultEntry = $this->readProjectFile('web/js/app-init.js');
        $loginEntry = $this->readProjectFile('web/js/app-login-init.js');

        $this->assertStringContainsString("import { createComponentRuntime } from './core/app-component-runtime.js';", $defaultEntry);
        $this->assertStringContainsString("import { createComponentRuntime } from './core/app-component-runtime.js';", $loginEntry);
        $this->assertDoesNotMatchRegularExpression('/import\s+[\'"]\.\/components\//', $defaultEntry);
        $this->assertDoesNotMatchRegularExpression('/import\s+[\'"]\.\/components\//', $loginEntry);

        $this->assertStringContainsString("runtime.register('sidebar-toggle', initSidebarToggle", $defaultEntry);
        $this->assertStringContainsString("runtime.register('global-search', initGlobalSearch", $defaultEntry);
        $this->assertStringContainsString("runtime.register('token-select', initTokenSelect", $defaultEntry);
        $this->assertStringContainsString("await mountModuleComponentsByPhase('late');", $defaultEntry);
        $this->assertStringContainsString('moduleRuntimeComponents', $defaultEntry);
        $this->assertStringContainsString("selector: '[data-app-component=\"tabs\"]'", $defaultEntry);
        $this->assertStringContainsString('runtime.mountAll();', $defaultEntry);

        $this->assertStringContainsString("runtime.register('password-hints', initPasswordHints", $loginEntry);
        $this->assertStringContainsString("runtime.register('password-toggle', initPasswordToggles", $loginEntry);
        $this->assertStringContainsString("runtime.register('login-submit-lock', initLoginSubmitLock", $loginEntry);
        $this->assertStringContainsString('runtime.mountAll();', $loginEntry);
    }

    public function testBookmarkLegacyEntrypointFileIsRemoved(): void
    {
        $this->assertFileDoesNotExist($this->projectRootPath() . '/web/js/components/app-bookmark-init.js');
    }

    public function testPageEditorUsesPageModuleEntrypointWithPageConfig(): void
    {
        $pageEditor = $this->readProjectFile('web/js/components/app-page-editor.js');
        $pageEditorEntry = $this->readProjectFile('web/js/pages/app-page-editor-entry.js');
        $adminPage = $this->readProjectFile('pages/page/index(default).phtml');
        $publicPage = $this->readProjectFile('pages/public-page/index(page).phtml');

        $this->assertStringContainsString('export function initPageEditor(root = document, config = {})', $pageEditor);
        $this->assertStringNotContainsString('const form = document.querySelector', $pageEditor);
        $this->assertStringContainsString("readPageConfig('page-editor')", $pageEditorEntry);
        $this->assertStringContainsString("assetVersion('js/pages/app-page-editor-entry.js')", $adminPage);
        $this->assertStringContainsString("assetVersion('js/pages/app-page-editor-entry.js')", $publicPage);
        $this->assertStringNotContainsString("assetVersion('js/components/app-page-editor.js')", $adminPage);
        $this->assertStringNotContainsString("assetVersion('js/components/app-page-editor.js')", $publicPage);
        $this->assertStringContainsString('id="page-config-page-editor"', $adminPage);
        $this->assertStringContainsString('id="page-config-page-editor"', $publicPage);
    }
}
