<?php

namespace App\Support\Providers;

use App\Filament\Resources\ProviderConnectionResource;
use App\Models\ProviderConnection;
use App\Models\Tenant;
use App\Support\Links\RequiredPermissionsLinks;

final class ProviderNextStepsRegistry
{
    /**
     * @return array<int, array{label: string, url: string}>
     */
    public function forReason(Tenant $tenant, string $reasonCode, ?ProviderConnection $connection = null): array
    {
        return match ($reasonCode) {
            ProviderReasonCodes::ProviderConnectionMissing,
            ProviderReasonCodes::ProviderConnectionInvalid,
            ProviderReasonCodes::TenantTargetMismatch => [
                [
                    'label' => 'Manage Provider Connections',
                    'url' => ProviderConnectionResource::getUrl('index', ['tenant' => $tenant->external_id], panel: 'admin'),
                ],
            ],
            ProviderReasonCodes::ProviderCredentialMissing,
            ProviderReasonCodes::ProviderCredentialInvalid,
            ProviderReasonCodes::ProviderAuthFailed,
            ProviderReasonCodes::ProviderConsentMissing => [
                [
                    'label' => 'Grant admin consent',
                    'url' => RequiredPermissionsLinks::adminConsentPrimaryUrl($tenant),
                ],
                [
                    'label' => $connection instanceof ProviderConnection ? 'Update Credentials' : 'Manage Provider Connections',
                    'url' => $connection instanceof ProviderConnection
                        ? ProviderConnectionResource::getUrl('edit', ['tenant' => $tenant->external_id, 'record' => (int) $connection->getKey()], panel: 'admin')
                        : ProviderConnectionResource::getUrl('index', ['tenant' => $tenant->external_id], panel: 'admin'),
                ],
            ],
            ProviderReasonCodes::ProviderPermissionMissing,
            ProviderReasonCodes::ProviderPermissionDenied,
            ProviderReasonCodes::ProviderPermissionRefreshFailed,
            ProviderReasonCodes::IntuneRbacPermissionMissing => [
                [
                    'label' => 'Open Required Permissions',
                    'url' => RequiredPermissionsLinks::requiredPermissions($tenant),
                ],
            ],
            ProviderReasonCodes::NetworkUnreachable,
            ProviderReasonCodes::RateLimited,
            ProviderReasonCodes::UnknownError => [
                [
                    'label' => 'Review Provider Connection',
                    'url' => $connection instanceof ProviderConnection
                        ? ProviderConnectionResource::getUrl('edit', ['tenant' => $tenant->external_id, 'record' => (int) $connection->getKey()], panel: 'admin')
                        : ProviderConnectionResource::getUrl('index', ['tenant' => $tenant->external_id], panel: 'admin'),
                ],
            ],
            default => [
                [
                    'label' => 'Manage Provider Connections',
                    'url' => ProviderConnectionResource::getUrl('index', ['tenant' => $tenant->external_id], panel: 'admin'),
                ],
            ],
        };
    }
}
