<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

final class BookmarksModuleIsolationContractTest extends TestCase
{
    use ModuleExtractionContractSupport;

    public function testCoreTemplatesHaveNoBookmarkSpecificMarkup(): void
    {
        $this->assertFilesDoNotContain(
            [
                dirname(__DIR__, 2) . '/templates/default.phtml',
                dirname(__DIR__, 2) . '/templates/partials/app-topbar.phtml',
                dirname(__DIR__, 2) . '/templates/partials/app-main-aside.phtml',
                dirname(__DIR__, 2) . '/templates/partials/app-main-aside-icon-bar.phtml',
            ],
            [
                'aside-tab-bookmarks',
                'aside-panel-bookmarks',
                'data-app-bookmark-dialog',
                'bookmarks.nav',
                'components.bookmark',
            ]
        );
    }

    public function testCoreBookmarkEntrypointsAreRemoved(): void
    {
        $root = dirname(__DIR__, 2);

        self::assertFileDoesNotExist($root . '/templates/partials/app-bookmark-dialog.phtml');
        self::assertFileDoesNotExist($root . '/web/js/components/app-bookmark-save.js');
        self::assertFileDoesNotExist($root . '/web/js/components/app-bookmark-panel.js');
        self::assertFileDoesNotExist($root . '/web/css/components/app-bookmark-form.css');
        self::assertDirectoryDoesNotExist($root . '/pages/bookmarks');
    }

    public function testCoreNoLongerContainsBookmarkServiceDirectory(): void
    {
        $root = dirname(__DIR__, 2);
        self::assertDirectoryDoesNotExist(
            $root . '/core/Service/Bookmark',
            'Bookmark service classes should live in modules/bookmarks/lib, not in core/Service'
        );
    }

    public function testCoreNoLongerContainsBookmarkRepositoryFiles(): void
    {
        $root = dirname(__DIR__, 2);
        $repoDir = $root . '/core/Repository/User';
        if (!is_dir($repoDir)) {
            self::addToAssertionCount(1);
            return;
        }

        $entries = scandir($repoDir) ?: [];
        $bookmarkFiles = array_filter($entries, static fn (string $f): bool => str_starts_with($f, 'Bookmark'));
        self::assertSame(
            [],
            array_values($bookmarkFiles),
            'Core core/Repository/User/ still contains Bookmark files: ' . implode(', ', $bookmarkFiles)
        );
    }

    public function testCoreNoLongerContainsBookmarkUrlNormalizer(): void
    {
        $root = dirname(__DIR__, 2);
        self::assertFileDoesNotExist(
            $root . '/core/Support/BookmarkUrlNormalizer.php',
            'BookmarkUrlNormalizer should live in modules/bookmarks/lib, not in core/Support'
        );
    }

    public function testCoreAuthLifecycleHasNoLegacyBookmarkSessionKeyWrites(): void
    {
        $file = dirname(__DIR__, 2) . '/core/Service/Auth/AuthSessionTenantContextService.php';
        $content = file_get_contents($file);
        self::assertIsString($content);
        self::assertStringNotContainsString('user_bookmarks', $content);
        self::assertStringNotContainsString('module.bookmarks.grouped', $content);
    }
}
