<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class FilterDrawerRuntimeContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    public function testUsersResetKeepsTenantParam(): void
    {
        $moduleContent = $this->readProjectFile('web/js/pages/admin-users-index.js');
        $this->assertStringContainsString("preserveFilterParams: ['tenant']", $moduleContent);
    }

    public function testFilterDrawerAndExperienceImplementFocusTrapAndDirtyApplyUi(): void
    {
        $drawerJs = $this->readProjectFile('web/js/components/app-filter-drawer.js');
        $this->assertStringContainsString("drawer.setAttribute('role', 'dialog')", $drawerJs);
        $this->assertStringContainsString("drawer.setAttribute('aria-modal', 'true')", $drawerJs);
        // Focus-trap + focus-return implementation moved to shared utility.
        $this->assertStringContainsString("from '../core/app-focus-trap.js'", $drawerJs);
        $this->assertStringContainsString('focusTrap.activate(', $drawerJs);
        $this->assertStringContainsString('focusTrap.deactivate()', $drawerJs);

        $trapJs = $this->readProjectFile('web/js/core/app-focus-trap.js');
        $this->assertStringContainsString("if (event.key !== 'Tab')", $trapJs);
        $this->assertStringContainsString("lastTrigger && typeof lastTrigger.focus === 'function'", $trapJs);

        $experienceJs = $this->readProjectFile('web/js/pages/app-list-filter-experience.js');
        $this->assertStringContainsString('countDraftChanges(', $experienceJs);
        $this->assertStringContainsString('countActiveDraftFilters(', $experienceJs);
        $this->assertStringContainsString('drawerController.setApplyEnabled(changedCount > 0);', $experienceJs);
        $this->assertStringContainsString('drawerController.setApplyCount(activeCount);', $experienceJs);
        $this->assertStringContainsString('announceLive(', $experienceJs);

        $stateJs = $this->readProjectFile('web/js/pages/app-list-filter-state.js');
        $this->assertStringContainsString('buildChipsFromMeta', $stateJs);
        $this->assertStringContainsString('removeChipFromMetaState', $stateJs);
        $this->assertStringContainsString('clearMetaState', $stateJs);
    }
}
