<?php

namespace App\Filament\Resources\Categories;

use App\Filament\Resources\Categories\Pages\ManageCategories;
use App\Models\Category;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class CategoryResource extends Resource
{
    protected static ?string $model = Category::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedSquares2x2;

    protected static ?string $navigationLabel = 'Kategoriýalar';

    protected static string|\UnitEnum|null $navigationGroup = 'Harytlar';

    protected static ?int $navigationSort = 1;

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('name')
                    ->label('Ady')
                    ->required()
                    ->live(onBlur: true),
                TextInput::make('slug')
                    ->label('Slug')
                    ->required()
                    ->unique(ignoreRecord: true),
                Select::make('icon')
                    ->label('Ikony (Material Symbol)')
                    ->required()
                    ->default('category')
                    ->options([
                        'set_meal' => 'set_meal — Nahar toplum',
                        'cruelty_free' => 'cruelty_free — Goýun eti',
                        'kebab_dining' => 'kebab_dining — Kebab / Goş',
                        'egg' => 'egg — Guş eti',
                        'local_pizza' => 'local_pizza — Kolbasalar',
                        'lunch_dining' => 'lunch_dining — Et tagamy',
                        'outdoor_grill' => 'outdoor_grill — Grill',
                        'restaurant_menu' => 'restaurant_menu — Menýu',
                        'food_bank' => 'food_bank — Azyk ammar',
                        'category' => 'category — Kategoriýa',
                        'bakery_dining' => 'bakery_dining — Çörek',
                        'fastfood' => 'fastfood — Çalt nahar',
                    ]),
                TextInput::make('sort_order')
                    ->label('Tertip')
                    ->required()
                    ->numeric()
                    ->default(0),
                Toggle::make('is_active')
                    ->label('Işjeň')
                    ->default(true),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Ady')
                    ->searchable(),
                TextColumn::make('slug')
                    ->label('Slug')
                    ->searchable(),
                TextColumn::make('icon')
                    ->label('Ikony'),
                TextColumn::make('sort_order')
                    ->label('Tertip')
                    ->numeric()
                    ->sortable(),
                IconColumn::make('is_active')
                    ->label('Işjeň')
                    ->boolean(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->defaultSort('sort_order')
            ->filters([])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageCategories::route('/'),
        ];
    }
}
