<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ModuleListResource\Pages;
use App\Filament\Resources\ModuleListResource\RelationManagers;
use App\Models\ModuleList;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Components\Section;

class ModuleListResource extends Resource
{
    protected static ?string $model = ModuleList::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static ?string $navigationGroup = 'Modules';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('')
                ->schema([
                    Forms\Components\TextInput::make('module_name')
                        ->label('Module Name')
                        ->required(),
                    Forms\Components\TextInput::make('dashboard_name')
                        ->label('DashBoard Name')
                        ->required(),
                    Forms\Components\TextInput::make('filter_name')
                        ->label('Filter Name')
                        ->required(),
                    Forms\Components\TextInput::make('file_name')
                        ->label('File Name')
                        ->required()
                        ->reactive()
                        ->extraAttributes([
                            'x-data' => '{ value: "" }',
                            'x-model' => 'value',
                            'x-on:keydown.enter.prevent' => '$wire.process(value)',
                        ]),
                    Forms\Components\Hidden::make('created_by')
                        ->label('Created By')
                        ->default(Filament::auth()->user()?->name),
                    Forms\Components\Hidden::make('updated_by')
                        ->label('Updated By')
                        ->default(Filament::auth()->user()?->name),
                ])
               ->columns(4),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')
                    ->label('ID')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('module_name')
                    ->label('Module Name')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('dashboard_name')
                    ->label('DashBoard Name')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('filter_name')
                    ->label('Filter Name')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('deleted_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                    Tables\Actions\ForceDeleteBulkAction::make(),
                    Tables\Actions\RestoreBulkAction::make(),
                ]),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListModuleLists::route('/'),
            'create' => Pages\CreateModuleList::route('/create'),
            'view' => Pages\ViewModuleList::route('/{record}'),
            'edit' => Pages\EditModuleList::route('/{record}/edit'),
        ];
    }

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