<?php

namespace App\Filament\Resources\Categories\Schemas;

use App\Models\Category;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Schema;

class CategoryForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('parent_id')
                    ->label('Parent category')
                    ->searchable()
                    ->placeholder('— Top level —')
                    ->helperText('Leave empty for a top-level category.')
                    ->options(fn() => Category::query()
                        ->where('level', '<=', 2)
                        ->orderBy('path')
                        ->get()
                        ->mapWithKeys(fn($c) => [
                            $c->id => str_repeat('— ', max(0, $c->level - 1)) . $c->name_en,
                        ])
                        ->all()),
                TextInput::make('name_en')
                    ->label('Name (English)')
                    ->required(),
                TextInput::make('name_ur')
                    ->label('Name (Urdu)')
                    ->default(null),
                TextInput::make('name_ps')
                    ->label('Name (Pashto)')
                    ->default(null),
                TextInput::make('slug')
                    ->required()
                    ->unique(ignoreRecord: true),
                TextInput::make('icon')
                    ->helperText('lucide icon key, used for top-level categories')
                    ->default(null),
                FileUpload::make('image')
                    ->image(),
                Textarea::make('description')
                    ->default(null)
                    ->columnSpanFull(),
                Toggle::make('is_active')
                    ->default(true)
                    ->required(),
                TextInput::make('sort_order')
                    ->required()
                    ->numeric()
                    ->default(0),
            ]);
    }
}
