<?php

namespace App\Filament\Resources;

use App\Filament\Resources\VehicleMasterResource\Pages;
use App\Filament\Resources\VehicleMasterResource\RelationManagers;
use App\Models\VehicleMaster;
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;

class VehicleMasterResource extends Resource
{
    protected static ?string $model = VehicleMaster::class;

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

    protected static ?string $navigationGroup = 'Transport Tracking';


    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('driver_master_id')
                    ->label('Name')
                    ->relationship('driverMaster', 'name')
                    ->required(),
                Forms\Components\Select::make('tracking_device_master_id')
                    ->label('Status')
                    ->relationship('trackingDeviceMaster', 'status'),
                Forms\Components\TextInput::make('registration_number')
                    ->required(),
                Forms\Components\TextInput::make('model')
                    ->required(),
                Forms\Components\Hidden::make('created_by')
                    ->default(Filament::auth()->user()?->name)
                    ->required(),
                Forms\Components\Hidden::make('updated_by')
                    ->label('Updated By'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('No.')
                    ->label('No.')
                    ->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
                        $paginator = $livewire->getTableRecords();
                        $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
                        $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
                        return ($currentPage - 1) * $perPage + $rowLoop->iteration;
                    }),
                Tables\Columns\TextColumn::make('driverMaster.name')
                    ->label('Name')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('trackingDeviceMaster.status')
                    ->label('Status')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('registration_number')
                    ->label('Registration Number')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('model')
                    ->label('Model')
                    ->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\ListVehicleMasters::route('/'),
            'create' => Pages\CreateVehicleMaster::route('/create'),
            'view' => Pages\ViewVehicleMaster::route('/{record}'),
            'edit' => Pages\EditVehicleMaster::route('/{record}/edit'),
        ];
    }

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