<?php

namespace App\Filament\Resources;

use App\Filament\Resources\QuestionAssetResource\Pages;
use App\Models\QuestionAsset;
use BackedEnum;
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\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use UnitEnum;

class QuestionAssetResource extends Resource
{
    protected static ?string $model = QuestionAsset::class;

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

    protected static ?string $navigationLabel = '素材管理';

    protected static ?string $modelLabel = '素材';

    protected static ?string $pluralModelLabel = '素材';

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

    protected static ?int $navigationSort = 5;

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Section::make('素材信息')
                    ->schema([
                        TextInput::make('question_id')->numeric()->required(),
                        Select::make('asset_type')
                            ->options([
                                'image' => '图片',
                                'svg' => 'SVG',
                                'latex' => 'LaTeX',
                            ])
                            ->required(),
                        TextInput::make('path')->required()->maxLength(255),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Tables\Table $table): Tables\Table
    {
        return $table
            ->columns([
                TextColumn::make('id')->label('ID')->sortable(),
                TextColumn::make('question_id')->label('题目ID')->sortable(),
                TextColumn::make('asset_type')->label('类型')->sortable(),
                TextColumn::make('path')->label('路径')->limit(60)->wrap(),
                TextColumn::make('updated_at')->label('更新')->dateTime(),
            ])
            ->filters([
                SelectFilter::make('asset_type')
                    ->options([
                        'image' => '图片',
                        'svg' => 'SVG',
                        'latex' => 'LaTeX',
                    ]),
            ])
            ->actions([
                ViewAction::make(),
                EditAction::make(),
            ])
            ->bulkActions([
                BulkActionGroup::make([]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListQuestionAssets::route('/'),
            'create' => Pages\CreateQuestionAsset::route('/create'),
            'view' => Pages\ViewQuestionAsset::route('/{record}'),
            'edit' => Pages\EditQuestionAsset::route('/{record}/edit'),
        ];
    }
}
