<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class AuthzUiActionContractTest extends TestCase
{
    use AuthzUiContractSupport;
    use ProjectFileAssertionSupport;

    #[DataProvider('adminPageAuthWiringProvider')]
    public function testAdminPageAuthKeysAreWiredFromActions(
        string $templateFile,
        string $actionFile,
        ?string $uiCapabilityMapConstant
    ): void {
        $templateContent = $this->readProjectFile($templateFile);
        $requiredKeys = $this->extractPageAuthKeys($templateContent);
        $this->assertNotEmpty($requiredKeys, $templateFile . ' must read at least one $pageAuth key.');

        $actionContent = $this->readProjectFile($actionFile);
        $this->assertStringContainsString("\$viewAuth['page']", $actionContent, $actionFile);

        $wiredKeys = $this->extractViewAuthPageKeys($actionContent);
        if ($uiCapabilityMapConstant !== null) {
            $this->assertStringContainsString('->pageCapabilities(', $actionContent, $actionFile);
            $this->assertStringContainsString($uiCapabilityMapConstant, $actionContent, $actionFile);
            $wiredKeys = array_values(array_unique([
                ...$wiredKeys,
                ...$this->extractUiCapabilityMapKeys($uiCapabilityMapConstant),
            ]));
        }

        $missingKeys = array_values(array_diff($requiredKeys, $wiredKeys));
        $this->assertSame(
            [],
            $missingKeys,
            sprintf(
                'Missing $pageAuth wiring in %s (from %s): %s',
                $templateFile,
                $actionFile,
                implode(', ', $missingKeys)
            )
        );
    }

    public function testTenantEditActionExposesDeleteCapabilityForTemplate(): void
    {
        $content = $this->readProjectFile('pages/admin/tenants/edit($id).php');
        $this->assertStringContainsString("'can_delete_tenant' =>", $content);
    }

    public function testMapDrivenHardCutActionsUseUiCapabilityMaps(): void
    {
        $actions = [
            'pages/admin/users/index().php' => 'UiCapabilityMap::PAGE_USERS_INDEX',
            'pages/admin/users/create().php' => 'UiCapabilityMap::PAGE_USERS_CREATE',
            'pages/admin/stats/index().php' => 'UiCapabilityMap::PAGE_STATS_INDEX',
            'pages/admin/scheduled-jobs/index().php' => 'UiCapabilityMap::PAGE_JOBS_PURGE',
        ];

        foreach ($actions as $action => $mapConstant) {
            $content = $this->readProjectFile($action);
            $this->assertStringContainsString('->pageCapabilities(', $content, $action);
            $this->assertStringContainsString($mapConstant, $content, $action);
        }
    }

    public function testModuleAuditActionsUseInlinePageCapabilities(): void
    {
        $actions = [
            'modules/audit/pages/audit/api-audit/index().php',
            'modules/audit/pages/audit/system-audit/index().php',
            'modules/audit/pages/audit/import-audit/index().php',
            'modules/audit/pages/audit/settings-user-lifecycle/audit-fragment($id).php',
        ];

        foreach ($actions as $action) {
            $content = $this->readProjectFile($action);
            $this->assertStringContainsString('->pageCapabilities(', $content, $action);
        }
    }
}
