<?php

declare(strict_types=1);

namespace App\Filament\Navigation;

use App\Filament\Resources\FieldResource;
use Filament\Navigation\NavigationItem;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class FieldNavigation
{
    public static function getNavigationItems(): array
    {
        // Fetch unique model_type values
        $modelTypes = DB::table('model_has_fields')->distinct()->pluck('model_type')->filter()->values();

        return collect($modelTypes)->map(function ($modelType) {
            $item = match ($modelType) {
                'App\Models\Product' => ['title' => 'محصولات', 'group'=>'محصولات', 'icon' => 'heroicon-o-device-phone-mobile'],
                'App\Models\Application' => ['title' => 'برنامه ها', 'group'=>'برنامه ها', 'icon' => 'heroicon-o-device-phone-mobile'],
                'App\Models\Game' => ['title' => 'بازی ها', 'group'=>'بازی ها', 'icon' => 'heroicon-o-puzzle-piece'],
                'App\Models\Category' => ['title' => 'دسته بندی ها', 'group'=>'گیفت کارت ها', 'icon' => 'heroicon-o-gift'],
                'App\Models\Virtual' => ['title' => 'شماره مجازی', 'group'=> 'شماره مجازی', 'icon' => 'heroicon-o-credit-card'],
                default => ['title' => Str::replace('App\Models\\', '', $modelType), 'group'=> $modelType, 'icon' => 'heroicon-o-shopping-cart'],
            };

            return NavigationItem::make('فیلد ها')
                ->url(fn () => FieldResource::getUrl('index', [
                    'tableFilters[model_type][value]' => $modelType,
                ]))
                ->icon($item['icon'])
                ->group($item['group'])
                ->sort(0); // Optional: Adjust sort order if needed
        })->toArray();
    }
}
