<?php

declare(strict_types=1);

namespace App\Filament\Resources\AlertDeliveryResource\Pages;

use App\Filament\Resources\AlertDeliveryResource;
use App\Models\Tenant;
use App\Support\OperateHub\OperateHubShell;
use Filament\Resources\Pages\ListRecords;

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

    public function mount(): void
    {
        $activeTenant = app(OperateHubShell::class)->activeEntitledTenant(request());

        if ($activeTenant instanceof Tenant) {
            $filtersSessionKey = $this->getTableFiltersSessionKey();
            $persistedFilters = session()->get($filtersSessionKey, []);

            if (! is_array($persistedFilters)) {
                $persistedFilters = [];
            }

            if (! is_string(data_get($persistedFilters, 'tenant_id.value'))) {
                data_set($persistedFilters, 'tenant_id.value', (string) $activeTenant->getKey());
                session()->put($filtersSessionKey, $persistedFilters);
            }
        }

        parent::mount();
    }

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