<?php

namespace App\Filament\Resources\ManagedEnvironmentResource\Pages;

use App\Models\ManagedEnvironment;
use App\Support\ManagedEnvironmentLinks;
use Filament\Actions\Action;

class ManageEnvironmentAccessScopes extends ViewManagedEnvironment
{
    protected static ?string $title = 'Manage environment access scope';

    public function mount(int|string|ManagedEnvironment $environment): void
    {
        parent::mount($environment instanceof ManagedEnvironment ? (string) $environment->getRouteKey() : $environment);
    }

    public function getSubheading(): ?string
    {
        return 'Workspace membership defines the role. Explicit environment scopes only narrow which workspace members can see this environment.';
    }

    protected function getHeaderWidgets(): array
    {
        return [];
    }

    protected function getHeaderActions(): array
    {
        $actions = array_values(array_filter(
            parent::getHeaderActions(),
            static fn ($action): bool => ! ($action instanceof Action && $action->getName() === 'memberships'),
        ));

        array_unshift(
            $actions,
            Action::make('back_to_overview')
                ->label('Back to environment overview')
                ->color('gray')
                ->url(fn (): string => ManagedEnvironmentLinks::viewUrl($this->getRecord())),
        );

        return $actions;
    }
}
