<?php

namespace App\Filament\Resources;

use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use App\Models\StudentTask;
use Filament\Resources\Resource;
use Filament\Forms\Components\Card;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\FileUpload;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\StudentTaskResource\Pages;
use App\Filament\Resources\StudentTaskResource\RelationManagers;
use Filament\Tables\Filters\SelectFilter;

class StudentTaskResource extends Resource
{
    protected static ?string $model = StudentTask::class;

    protected static ?string $navigationIcon = 'heroicon-o-clipboard-document';
    protected static ?string $navigationLabel = 'Tugas Siswa';
    protected static ?string $pluralLabel = 'Tugas Siswa';
    protected static ?string $modelLabel = 'Tugas Siswa';
    protected static ?string $slug = 'tugas-siswa';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Card::make()
                    ->schema([
                        TextInput::make('name')
                            ->label('Nama Tugas')
                            ->required()
                            ->placeholder('Masukan nama tugas'),

                        Textarea::make('description')
                            ->label('Deskripsi Tugas')
                            ->required()
                            ->placeholder('Masukan deskripsi tugas'),

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

                        Select::make('subject_id')
                            ->label('Mata Pelajaran')
                            ->relationship('subject', 'name')
                            ->required()
                            ->placeholder('Pilih mata pelajaran'),

                        FileUpload::make('file')
                            ->label('File Tugas')
                            ->disk('public')
                            ->directory('tugas-siswa')
                            ->acceptedFileTypes(['application/pdf', 'image/*', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'video/*'])
                            ->nullable()
                            ->placeholder('Unggah file tugas (opsional)')
                    ])
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Nama Tugas')
                    ->searchable()
                    ->sortable(),

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

                TextColumn::make('subject.name')
                    ->label('Mata Pelajaran')
                    ->badge()
                    ->colors(['primary'])
                    ->searchable()
                    ->sortable(),
            ])
            ->filters([
                SelectFilter::make('classroom_id')
                    ->label('Kelas')
                    ->relationship('classroom', 'name')
                    ->placeholder('Semua Kelas'),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            StudentTaskResource\RelationManagers\StudentGradeRelationManager::class,
        ];
    }

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