<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class PublicPageContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    public function testPublicStaticPageRoutesPointToPublicPageTargets(): void
    {
        $routes = include $this->projectRootPath() . '/config/routes.php';
        $this->assertIsArray($routes);

        $byPath = [];
        foreach ($routes as $route) {
            if (!is_array($route)) {
                continue;
            }
            $path = trim((string) ($route['path'] ?? ''));
            if ($path === '') {
                continue;
            }
            $byPath[$path] = $route;
        }

        foreach (['imprint', 'privacy', 'terms'] as $slug) {
            $this->assertArrayHasKey($slug, $byPath);
            $this->assertSame('public-page/' . $slug, (string) ($byPath[$slug]['target'] ?? ''));
            $this->assertTrue((bool) ($byPath[$slug]['public'] ?? false));
        }
    }

    public function testPageTemplatesAreSeparatedByContext(): void
    {
        $root = $this->projectRootPath();

        $this->assertFileExists($root . '/pages/page/index(default).phtml');
        $this->assertFileDoesNotExist($root . '/pages/page/index(page).phtml');
        $this->assertFileExists($root . '/pages/public-page/index(page).phtml');
    }

    public function testPageMutationsUseSettingsUpdateCapabilityGuard(): void
    {
        $indexAction = $this->readProjectFile('pages/page/index($slug).php');
        $copyAction = $this->readProjectFile('pages/page/copy-language().php');

        $this->assertStringContainsString('UiAccessService::class', $indexAction);
        $this->assertStringContainsString('SettingsAuthorizationPolicy::ABILITY_ADMIN_SETTINGS_UPDATE', $indexAction);
        $this->assertStringNotContainsString('$canEdit = $currentUserId > 0;', $indexAction);

        $this->assertStringContainsString('UiAccessService::class', $copyAction);
        $this->assertStringContainsString('SettingsAuthorizationPolicy::ABILITY_ADMIN_SETTINGS_UPDATE', $copyAction);
    }
}
