<?php

namespace MintyPHP\Tests\Architecture;

trait AuthzUiContractSupport
{
    /**
     * @return array<string, array{0: string, 1: string, 2: string|null}>
     */
    public static function adminPageAuthWiringProvider(): array
    {
        return [
            'users index' => [
                'pages/admin/users/index(default).phtml',
                'pages/admin/users/index().php',
                'UiCapabilityMap::PAGE_USERS_INDEX',
            ],
            'users create' => [
                'pages/admin/users/create(default).phtml',
                'pages/admin/users/create().php',
                'UiCapabilityMap::PAGE_USERS_CREATE',
            ],
            'tenants edit' => [
                'pages/admin/tenants/edit(default).phtml',
                'pages/admin/tenants/edit($id).php',
                null,
            ],
            'departments edit' => [
                'pages/admin/departments/edit(default).phtml',
                'pages/admin/departments/edit($id).php',
                null,
            ],
            'stats index' => [
                'pages/admin/stats/index(default).phtml',
                'pages/admin/stats/index().php',
                'UiCapabilityMap::PAGE_STATS_INDEX',
            ],
            'api audit index' => [
                'modules/audit/pages/audit/api-audit/index(default).phtml',
                'modules/audit/pages/audit/api-audit/index().php',
                null,
            ],
            'import audit index' => [
                'modules/audit/pages/audit/import-audit/index(default).phtml',
                'modules/audit/pages/audit/import-audit/index().php',
                null,
            ],
            'scheduled jobs index' => [
                'pages/admin/scheduled-jobs/index(default).phtml',
                'pages/admin/scheduled-jobs/index().php',
                'UiCapabilityMap::PAGE_JOBS_PURGE',
            ],
            'system audit index' => [
                'modules/audit/pages/audit/system-audit/index(default).phtml',
                'modules/audit/pages/audit/system-audit/index().php',
                null,
            ],
        ];
    }

    /**
     * @return list<string>
     */
    private function extractPageAuthKeys(string $content): array
    {
        preg_match_all('/\\$pageAuth\\[[\'"]([a-z_]+)[\'"]\\]/', $content, $matches);
        $keys = array_values(array_unique($matches[1]));
        sort($keys);
        return $keys;
    }

    /**
     * @return list<string>
     */
    private function extractViewAuthPageKeys(string $content): array
    {
        $keys = [];

        // Match inline array: $viewAuth['page'] = ['key' => ...];
        preg_match_all('/\\$viewAuth\\[\'page\'\\]\\s*=\\s*\\[(.*?)\\];/s', $content, $blocks);
        foreach ($blocks[1] as $block) {
            preg_match_all('/[\'"]([a-z_]+)[\'"]\\s*=>/', $block, $matches);
            foreach ($matches[1] as $key) {
                $keys[] = $key;
            }
        }

        // Match pageCapabilities() inline array: ->pageCapabilities(..., ['key' => ...])
        if (preg_match('/->pageCapabilities\\([^,]+,\\s*\\[(.*?)\\]\\s*\\)/s', $content, $pcMatch)) {
            preg_match_all('/[\'"]([a-z_]+)[\'"]\\s*=>/', $pcMatch[1], $matches);
            foreach ($matches[1] as $key) {
                $keys[] = $key;
            }
        }

        $keys = array_values(array_unique($keys));
        sort($keys);
        return $keys;
    }

    /**
     * @return list<string>
     */
    private function extractUiCapabilityMapKeys(string $mapConstant): array
    {
        $parts = explode('::', $mapConstant);
        $constantName = trim((string) end($parts));
        $this->assertNotSame('', $constantName, 'Invalid UiCapabilityMap constant: ' . $mapConstant);

        $mapContent = $this->readProjectFile('core/Service/Access/UiCapabilityMap.php');
        $constantPattern = '/public const\\s+' . preg_quote($constantName, '/') . '\\s*=\\s*\\[(.*?)\\];/s';
        $matched = preg_match($constantPattern, $mapContent, $constantMatch);
        $this->assertSame(
            1,
            $matched,
            'Could not find UiCapabilityMap constant block: ' . $mapConstant
        );

        preg_match_all('/[\'"]([a-z_]+)[\'"]\\s*=>/', (string) ($constantMatch[1] ?? ''), $keyMatches);
        $keys = array_values(array_unique($keyMatches[1]));
        sort($keys);
        return $keys;
    }
}
