<?php

declare(strict_types=1);

namespace App\Support\Ai;

final class AiUseCaseCatalog
{
    /**
     * @var array<string, array{
     *     key: string,
     *     label: string,
     *     future_consumer: string,
     *     visibility: string,
     *     allowed_provider_classes: list<string>,
     *     allowed_data_classifications: list<string>,
     *     source_family: string,
     *     tenant_context_permitted: bool
     * }>
     */
    private const USE_CASES = [
        'product_knowledge.answer_draft' => [
            'key' => 'product_knowledge.answer_draft',
            'label' => 'Product knowledge answer draft',
            'future_consumer' => 'ContextualHelpResolver',
            'visibility' => 'internal_only_draft',
            'allowed_provider_classes' => [AiProviderClass::LocalPrivate->value],
            'allowed_data_classifications' => [
                AiDataClassification::ProductKnowledge->value,
                AiDataClassification::OperationalMetadata->value,
            ],
            'source_family' => 'product_knowledge',
            'tenant_context_permitted' => false,
        ],
        'support_diagnostics.summary_draft' => [
            'key' => 'support_diagnostics.summary_draft',
            'label' => 'Support diagnostics summary draft',
            'future_consumer' => 'SupportDiagnosticBundleBuilder',
            'visibility' => 'internal_only_draft',
            'allowed_provider_classes' => [AiProviderClass::LocalPrivate->value],
            'allowed_data_classifications' => [AiDataClassification::RedactedSupportSummary->value],
            'source_family' => 'support_diagnostics',
            'tenant_context_permitted' => true,
        ],
    ];

    /**
     * @return list<array{
     *     key: string,
     *     label: string,
     *     future_consumer: string,
     *     visibility: string,
     *     allowed_provider_classes: list<string>,
     *     allowed_data_classifications: list<string>,
     *     source_family: string,
     *     tenant_context_permitted: bool
     * }>
     */
    public function all(): array
    {
        return array_values(self::USE_CASES);
    }

    /**
     * @return array{
     *     key: string,
     *     label: string,
     *     future_consumer: string,
     *     visibility: string,
     *     allowed_provider_classes: list<string>,
     *     allowed_data_classifications: list<string>,
     *     source_family: string,
     *     tenant_context_permitted: bool
     * }|null
     */
    public function find(string $key): ?array
    {
        return self::USE_CASES[$key] ?? null;
    }

    /**
     * @return list<string>
     */
    public function labels(): array
    {
        return array_map(
            static fn (array $definition): string => $definition['label'],
            $this->all(),
        );
    }

    /**
     * @return list<string>
     */
    public function allowedProviderClassLabelsForMode(AiPolicyMode $mode): array
    {
        if ($mode === AiPolicyMode::Disabled) {
            return [];
        }

        $labels = [];

        foreach ($this->all() as $definition) {
            foreach ($definition['allowed_provider_classes'] as $providerClass) {
                $labels[$providerClass] = AiProviderClass::from($providerClass)->label();
            }
        }

        return array_values($labels);
    }

    /**
     * @return list<string>
     */
    public function blockedDataClassificationLabels(): array
    {
        return array_map(
            static fn (AiDataClassification $classification): string => $classification->label(),
            [
                AiDataClassification::PersonalData,
                AiDataClassification::CustomerConfidential,
                AiDataClassification::RawProviderPayload,
            ],
        );
    }
}