<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class AuthLoginAccessibilityContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    /**
     * @return list<string>
     */
    private function authLoginTemplates(): array
    {
        return [
            'pages/auth/login(login).phtml',
            'pages/auth/register(login).phtml',
            'pages/auth/forgot(login).phtml',
            'pages/auth/reset(login).phtml',
            'pages/auth/verify(login).phtml',
            'pages/auth/verify-email(login).phtml',
        ];
    }

    public function testAuthLoginTemplatesDoNotUseRoleButtonLinks(): void
    {
        foreach ($this->authLoginTemplates() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertStringNotContainsString('role="button"', $content, $file);
        }
    }

    public function testAuthLoginErrorNoticesUseAssertiveLiveRegions(): void
    {
        foreach ($this->authLoginTemplates() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertStringContainsString('class="notice" data-variant="error" role="alert" aria-live="assertive"', $content, $file);
        }
    }

    public function testTenantSelectionUsesSemanticFieldsetAndTextLabel(): void
    {
        $content = $this->readProjectFile('pages/auth/login(login).phtml');

        $this->assertStringContainsString('login-tenant-fieldset', $content);
        $this->assertStringContainsString('login-tenant-select-label', $content);
        $this->assertStringContainsString('login-tenant-choice-text', $content);
        $this->assertStringContainsString('login-microsoft-link', $content);
    }

    public function testAuthLoginFormsExposeErrorDescriptionForInputs(): void
    {
        $describedByPattern = '/aria-describedby="[^"]*\\$errorNoticeId[^"]*"/';

        foreach ($this->authLoginTemplates() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertMatchesRegularExpression($describedByPattern, $content, $file);
            $this->assertStringContainsString('aria-invalid="true"', $content, $file);
        }
    }

    public function testPasswordToggleUsesLocalizedLabelsAndKeyboardFocusableControl(): void
    {
        $layoutContent = $this->readProjectFile('templates/login.phtml');
        $scriptContent = $this->readProjectFile('web/js/components/app-password-toggle.js');

        $this->assertStringContainsString('data-password-toggle-show-label', $layoutContent);
        $this->assertStringContainsString('data-password-toggle-hide-label', $layoutContent);

        $this->assertStringContainsString('passwordToggleShowLabel', $scriptContent);
        $this->assertStringContainsString('passwordToggleHideLabel', $scriptContent);
        $this->assertStringNotContainsString('toggle.tabIndex = -1;', $scriptContent);
        $this->assertStringNotContainsString("'Show password'", $scriptContent);
        $this->assertStringNotContainsString("'Hide password'", $scriptContent);
    }
}
