<?php

namespace MintyPHP\Tests\Architecture;

use MintyPHP\App\Module\ModuleRegistry;
use PHPUnit\Framework\TestCase;

final class ModuleRegistryBootstrapContractTest extends TestCase
{
    public function testRegistryIsAvailableInContainer(): void
    {
        $registry = app(ModuleRegistry::class);

        self::assertInstanceOf(ModuleRegistry::class, $registry);
    }

    public function testAddressBookModuleLoadsExpectedRuntimeContributions(): void
    {
        $modulesDir = dirname(__DIR__, 2) . '/modules';
        $registry = ModuleRegistry::boot($modulesDir, ['addressbook']);

        self::assertTrue($registry->hasModule('addressbook'));
        self::assertNotEmpty($registry->getPermissions());
        self::assertNotEmpty($registry->getSearchResources());
        self::assertEmpty($registry->getLayoutContextProviders());
        self::assertEmpty($registry->getSessionProviders());
        self::assertNotEmpty($registry->getSlotContributions('explorer.nav_item'));
    }

    public function testEmptyEnvDisablesAllConfiguredModules(): void
    {
        $modulesDir = dirname(__DIR__, 2) . '/modules';

        putenv('APP_ENABLED_MODULES=');

        try {
            $enabledModules = ModuleRegistry::resolveEnabledModules([
                'enabled_modules' => ['addressbook', 'bookmarks'],
            ]);
            $registry = ModuleRegistry::boot($modulesDir, $enabledModules);
        } finally {
            putenv('APP_ENABLED_MODULES');
        }

        self::assertSame([], $enabledModules, 'APP_ENABLED_MODULES empty string must disable all modules.');
        self::assertSame([], $registry->getModules());
        self::assertSame([], $registry->getRoutes());
        self::assertSame([], $registry->getPermissions());
        self::assertSame([], $registry->getSearchResources());
        self::assertSame([], $registry->getLayoutContextProviders());
        self::assertSame([], $registry->getSessionProviders());
        self::assertSame([], $registry->getSlotContributions('aside.tab_panel'));
    }
}
