<?php

namespace App\Filament\Resources;

use Dom\Text;
use Filament\Forms;
use Filament\Tables;
use App\Models\Student;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Forms\Components\Card;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\ImageColumn;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\FileUpload;
use Illuminate\Database\Eloquent\Builder;
use App\Filament\Resources\StudentResource\Pages;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\StudentResource\RelationManagers;

class StudentResource extends Resource
{
    protected static ?string $model = Student::class;

    protected static ?string $navigationIcon = 'heroicon-o-user-group';
    protected static ?string $navigationLabel = 'Siswa';
    protected static ?string $pluralLabel = 'Siswa';
    protected static ?string $modelLabel = 'Siswa';
    protected static ?string $slug = 'siswa';
    
    public static function getNavigationBadge(): ?string
    {
        return static::getModel()::count();
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Card::make()
                    ->schema([
                        FileUpload::make('avatar_url')
                            ->label('Foto Siswa')
                            ->image()
                            ->disk('public')
                            ->directory('student')
                            ->imageCropAspectRatio('1:1')
                            ->imageEditor()
                            ->placeholder('Unggah foto siswa (opsional)')
                            ->acceptedFileTypes(['image/*'])
                            ->columnSpanFull(),

                        TextInput::make('nis')
                            ->label('NIS')
                            ->required()
                            ->placeholder('Masukkan NIS, max : 16 karakter')
                            ->unique(ignoreRecord: true)
                            ->numeric()
                            ->maxLength(16),

                        TextInput::make('name')
                            ->label('Nama')
                            ->required()
                            ->placeholder('Masukkan nama lengkap siswa')
                            ->maxLength(255),

                        DatePicker::make('date_of_birth')
                            ->label('Tanggal Lahir')
                            ->required()
                            ->placeholder('Pilih tanggal lahir siswa'),

                        TextInput::make('place_of_birth')
                            ->label('Tempat Lahir')
                            ->required()
                            ->placeholder('Masukkan tempat lahir siswa')
                            ->maxLength(255),

                        Select::make('classroom_id')
                            ->label('Kelas')
                            ->relationship('classroom', 'name')
                            ->required()
                            ->placeholder('Pilih kelas siswa'),

                        Select::make('user_id')
                            ->label('Orang Tua / Wali')
                            ->relationship('user', 'name')
                            ->required()
                            ->unique(ignoreRecord: true)
                            ->placeholder('Pilih akun pengguna untuk siswa'),
                    ])
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('avatar_url')
                    ->label('')
                    ->circular()
                    ->size(50),

                TextColumn::make('nis')
                    ->label('NIS')
                    ->sortable()
                    ->searchable(),

                TextColumn::make('name')
                    ->label('Siswa')
                    ->sortable()
                    ->searchable()
                    ->wrap(),

                TextColumn::make('classroom.name')
                    ->label('Kelas')
                    ->badge()
                    ->colors(['primary'])
                    ->sortable()
                    ->searchable(),

                TextColumn::make('user.name')
                    ->label('Orang Tua / Wali')
                    ->sortable()
                    ->searchable(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListStudents::route('/'),
            'create' => Pages\CreateStudent::route('/create'),
            'edit' => Pages\EditStudent::route('/{record}/edit'),
        ];
    }
}
