<?php

declare(strict_types=1);

namespace App\Services\Entitlements;

final class WorkspacePlanProfileCatalog
{
    /**
     * @var array<string, array{id: string, label: string, description: string, managed_tenant_limit_default: int, review_pack_generation_default: bool, is_default: bool}>
     */
    private const PROFILES = [
        'starter' => [
            'id' => 'starter',
            'label' => 'Starter',
            'description' => 'Minimal allowance for early workspace access and low-volume operations.',
            'managed_tenant_limit_default' => 1,
            'review_pack_generation_default' => false,
            'is_default' => false,
        ],
        'standard' => [
            'id' => 'standard',
            'label' => 'Standard',
            'description' => 'Balanced defaults for most managed workspaces.',
            'managed_tenant_limit_default' => 25,
            'review_pack_generation_default' => true,
            'is_default' => true,
        ],
        'scale' => [
            'id' => 'scale',
            'label' => 'Scale',
            'description' => 'Higher managed-tenant capacity for larger workspace portfolios.',
            'managed_tenant_limit_default' => 100,
            'review_pack_generation_default' => true,
            'is_default' => false,
        ],
    ];

    /**
     * @return list<array{id: string, label: string, description: string, managed_tenant_limit_default: int, review_pack_generation_default: bool, is_default: bool}>
     */
    public function all(): array
    {
        return array_values(self::PROFILES);
    }

    /**
     * @return array{id: string, label: string, description: string, managed_tenant_limit_default: int, review_pack_generation_default: bool, is_default: bool}
     */
    public function default(): array
    {
        return self::PROFILES[self::defaultProfileId()];
    }

    /**
     * @return array{id: string, label: string, description: string, managed_tenant_limit_default: int, review_pack_generation_default: bool, is_default: bool}|null
     */
    public function find(?string $id): ?array
    {
        if ($id === null) {
            return null;
        }

        return self::PROFILES[$id] ?? null;
    }

    /**
     * @return array{id: string, label: string, description: string, managed_tenant_limit_default: int, review_pack_generation_default: bool, is_default: bool}
     */
    public function resolve(?string $id): array
    {
        return $this->find($id) ?? $this->default();
    }

    /**
     * @return array<string, string>
     */
    public function optionLabels(): array
    {
        return array_map(
            static fn (array $profile): string => $profile['label'],
            self::PROFILES,
        );
    }

    /**
     * @return list<string>
     */
    public static function profileIds(): array
    {
        return array_keys(self::PROFILES);
    }

    public static function defaultProfileId(): string
    {
        foreach (self::PROFILES as $id => $profile) {
            if ($profile['is_default']) {
                return $id;
            }
        }

        throw new \RuntimeException('Workspace plan profile catalog is missing a default profile.');
    }
}