<?php

namespace App\Filament\Pages;

use App\Filament\Clusters\Inventory\InventoryCluster;
use App\Filament\Widgets\Inventory\InventoryKpiHeader;
use App\Services\Inventory\CoverageCapabilitiesResolver;
use App\Support\Inventory\InventoryPolicyTypeMeta;
use BackedEnum;
use Filament\Pages\Page;
use UnitEnum;

class InventoryCoverage extends Page
{
    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-table-cells';

    protected static ?int $navigationSort = 3;

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

    protected static ?string $navigationLabel = 'Coverage';

    protected static ?string $cluster = InventoryCluster::class;

    protected string $view = 'filament.pages.inventory-coverage';

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

    /**
     * @var array<int, array<string, mixed>>
     */
    public array $supportedPolicyTypes = [];

    /**
     * @var array<int, array<string, mixed>>
     */
    public array $foundationTypes = [];

    public function mount(): void
    {
        $resolver = app(CoverageCapabilitiesResolver::class);

        $this->supportedPolicyTypes = collect(InventoryPolicyTypeMeta::supported())
            ->map(function (array $row) use ($resolver): array {
                $type = (string) ($row['type'] ?? '');

                return array_merge($row, [
                    'dependencies' => $type !== '' && $resolver->supportsDependencies($type),
                ]);
            })
            ->all();

        $this->foundationTypes = collect(InventoryPolicyTypeMeta::foundations())
            ->map(function (array $row): array {
                return array_merge($row, [
                    'dependencies' => false,
                ]);
            })
            ->all();
    }
}
