<?php

namespace App\Filament\Resources\Vocabularies;

use App\Filament\Resources\Vocabularies\Pages\CreateVocabulary;
use App\Filament\Resources\Vocabularies\Pages\EditVocabulary;
use App\Filament\Resources\Vocabularies\Pages\ListVocabularies;
use App\Filament\Resources\Vocabularies\Schemas\VocabularyForm;
use App\Filament\Resources\Vocabularies\Tables\VocabulariesTable;
use App\Models\Vocabulary;
use BackedEnum;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas\Components as Schemas;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables;
use Filament\Tables\Table;

class VocabularyResource extends Resource
{
    protected static ?string $model = Vocabulary::class;

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

    protected static ?int $navigationSort = 4;

    protected static ?string $recordTitleAttribute = 'word';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Schemas\Section::make('Core Info')
                    ->schema([
                        Forms\Components\TextInput::make('word')
                            ->label('Word (Kanji/Kana)')
                            ->required()
                            ->maxLength(255),
                        Forms\Components\TextInput::make('reading')
                            ->label('Reading (Kana)')
                            ->required()
                            ->maxLength(255),
                        Forms\Components\TextInput::make('romaji')
                            ->maxLength(255),
                        Forms\Components\TextInput::make('meaning_id')
                            ->label('Meaning (Bahasa Indonesia)')
                            ->required()
                            ->maxLength(255),
                        Forms\Components\TextInput::make('meaning_en')
                            ->label('Meaning (English)')
                            ->required()
                            ->maxLength(255),
                    ])->columns(2),

                Schemas\Section::make('Metadata & Media')
                    ->schema([
                        Forms\Components\Select::make('level_id')
                            ->label('JLPT Level')
                            ->relationship('level', 'code')
                            ->searchable()
                            ->preload()
                            ->required(),
                        Forms\Components\Select::make('type')
                            ->options([
                                'noun' => 'Noun',
                                'verb' => 'Verb',
                                'adjective' => 'Adjective',
                                'adverb' => 'Adverb',
                                'particle' => 'Particle',
                                'expression' => 'Expression',
                            ])
                            ->required(),
                        Forms\Components\FileUpload::make('audio_url')
                            ->label('Pronunciation Audio')
                            ->disk('r2')
                            ->directory('audio/vocab')
                            ->visibility('public')
                            ->acceptedFileTypes(['audio/mpeg', 'audio/wav', 'audio/ogg']),
                    ])->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('word')
                    ->label('Word')
                    ->searchable()
                    ->description(fn (Vocabulary $record): string => $record->reading),
                Tables\Columns\TextColumn::make('meaning_id')
                    ->label('Meaning')
                    ->searchable(),
                Tables\Columns\TextColumn::make('level.code')
                    ->label('Level')
                    ->badge()
                    ->color(fn (?string $state): string => match ($state) {
                        'N1' => 'danger',
                        'N2' => 'warning',
                        'N3' => 'info',
                        default => 'success',
                    }),
                Tables\Columns\TextColumn::make('type')
                    ->badge(),
                Tables\Columns\IconColumn::make('audio_url')
                    ->label('Audio')
                    ->icon('heroicon-o-speaker-wave')
                    ->color(fn ($state) => $state ? 'success' : 'gray'),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('level_id')
                    ->label('Level')
                    ->relationship('level', 'code'),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => ListVocabularies::route('/'),
            'create' => CreateVocabulary::route('/create'),
            'edit' => EditVocabulary::route('/{record}/edit'),
        ];
    }
}
