<?php

declare(strict_types=1);

namespace App\Support\Livewire\TrustedState;

use App\Models\ManagedEnvironment;
use App\Models\ManagedEnvironmentOnboardingSession;
use App\Models\User;
use App\Models\Workspace;
use App\Services\Onboarding\OnboardingDraftResolver;
use App\Services\System\AllowedTenantUniverse;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Http\Request;

final class TrustedStateResolver
{
    /**
     * @return array<string>
     */
    public function requiredAuthoritySources(string $component, TrustedStatePolicy $policy): array
    {
        return $policy->forComponent($component)['authority_sources'];
    }

    public function currentWorkspaceForMember(User $user, WorkspaceContext $workspaceContext, ?Request $request = null): Workspace
    {
        return $workspaceContext->currentWorkspaceForMemberOrFail($user, $request);
    }

    public function resolveOnboardingDraft(
        ManagedEnvironmentOnboardingSession|int|string $draft,
        User $user,
        Workspace $workspace,
        OnboardingDraftResolver $resolver,
    ): ManagedEnvironmentOnboardingSession {
        return $resolver->resolveForTrustedAction($draft, $user, $workspace);
    }

    public function resolveAllowedTenantProposal(
        int|string|null $tenantId,
        AllowedTenantUniverse $allowedTenantUniverse,
    ): ?ManagedEnvironment {
        return $allowedTenantUniverse->resolveAllowed($tenantId);
    }

    public function resolveAllowedTenantProposalOrFail(
        int|string|null $tenantId,
        AllowedTenantUniverse $allowedTenantUniverse,
    ): ManagedEnvironment {
        return $allowedTenantUniverse->resolveAllowedOrFail($tenantId);
    }
}
