<?php

namespace App\Filament\Resources\Master\Courses;

use App\Filament\Actions\Cheerful\DeleteAction;
use App\Filament\Actions\Cheerful\EditAction;
use App\Filament\Actions\Cheerful\ForceDeleteAction;
use App\Filament\Actions\Cheerful\RestoreAction;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\TimestampColumns;
use App\Filament\Resources\Master\Courses\Pages\ManageCourses;
use App\Models\Course;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Grouping\Group;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use UnitEnum;

class CourseResource extends Resource
{
    protected static ?string $model = Course::class;

    protected static string|UnitEnum|null $navigationGroup = 'Master';

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;

    protected static ?string $navigationLabel = 'Mata Kuliah';

    protected static ?int $navigationSort = 20;

    protected static ?string $recordTitleAttribute = 'name';

    protected static ?string $slug = 'master/courses';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('code')
                    ->label('Kode')
                    ->placeholder('FSI419')
                    ->autocomplete(false)
                    ->autofocus()
                    ->required()
                    ->maxLength(20)
                    ->unique(ignoreRecord: true),

                TextInput::make('name')
                    ->label('Nama')
                    ->placeholder('Sistem Pendukung Keputusan')
                    ->autocomplete(false)
                    ->required()
                    ->maxLength(100),

                TextInput::make('credit')
                    ->label('SKS')
                    ->placeholder('2')
                    ->autocomplete(false)
                    ->required()
                    ->numeric()
                    ->minValue(1)
                    ->maxValue(6),

                TextInput::make('semester')
                    ->label('Semester')
                    ->placeholder('3')
                    ->autocomplete(false)
                    ->required()
                    ->numeric()
                    ->minValue(1)
                    ->maxValue(8)
                    ->helperText('Semester ke berapa mata kuliah ini diajarkan (1-8).'),

                Select::make('lecturer_id')
                    ->label('Dosen Pengampu')
                    ->placeholder('Pilih Dosen')
                    ->relationship('lecturer', 'full_name')
                    ->preload()
                    ->searchable()
                    ->helperText('Pilih dosen yang mengampu mata kuliah ini.')
                    ->required(),

            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Nama')
                    ->searchable(query: function (Builder $query, string $search) {
                        $query->where('name', 'like', "%{$search}%")
                            ->orWhere('code', 'like', "%{$search}%");
                    })
                    ->sortable()
                    ->description(fn (Course $course) => $course->code),

                TextColumn::make('credit')
                    ->label('SKS')
                    ->sortable(),

                TextColumn::make('lecturer.full_name')
                    ->label('Dosen Pengampu')
                    ->searchable(),

                ...TimestampColumns::make(),
            ])
            ->filters([
                TrashedFilter::make()
                    ->native(false),
            ])
            ->recordActions([
                EditAction::make()
                    ->modalWidth(Width::Large),

                DeleteAction::make(),

                ForceDeleteAction::make(),

                RestoreAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('Mata Kuliah'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::OutlinedRectangleStack)
            ->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
            ->defaultSort('code', 'desc')
            ->defaultGroup('semester')
            ->groups([
                Group::make('semester')
                    ->label('Semester')
                    ->getTitleFromRecordUsing(fn (Course $record): string => 'Semester '.$record->semester)
                    ->collapsible(),
            ])
            ->deferFilters(false)
            ->paginated(false);
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageCourses::route('/'),
        ];
    }

    public static function getRecordRouteBindingEloquentQuery(): Builder
    {
        return parent::getRecordRouteBindingEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
}
