<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class ListUiSharedPartialsContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    /**
     * @return list<string>
     */
    private function listTabsTemplateFiles(): array
    {
        return [
            'pages/admin/users/index(default).phtml',
            'pages/admin/departments/index(default).phtml',
        ];
    }

    /**
     * @return list<string>
     */
    private function purgeTitlebarTemplateFiles(): array
    {
        return [
            'pages/admin/scheduled-jobs/index(default).phtml',
            'pages/admin/api-audit/index(default).phtml',
            'pages/admin/import-audit/index(default).phtml',
            'pages/admin/user-lifecycle-audit/index(default).phtml',
            'pages/admin/system-audit/index(default).phtml',
        ];
    }

    public function testTabsTemplatesUseSharedTabsPartial(): void
    {
        foreach ($this->listTabsTemplateFiles() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertStringContainsString("require templatePath('partials/app-list-tabs.phtml');", $content, $file);
            $this->assertSame(0, preg_match('/<div\s+class=\"app-list-tabs\"/', $content), $file);
        }
    }

    public function testPurgeTitlebarTemplatesUseSharedPurgePartial(): void
    {
        foreach ($this->purgeTitlebarTemplateFiles() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertStringContainsString("require templatePath('partials/app-list-purge-action.phtml');", $content, $file);
            $this->assertSame(0, preg_match('/<form\s+id=\"[^\"]*purge-form\"/', $content), $file);
            $this->assertStringNotContainsString('onclick="return confirm(\'', $content, $file);
        }
    }

    public function testTabsAndPurgePartialsExposeExpectedMarkupContract(): void
    {
        $tabsPartial = $this->readProjectFile('templates/partials/app-list-tabs.phtml');
        $this->assertStringContainsString('class="app-list-tabs"', $tabsPartial);
        $this->assertStringContainsString("class=\"<?php e(\$tabActive ? 'tab-link is-active' : 'tab-link'); ?>\"", $tabsPartial);

        $purgePartial = $this->readProjectFile('templates/partials/app-list-purge-action.phtml');
        $this->assertStringContainsString('method="post"', $purgePartial);
        $this->assertStringContainsString('Session::getCsrfInput();', $purgePartial);
        $this->assertStringContainsString('data-confirm-message="<?php e($listPurgeConfirmMessage); ?>"', $purgePartial);
        $this->assertStringNotContainsString('onclick="return confirm(\'', $purgePartial);
    }

    public function testAppInitIncludesGlobalConfirmActionComponent(): void
    {
        $content = $this->readProjectFile('web/js/app-init.js');
        $this->assertStringContainsString("import './components/app-confirm-actions.js';", $content);
    }
}
