<?php

namespace App\Filament\Resources;

use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use App\Models\StudentGrowth;
use Filament\Resources\Resource;
use Filament\Forms\Components\Card;
use Illuminate\Support\Facades\Auth;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\StudentGrowthResource\Pages;
use App\Filament\Resources\StudentGrowthResource\RelationManagers;
use Filament\Tables\Filters\SelectFilter;

class StudentGrowthResource extends Resource
{
    protected static ?string $model = StudentGrowth::class;

    protected static ?string $navigationIcon = 'heroicon-o-presentation-chart-line';
    protected static ?string $navigationLabel = 'Perkembangan Siswa';
    protected static ?string $pluralLabel = 'Perkembangan Siswa';
    protected static ?string $modelLabel = 'Perkembangan Siswa';
    protected static ?string $slug = 'perkembangan-siswa';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Card::make()
                    ->schema([
                        Select::make('user_id')
                            ->label('Guru')
                            ->relationship('user', 'name')
                            ->required()
                            ->default(Auth::id())
                            ->disabled()
                            ->preload(),

                        Select::make('student_id')
                            ->label('Siswa')
                            ->relationship('student', 'name')
                            ->required()
                            ->preload(),

                        TextInput::make('incident')
                            ->label('Kejadian')
                            ->placeholder('Deskripsikan kejadian yang terjadi')
                            ->required()
                            ->maxLength(255),

                        TextInput::make('behavior')
                            ->label('Perilaku')
                            ->placeholder('Deskripsikan perilaku siswa terkait kejadian')
                            ->required()
                            ->maxLength(255),

                        TextInput::make('suggestion')
                            ->label('Saran')
                            ->placeholder('Berikan saran atau tindakan yang perlu diambil')
                            ->required()
                            ->maxLength(255),
                    ])
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('student.name')
                    ->label('Siswa')
                    ->sortable()
                    ->badge()
                    ->colors(['primary'])
                    ->searchable(),

                TextColumn::make('user.name')
                    ->label('Mengetahui')
                    ->sortable()
                    ->badge()
                    ->colors(['success'])
                    ->searchable(),

                TextColumn::make('incident')
                    ->label('Kejadian & Perilaku')
                    ->getStateUsing(function ($record) {
                        return "Kejadian : {$record->incident}\nPerilaku : {$record->behavior}";
                    })
                    ->formatStateUsing(fn(string $state) => nl2br(e($state)))
                    ->html()
                    ->sortable()
                    ->searchable(['incident', 'behavior']),
                

                TextColumn::make('suggestion')
                    ->label('Saran')
                    ->sortable()
                    ->searchable(),
            ])
            ->filters([
                SelectFilter::make('student_id')
                    ->label('Siswa')
                    ->relationship('student', 'name')
                    ->multiple()
                    ->preload(),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\Action::make('download_pdf')
                ->label('Cetak')
                ->icon('heroicon-o-printer')
                ->url(fn ($record) => route('student-growth.download', ['student' => $record->student_id]))
                ->color('warning')
                ->requiresConfirmation(),
                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\ListStudentGrowths::route('/'),
            'create' => Pages\CreateStudentGrowth::route('/create'),
            'edit' => Pages\EditStudentGrowth::route('/{record}/edit'),
        ];
    }
}
