<?php

namespace App\Filament\Resources\StudentTaskResource\RelationManagers;

use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Resources\RelationManagers\RelationManager;

class StudentGradeRelationManager extends RelationManager
{
    protected static string $relationship = 'nilai';
    protected static ?string $pluralLabel = 'Nilai Siswa';
    protected static ?string $modelLabel = 'Nilai Siswa';
    protected static ?string $pluralModelLabel = 'Nilai Siswa';

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Select::make('student_id')
                ->label('Siswa')
                ->options(function () {
                    $task = $this->getOwnerRecord();
            
                    if (!$task || !$task->classroom) {
                        return [];
                    }

                    $students = $task->classroom->students;
                    $studentIdsWithGrades = $task->nilai->pluck('student_id')->toArray();
                    $availableStudents = $students->whereNotIn('id', $studentIdsWithGrades);
                    return $availableStudents->pluck('name', 'id');
                })
                ->placeholder('Pilih siswa')
                ->required(),            

                FileUpload::make('file')
                    ->label('File Tugas')
                    ->placeholder('Unggah file tugas'),

                TextInput::make('grade')
                    ->label('Nilai')
                    ->required()
                    ->numeric()
                    ->placeholder('Masukkan nilai siswa'),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('task_id')
            ->columns([
                Tables\Columns\TextColumn::make('student.name')
                    ->label('Siswa')
                    ->sortable()
                    ->searchable(),

                Tables\Columns\TextColumn::make('task.name')
                    ->label('Tugas')
                    ->sortable()
                    ->searchable(),

                Tables\Columns\TextColumn::make('grade')
                    ->label('Nilai')
                    ->badge()
                    ->colors(['success'])
                    ->sortable()
                    ->searchable(),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\CreateAction::make(),
                Tables\Actions\Action::make('Print PDF')
                    ->label('Cetak PDF')
                    ->icon('heroicon-o-printer')
                    ->color('success')
                    ->url(fn () => route('student-grade.download', ['task' => $this->getOwnerRecord()->id]))
                    ->openUrlInNewTab()
                    ->requiresConfirmation(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }
}
