<?php

declare(strict_types=1);

namespace App\Support\OperationalControls;

final class OperationalControlCatalog
{
    /**
     * @var array<string, array{key: string, label: string, supported_scopes: array<int, string>, operation_types: array<int, string>, affected_surfaces: array<int, string>}>
     */
    private const DEFINITIONS = [
        'restore.execute' => [
            'key' => 'restore.execute',
            'label' => 'Restore execution',
            'supported_scopes' => ['global', 'workspace'],
            'operation_types' => ['restore.execute'],
            'affected_surfaces' => ['tenant.restore_runs.create'],
        ],
        'promotion.execute' => [
            'key' => 'promotion.execute',
            'label' => 'Promotion execution',
            'supported_scopes' => ['global', 'workspace'],
            'operation_types' => ['promotion.execute'],
            'affected_surfaces' => ['admin.cross_environment_compare.execute'],
        ],
        'ai.execution' => [
            'key' => 'ai.execution',
            'label' => 'AI execution',
            'supported_scopes' => ['global'],
            'operation_types' => ['ai.execution'],
            'affected_surfaces' => ['governed_ai.execution'],
        ],
    ];

    /**
     * @return array<int, string>
     */
    public function keys(): array
    {
        return array_keys(self::DEFINITIONS);
    }

    /**
     * @return array<string, array<string, mixed>>
     */
    public function definitions(): array
    {
        return self::DEFINITIONS;
    }

    /**
     * @return array{key: string, label: string, supported_scopes: array<int, string>, operation_types: array<int, string>, affected_surfaces: array<int, string>}
     */
    public function definition(string $controlKey): array
    {
        $controlKey = trim($controlKey);

        if (! array_key_exists($controlKey, self::DEFINITIONS)) {
            throw new \InvalidArgumentException("Unknown operational control [{$controlKey}].");
        }

        return self::DEFINITIONS[$controlKey];
    }

    public function label(string $controlKey): string
    {
        return $this->definition($controlKey)['label'];
    }
}