<?php

namespace App\Filament\Resources;

use App\Filament\Resources\EventTypeResource\Pages;
use App\Models\EventType;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Actions;
use Filament\Tables\Table;
use UnitEnum;
use BackedEnum;

class EventTypeResource extends Resource
{
    protected static ?string $model = EventType::class;
    protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-swatch';
    protected static UnitEnum|string|null $navigationGroup = 'Library';
    protected static ?int $navigationSort = 20;

    public static function form(Schema $form): Schema
    {
        return $form->schema([
            KeyValue::make('name')->label('Name (de/en)')->default(['de' => '', 'en' => ''])->required(),
            TextInput::make('slug')->required()->unique(ignoreRecord: true),
            TextInput::make('icon')->maxLength(64),
            KeyValue::make('settings')->label('Settings')->keyLabel('key')->valueLabel('value'),
            Select::make('emotions')
                ->label('Emotions')
                ->multiple()
                ->searchable()
                ->preload()
                ->relationship('emotions', 'name'),
        ])->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')->sortable(),
                Tables\Columns\TextColumn::make('name')->searchable(),
                Tables\Columns\TextColumn::make('slug')->searchable(),
                Tables\Columns\TextColumn::make('icon'),
                Tables\Columns\TextColumn::make('created_at')->since(),
            ])
            ->filters([])
            ->actions([
                Actions\EditAction::make(),
            ])
            ->bulkActions([
                Actions\DeleteBulkAction::make(),
            ]);
    }

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