<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class DetailValidationSummaryContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    /**
     * @return list<string>
     */
    private function detailTemplateFiles(): array
    {
        return [
            'pages/admin/users/create(default).phtml',
            'pages/admin/users/edit(default).phtml',
            'pages/admin/tenants/create(default).phtml',
            'pages/admin/tenants/edit(default).phtml',
            'pages/admin/departments/create(default).phtml',
            'pages/admin/departments/edit(default).phtml',
            'pages/admin/roles/create(default).phtml',
            'pages/admin/roles/edit(default).phtml',
            'pages/admin/permissions/create(default).phtml',
            'pages/admin/permissions/edit(default).phtml',
            'pages/admin/scheduled-jobs/edit(default).phtml',
        ];
    }

    /**
     * @return list<string>
     */
    private function detailActionFiles(): array
    {
        return [
            'pages/admin/users/create().php',
            'pages/admin/users/edit($id).php',
            'pages/admin/tenants/create().php',
            'pages/admin/tenants/edit($id).php',
            'pages/admin/departments/create().php',
            'pages/admin/departments/edit($id).php',
            'pages/admin/roles/create().php',
            'pages/admin/roles/edit($id).php',
            'pages/admin/permissions/create().php',
            'pages/admin/permissions/edit($id).php',
            'pages/admin/scheduled-jobs/edit($id).php',
        ];
    }

    public function testScopedDetailTemplatesUseSharedValidationSummaryPartial(): void
    {
        foreach ($this->detailTemplateFiles() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertStringContainsString("require templatePath('partials/app-details-validation-summary.phtml');", $content, $file);
        }
    }

    public function testScopedDetailTemplatesDoNotInlineLegacyErrorLoop(): void
    {
        foreach ($this->detailTemplateFiles() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertSame(0, preg_match('/foreach\s*\(\$errors\s+as\s+\$error\)/', $content), $file);
        }
    }

    public function testSharedPartialExposesValidationSummaryContract(): void
    {
        $content = $this->readProjectFile('templates/partials/app-details-validation-summary.phtml');
        $this->assertStringContainsString('data-validation-summary', $content);
        $this->assertStringContainsString('data-validation-summary-autofocus=', $content);
        $this->assertStringContainsString('data-validation-error-link', $content);
        $this->assertStringNotContainsString('class="notice"', $content);
    }

    public function testScopedDetailActionsProvideStructuredValidationSummaryErrors(): void
    {
        foreach ($this->detailActionFiles() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertStringContainsString('$validationSummaryErrors = $errorBag->toArray();', $content, $file);
        }
    }

    public function testDefaultStyleGroupIncludesValidationSummaryComponentCss(): void
    {
        $content = $this->readProjectFile('config/assets.php');
        $this->assertStringContainsString("'css/components/app-details-validation-summary.css'", $content);
    }
}
