<?php

namespace App\Filament\Resources;

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

class TaskResource extends Resource
{
    protected static ?string $model = Task::class;
    protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-clipboard-document-check';
    protected static UnitEnum|string|null $navigationGroup = 'Library';
    protected static ?int $navigationSort = 30;

    public static function form(Schema $form): Schema
    {
        return $form->schema([
            Select::make('emotion_id')->relationship('emotion', 'name')->required()->searchable()->preload(),
            Select::make('event_type_id')->relationship('eventType', 'name')->searchable()->preload()->label('Event Type (optional)'),
            KeyValue::make('title')->label('Title (de/en)')->default(['de' => '', 'en' => ''])->required(),
            KeyValue::make('description')->label('Description (de/en)'),
            Select::make('difficulty')->options([
                'easy' => 'Easy',
                'medium' => 'Medium',
                'hard' => 'Hard',
            ])->default('easy'),
            KeyValue::make('example_text')->label('Example (de/en)'),
            TextInput::make('sort_order')->numeric()->default(0),
            Toggle::make('is_active')->default(true),
        ])->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')->sortable(),
                Tables\Columns\TextColumn::make('emotion.name')->label('Emotion')->sortable()->searchable(),
                Tables\Columns\TextColumn::make('eventType.name')->label('Event Type')->toggleable(),
                Tables\Columns\TextColumn::make('title')->searchable()->limit(40),
                Tables\Columns\TextColumn::make('difficulty')->badge(),
                Tables\Columns\IconColumn::make('is_active')->boolean(),
                Tables\Columns\TextColumn::make('sort_order')->sortable(),
            ])
            ->filters([])
            ->actions([
                Actions\EditAction::make(),
                Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Actions\DeleteBulkAction::make(),
            ]);
    }

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