<?php

namespace App\Support;

final class AdminPermissionBridge
{
    /** @var array<string, string> */
    private const DEPRECATED_LEGACY_SLUG_ALIASES = [
        'prd.audit.all' => 'prd.audit.view',
        'prd.audit.self' => 'prd.audit.view',
        'prd.audit.finance' => 'prd.audit.view',
        'prd.report.all' => 'prd.report.view',
        'prd.report.risk' => 'prd.report.view',
        'prd.report.finance' => 'prd.report.view',
        'prd.report.player' => 'prd.report.view',
    ];

    /**
     * 将请求或历史数据中的 `prd.*` 归一为当前 Registry 目录中的 slug。
     *
     * @param  list<string>  $slugs
     * @return list<string>
     */
    public static function normalizeCanonicalLegacySlugs(array $slugs): array
    {
        $canonical = [];
        $known = array_fill_keys(self::allLegacySlugs(), true);

        foreach ($slugs as $slug) {
            if (! is_string($slug) || $slug === '') {
                continue;
            }

            if (isset(self::DEPRECATED_LEGACY_SLUG_ALIASES[$slug])) {
                $slug = self::DEPRECATED_LEGACY_SLUG_ALIASES[$slug];
            }

            if (isset($known[$slug])) {
                $canonical[$slug] = true;
            }
        }

        $keys = array_keys($canonical);
        sort($keys);

        return $keys;
    }

    /** @return array<string, list<string>> */
    public static function legacyMap(): array
    {
        /** @var array<string, list<string>> */
        return config('admin_permissions.legacy_map', []);
    }

    /** @return list<string> */
    public static function allLegacySlugs(): array
    {
        $keys = array_keys(self::legacyMap());
        sort($keys);

        return $keys;
    }

    /** @return list<string> */
    public static function menuActionCodesForLegacy(string $legacySlug): array
    {
        return array_values(array_unique(self::legacyMap()[$legacySlug] ?? []));
    }

    /**
     * 由已授权的 menu_action.permission_code 反推 `prd.*` 展示 slug（须满足映射中的全部 code）。
     *
     * @param  list<string>  $menuActionCodes
     * @return list<string>
     */
    public static function legacySlugsGrantedByMenuActionCodes(array $menuActionCodes): array
    {
        if ($menuActionCodes === []) {
            return [];
        }

        $set = [];
        foreach ($menuActionCodes as $code) {
            if (is_string($code) && $code !== '') {
                $set[$code] = true;
            }
        }
        if ($set === []) {
            return [];
        }

        $out = [];
        foreach (self::legacyMap() as $legacySlug => $requiredCodes) {
            if ($requiredCodes === []) {
                continue;
            }

            $hasAll = true;
            foreach ($requiredCodes as $code) {
                if (! isset($set[$code])) {
                    $hasAll = false;
                    break;
                }
            }

            if ($hasAll) {
                $out[$legacySlug] = true;
            }
        }

        $keys = array_keys($out);
        sort($keys);

        return $keys;
    }
}
