<?php

namespace App\Filament\Resources;

use App\Filament\Resources\QuestionResource\Pages;
use App\Jobs\GenerateSolutionJob;
use App\Jobs\GenerateSvgJob;
use App\Jobs\MatchKnowledgeJob;
use App\Models\Question;
use BackedEnum;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
use Filament\Schemas\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;
use UnitEnum;

class QuestionResource extends Resource
{
    protected static bool $shouldRegisterNavigation = false;
    protected static ?string $model = Question::class;

    protected static ?string $navigationLabel = '正式题库';

    protected static ?string $modelLabel = '题目';

    protected static ?string $pluralModelLabel = '题目';

    protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static UnitEnum|string|null $navigationGroup = '题库管理';

    protected static ?int $navigationSort = 1;

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Section::make('题目内容')
                    ->schema([
                        TextInput::make('question_code')->required()->maxLength(128),
                        Select::make('question_type')
                            ->options([
                                'choice' => '选择题',
                                'fill' => '填空题',
                                'answer' => '解答题',
                            ])
                            ->required(),
                        Textarea::make('stem')->required()->rows(6),
                        Textarea::make('answer')->rows(3),
                        Textarea::make('solution')->rows(6),
                        TextInput::make('difficulty')->numeric()->minValue(1)->maxValue(5),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Tables\Table $table): Tables\Table
    {
        return $table
            ->columns([
                TextColumn::make('question_code')->label('编码')->searchable(),
                TextColumn::make('question_type')->label('题型')->sortable(),
                TextColumn::make('stem')->label('题干')->limit(60)->wrap()->searchable(),
                TextColumn::make('difficulty')->label('难度')->sortable(),
                TextColumn::make('updated_at')->label('更新')->dateTime(),
            ])
            ->filters([
                SelectFilter::make('question_type')
                    ->options([
                        'choice' => '选择题',
                        'fill' => '填空题',
                        'answer' => '解答题',
                    ]),
                Filter::make('difficulty_range')
                    ->form([
                        TextInput::make('min')->numeric(),
                        TextInput::make('max')->numeric(),
                    ])
                    ->query(function (Builder $query, array $data) {
                        if ($data['min'] !== null && $data['max'] !== null) {
                            $query->whereBetween('difficulty', [$data['min'], $data['max']]);
                        }
                    }),
            ])
            ->actions([
                ViewAction::make(),
                EditAction::make(),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    BulkAction::make('queue_solution')
                        ->label('生成解析')
                        ->action(fn ($records) => $records->each(fn ($record) => GenerateSolutionJob::dispatch($record->id))),
                    BulkAction::make('match_knowledge')
                        ->label('匹配知识点')
                        ->action(fn ($records) => $records->each(fn ($record) => MatchKnowledgeJob::dispatch($record->id))),
                    BulkAction::make('generate_svg')
                        ->label('生成SVG')
                        ->action(fn ($records) => $records->each(fn ($record) => GenerateSvgJob::dispatch($record->id))),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListQuestions::route('/'),
            'create' => Pages\CreateQuestion::route('/create'),
            'view' => Pages\ViewQuestion::route('/{record}'),
            'edit' => Pages\EditQuestion::route('/{record}/edit'),
        ];
    }
}
