<?php

declare(strict_types=1);

namespace App\Filament\Clusters\Inventory;

use App\Models\ManagedEnvironment;
use App\Models\Workspace;
use App\Support\Navigation\NavigationScope;
use App\Support\OperateHub\OperateHubShell;
use BackedEnum;
use Filament\Clusters\Cluster;
use Filament\Facades\Filament;
use Filament\Pages\Enums\SubNavigationPosition;
use Filament\Panel;
use Illuminate\Database\Eloquent\Model;
use UnitEnum;

class InventoryCluster extends Cluster
{
    protected static ?SubNavigationPosition $subNavigationPosition = SubNavigationPosition::Start;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-squares-2x2';

    protected static string|UnitEnum|null $navigationGroup = 'Inventory';

    protected static ?string $navigationLabel = 'Items';

    public static function shouldRegisterNavigation(): bool
    {
        return NavigationScope::shouldRegisterEnvironmentNavigation()
            && parent::shouldRegisterNavigation();
    }

    public static function getSlug(?Panel $panel = null): string
    {
        $panelId = $panel?->getId() ?? Filament::getCurrentOrDefaultPanel()?->getId();

        if ($panelId !== 'admin') {
            return parent::getSlug($panel);
        }

        return 'workspaces/{workspace}/environments/{environment}/inventory';
    }

    /**
     * @param  array<mixed>  $parameters
     */
    public static function getUrl(array $parameters = [], bool $isAbsolute = true, ?string $panel = null, ?Model $tenant = null): string
    {
        $panelId = $panel ?? Filament::getCurrentOrDefaultPanel()?->getId() ?? 'admin';

        if ($panelId !== 'admin') {
            return parent::getUrl($parameters, $isAbsolute, $panelId, $tenant);
        }

        $resolvedTenant = static::resolveAdminUrlTenant($parameters, $tenant);

        if (! $resolvedTenant instanceof ManagedEnvironment) {
            return url('/admin');
        }

        $workspace = static::resolveAdminUrlWorkspace($resolvedTenant, $parameters);

        if (! $workspace instanceof Workspace && ! is_string($workspace) && ! is_int($workspace)) {
            return url('/admin');
        }

        $parameters['environment'] ??= $resolvedTenant;
        $parameters['workspace'] ??= $workspace;
        unset($parameters['tenant']);

        return parent::getUrl($parameters, $isAbsolute, $panelId, null);
    }

    /**
     * @param  array<mixed>  $parameters
     */
    private static function resolveAdminUrlTenant(array $parameters, ?Model $tenant = null): ?ManagedEnvironment
    {
        $parameterTenant = $parameters['tenant'] ?? $parameters['environment'] ?? null;

        if ($parameterTenant instanceof ManagedEnvironment) {
            return $parameterTenant;
        }

        if ($tenant instanceof ManagedEnvironment) {
            return $tenant;
        }

        $filamentTenant = Filament::getTenant();

        if ($filamentTenant instanceof ManagedEnvironment) {
            return $filamentTenant;
        }

        return app(OperateHubShell::class)->tenantOwnedPanelContext(request());
    }

    /**
     * @param  array<mixed>  $parameters
     */
    private static function resolveAdminUrlWorkspace(ManagedEnvironment $tenant, array $parameters): Workspace|string|int|null
    {
        $workspace = $parameters['workspace'] ?? null;

        if ($workspace instanceof Workspace || is_string($workspace) || is_int($workspace)) {
            return $workspace;
        }

        $tenantWorkspace = $tenant->workspace;

        if ($tenantWorkspace instanceof Workspace) {
            return $tenantWorkspace;
        }

        return $tenant->workspace()->first();
    }
}
