<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

final class DetailValidationTemplateContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

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

    public function testScopedDetailTemplatesDoNotInlineLegacyErrorLoop(): void
    {
        foreach (DetailValidationContractFiles::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);
    }
}
