<?php

declare(strict_types=1);

namespace App\Filament\Resources\AlertDeliveryResource\Pages;

use App\Filament\Resources\AlertDeliveryResource;
use App\Support\Filament\CanonicalAdminEnvironmentFilterState;
use App\Support\Navigation\CanonicalNavigationContext;
use App\Support\OperateHub\OperateHubShell;
use Filament\Actions\Action;
use Filament\Resources\Pages\ListRecords;

class ListAlertDeliveries extends ListRecords
{
    protected static string $resource = AlertDeliveryResource::class;

    public function mount(): void
    {
        app(CanonicalAdminEnvironmentFilterState::class)->sync($this->getTableFiltersSessionKey(), request: request());

        parent::mount();
    }

    protected function getHeaderActions(): array
    {
        $actions = app(OperateHubShell::class)->headerActions(
            scopeActionName: 'operate_hub_scope_alerts',
            returnActionName: 'operate_hub_return_alerts',
        );

        $navigationContext = CanonicalNavigationContext::fromRequest(request());

        if ($navigationContext?->backLinkLabel !== null && $navigationContext->backLinkUrl !== null) {
            array_splice($actions, 1, 0, [
                Action::make('operate_hub_back_to_origin_alert_deliveries')
                    ->label($navigationContext->backLinkLabel)
                    ->icon('heroicon-o-arrow-left')
                    ->color('gray')
                    ->url($navigationContext->backLinkUrl),
            ]);
        }

        return $actions;
    }
}
