<?php

namespace App\Filament\Resources;

use App\Filament\Exports\PumpTestingEntryExporter;
use App\Filament\Resources\PumpTestingEntryResource\Pages;
use App\Models\Configuration;
use App\Models\Item;
use App\Models\Plant;
use App\Models\PumpTestingEntry;
use App\Models\PumpTestingMaster;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Validation\Rule;

class PumpTestingEntryResource extends Resource
{
    protected static ?string $model = PumpTestingEntry::class;

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

    protected static ?string $navigationGroup = 'Pump Testing Panel';

    protected static ?int $navigationSort = 2;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('')
                    ->schema([
                        Forms\Components\Select::make('plant_id')
                            ->label('Plant Name')
                            ->relationship('plant', 'name')
                            ->options(function (callable $get) {
                                $userHas = Filament::auth()->user()->plant_id;

                                return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
                            })
                            ->columnSpan(1) // (['default' => 1, 'sm' => 2])
                            ->required()
                            ->searchable()
                            ->reactive()
                            ->default(function () {
                                $userHas = Filament::auth()->user()->plant_id;

                                return ($userHas && strlen($userHas) > 0) ? $userHas : optional(PumpTestingEntry::latest()->first())->plant_id ?? null;
                            })
                            ->disabled(fn (Get $get) => ! empty($get('id')))
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $plantId = $get('plant_id');
                                $set('pump_testing_master_id', null);
                                $set('pump_master_id', null);
                                // $set('pump_type', null);

                                if (! $plantId) {
                                    $set('pTeError', 'Please select a plant first.');

                                    return;
                                } else {
                                    $set('pTeError', null);
                                }

                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->extraAttributes(fn ($get) => [
                                'class' => $get('pTeError') ? 'border-red-500' : '',
                            ])
                            ->hint(fn ($get) => $get('pTeError') ? $get('pTeError') : null)
                            ->hintColor('danger'),
                        Forms\Components\Select::make('tested_type')
                            ->label('Tested Type')
                            ->selectablePlaceholder(false)
                            ->options(function (callable $get) {
                                $plantId = $get('plant_id');
                                if ($plantId) {
                                    return Configuration::where('plant_id', $plantId)
                                        ->where('c_name', 'PUMP_DISPLAY_TEST_TYPE')
                                        ->whereNot('c_value', 'Routine Test')
                                        ->orderBy('created_at')
                                        ->pluck('c_value', 'c_value')
                                        ->toArray();
                                } else {
                                    return Configuration::where('c_name', 'PUMP_DISPLAY_TEST_TYPE')
                                        ->whereNot('c_value', 'Routine Test')
                                        ->orderBy('created_at')
                                        ->pluck('c_value', 'c_value')
                                        ->toArray();
                                }
                            })
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->default('Pump Performance Test')
                            ->reactive(),
                        Forms\Components\DatePicker::make('tested_date')
                            ->label('Tested Date')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->default(now())
                            ->required()
                            ->reactive(),
                        Forms\Components\TextInput::make('correction_head')
                            ->label('Correction Head')
                            ->placeholder('Scan the correction head')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\Hidden::make('pump_testing_master_id')
                            // ->relationship('pumpTestingMasters', 'id')
                            ->required(),
                        Forms\Components\Select::make('pump_master_id')
                            ->label('Pump Code')
                            // ->relationship('pumpTestingMasters', 'id')
                            ->options(function (callable $get) {
                                $plantId = $get('plant_id');
                                if (! $plantId) {
                                    return [];
                                }

                                return Item::where('plant_id', $plantId)->whereHas('pumpTestingMasters')->orderBy('code')->pluck('code', 'code');
                            })
                            ->columnSpan(1)
                            ->required()
                            ->searchable()
                            ->reactive()
                            ->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
                                $plantId = $get('plant_id'); // Get selected plant
                                $set('pump_testing_master_id', null);
                                // $set('pump_type', null);
                                if (! $plantId) {
                                    $set('pump_master_id', null);
                                } elseif ($state) {
                                    // $pumpMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $state)->first();
                                    $pumpMaster = PumpTestingMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($state) {
                                        $query->where('code', $state);
                                    })->first();

                                    if (! $pumpMaster) {
                                        $set('pump_master_id', null);
                                    } else {
                                        $set('pump_testing_master_id', $pumpMaster->id);
                                        // $set('pump_type', $pumpMaster->item->description ?? null);
                                    }
                                }

                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->afterStateHydrated(function ($component, $state, Get $get, Set $set) {
                                if ($get('id')) {
                                    $itemId = PumpTestingMaster::where('id', $get('pump_testing_master_id'))->first()?->item_id;
                                    if ($itemId) {
                                        $item = Item::where('id', $itemId)->first()?->code;
                                        if ($item) {
                                            $set('pump_master_id', $item);
                                        } else {
                                            $set('pump_master_id', null);
                                        }
                                    } else {
                                        $set('pump_master_id', null);
                                    }
                                }
                            }),
                        // Forms\Components\TextInput::make('pump_type')
                        //     ->label('Pump Type]')
                        //     ->placeholder('Choose the pump code first')
                        //     ->readOnly()
                        //     ->afterStateUpdated(function ($state, callable $set) {
                        //         $set('updated_by', Filament::auth()->user()?->name);
                        //     })
                        //     ->columnSpan(1)
                        //     ->required()
                        //     ->reactive(),
                        Forms\Components\TextInput::make('pump_serial_number')
                            ->label('Pump Serial Number')
                            ->placeholder('Scan the pump serial number')
                            ->rule(function (callable $get) {
                                return Rule::unique('pump_testing_entries', 'pump_serial_number')
                                    ->where('plant_id', $get('plant_id'))
                                    ->where('pump_testing_master_id', $get('pump_testing_master_id'))
                                    ->where('tested_type', $get('tested_type'))
                                    ->where('sl_no', $get('sl_no'))
                                    ->ignore($get('id'));
                            })
                            ->afterStateUpdated(function ($state, callable $set) {

                                if (strpos($state, '|') != false) {
                                    $parts = explode('|', $state);
                                    $set('pump_serial_number', $parts[1]);
                                }

                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\TextInput::make('sl_no')
                            ->label('Sl. No.')
                            ->placeholder('Scan the sl. no.')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\TextInput::make('voltage')
                            ->label('Voltage')
                            ->placeholder('Scan the voltage')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\TextInput::make('frequency')
                            ->label('Frequency')
                            ->placeholder('Scan the frequency')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\TextInput::make('speed')
                            ->label('Speed')
                            ->placeholder('Scan the speed')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\TextInput::make('head')
                            ->label('Head')
                            ->placeholder('Scan the head')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\TextInput::make('flow_reading')
                            ->label('Flow Reading')
                            ->placeholder('Scan the flow reading')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\TextInput::make('current')
                            ->label('Current')
                            ->placeholder('Scan the current')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\TextInput::make('watt')
                            ->label('Watt')
                            ->placeholder('Scan the watt')
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('updated_by', Filament::auth()->user()?->name);
                            })
                            ->columnSpan(1)
                            ->required()
                            ->reactive(),
                        Forms\Components\Hidden::make('created_by')
                            ->default(fn () => Filament::auth()->user()?->name)
                            ->columnSpan(1)
                            ->required(),
                        Forms\Components\Hidden::make('updated_by')
                            ->default(fn () => Filament::auth()->user()?->name)
                            ->columnSpan(1)
                            ->required(),
                        Forms\Components\TextInput::make('id')
                            ->hidden()
                            ->columnSpan(1)
                            ->readOnly(),
                    ])
                    ->columns(['default' => 1, 'sm' => 2]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                // Tables\Columns\TextColumn::make('id')
                //     ->label('ID')
                //     ->numeric()
                //     ->sortable(),
                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('plant.name')
                    ->label('Plant Name')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('tested_date')
                    ->label('Tested Date')
                    ->date() // 'F d, Y'
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('tested_type')
                    ->label('Tested Type')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('pumpTestingMasters.item.code')
                    ->label('Pump Code')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('pumpTestingMasters.item.category')
                    ->label('Pump Category')
                    ->default('-')
                    ->searchable()
                    ->alignCenter()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('pumpTestingMasters.item.description')
                    ->label('Pump Type')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('pumpTestingMasters.item.uom')
                    ->label('Unit Of Measure')
                    ->default('-')
                    ->searchable()
                    ->alignCenter()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('pump_serial_number')
                    ->label('Pump Serial Number')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('correction_head')
                    ->label('Correction Head')
                    ->searchable()
                    ->alignCenter(),
                // ->sortable(),
                Tables\Columns\TextColumn::make('sl_no')
                    ->label('Sl. No.')
                    ->searchable()
                    ->alignCenter(),
                // ->sortable(),
                Tables\Columns\TextColumn::make('voltage')
                    ->label('Voltage')
                    ->searchable()
                    ->alignCenter(),
                // ->sortable(),
                Tables\Columns\TextColumn::make('frequency')
                    ->label('Frequency')
                    ->searchable()
                    ->alignCenter(),
                // ->sortable(),
                Tables\Columns\TextColumn::make('speed')
                    ->label('Speed')
                    ->searchable()
                    ->alignCenter(),
                // ->sortable(),
                Tables\Columns\TextColumn::make('head')
                    ->label('Head')
                    ->searchable()
                    ->alignCenter(),
                // ->sortable(),
                Tables\Columns\TextColumn::make('flow_reading')
                    ->label('Flow Reading')
                    ->searchable()
                    ->alignCenter(),
                // ->sortable(),
                Tables\Columns\TextColumn::make('current')
                    ->label('Current')
                    ->searchable()
                    ->alignCenter(),
                // ->sortable(),
                Tables\Columns\TextColumn::make('watt')
                    ->label('Watt')
                    ->searchable()
                    ->alignCenter(),
                // ->sortable(),
                Tables\Columns\TextColumn::make('created_by')
                    ->label('Created By')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Created At')
                    ->dateTime()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('updated_by')
                    ->label('Updated By')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('updated_at')
                    ->label('Updated At')
                    ->dateTime()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('deleted_at')
                    ->label('Deleted At')
                    ->dateTime()
                    ->alignCenter()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
                Filter::make('advanced_filters')
                    ->label('Advanced Filters')
                    ->form([
                        Select::make('Plant')
                            ->label('Search by Plant Name')
                            ->nullable()
                            ->searchable()
                            // ->options(function () {
                                //     return Plant::pluck('name', 'id');
                                // })
                            ->reactive()
                            ->options(function (callable $get) {
                                $userHas = Filament::auth()->user()->plant_id;

                                if ($userHas && strlen($userHas) > 0) {
                                    return Plant::where('id', $userHas)->pluck('name', 'id')->toArray();
                                } else {
                                    return Plant::whereHas('pumpTestingEntries', function ($query) {
                                        $query->whereNotNull('id');
                                    })->orderBy('code')->pluck('name', 'id');
                                }

                                // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
                            })
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $set('Item', null);
                                $set('tested_type', null);
                                $set('created_by', null);
                                $set('updated_by', null);
                            }),
                        DatePicker::make('tested_from')
                            ->label('Tested From')
                            ->placeholder('Select From Date')
                            ->reactive()
                            ->native(false),
                        DatePicker::make('tested_to')
                            ->label('Tested To')
                            ->placeholder('Select To Date')
                            ->reactive()
                            ->native(false),
                        Select::make('tested_type')
                            ->label('Search by Tested Type')
                            ->nullable()
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');
                                if (! $plantId) {
                                    return PumpTestingEntry::whereNotNull('tested_type')->select('tested_type')->distinct()->pluck('tested_type', 'tested_type');
                                } else {
                                    return PumpTestingEntry::where('plant_id', $plantId)->whereNotNull('tested_type')->select('tested_type')->distinct()->pluck('tested_type', 'tested_type');
                                }
                            })
                            ->searchable()
                            ->reactive(),
                        Select::make('Item')
                            ->label('Search by Pump Code')
                            ->nullable()
                            ->options(function (callable $get) {
                                $pId = $get('Plant');

                                if (! $pId) {
                                    return [];
                                } else {
                                    return Item::whereHas('pumpTestingMasters', function ($query) use ($pId) {
                                        if ($pId) {
                                            $query->where('plant_id', $pId);
                                        }
                                        $query->whereHas('pumpTestingEntries');
                                    })->pluck('code', 'id');
                                }
                            })
                            ->searchable()
                            ->reactive(),
                        TextInput::make('description')
                            ->label('Pump Type')
                            ->placeholder('Enter the Pump Type'),
                        TextInput::make('pump_serial_number')
                            ->label('Pump Serial Number')
                            ->placeholder('Enter the Pump Serial Number'),
                        Select::make('created_by')
                            ->label('Search by Created By')
                            ->nullable()
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');
                                if (! $plantId) {
                                    return PumpTestingEntry::whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by');
                                } else {
                                    return PumpTestingEntry::where('plant_id', $plantId)->whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by');
                                }
                            })
                            ->searchable()
                            ->reactive(),
                        DateTimePicker::make(name: 'created_from')
                            ->label('Created From')
                            ->placeholder(placeholder: 'Select From DateTime')
                            ->reactive()
                            ->native(false),
                        DateTimePicker::make('created_to')
                            ->label('Created To')
                            ->placeholder(placeholder: 'Select To DateTime')
                            ->reactive()
                            ->native(false),
                        Select::make('updated_by')
                            ->label('Search by Updated By')
                            ->nullable()
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');
                                if (! $plantId) {
                                    return PumpTestingEntry::whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by');
                                } else {
                                    return PumpTestingEntry::where('plant_id', $plantId)->whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by');
                                }
                            })
                            ->searchable()
                            ->reactive(),
                        DateTimePicker::make(name: 'updated_from')
                            ->label('Updated From')
                            ->placeholder(placeholder: 'Select From DateTime')
                            ->reactive()
                            ->native(false),
                        DateTimePicker::make('updated_to')
                            ->label('Updated To')
                            ->placeholder(placeholder: 'Select To DateTime')
                            ->reactive()
                            ->native(false),
                    ])
                    ->query(function ($query, array $data) {
                        // Hide all records initially if no filters are applied
                        if (empty($data['Plant']) && empty($data['tested_from']) && empty($data['tested_to']) && empty($data['tested_type']) && empty($data['Item']) && empty($data['description']) && empty($data['pump_serial_number']) && empty($data['created_by']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['updated_by']) && empty($data['updated_from']) && empty($data['updated_to'])) {// || $data['isi_type'] == 'All')
                            return $query->whereRaw('1 = 0');
                        }

                        if (! empty($data['Plant'])) {
                            $query->where('plant_id', $data['Plant']);
                        } else {
                            $userHas = Filament::auth()->user()->plant_id;

                            if ($userHas && strlen($userHas) > 0) {
                                return $query->whereRaw('1 = 0');
                            }
                        }

                        if (! empty($data['tested_from'])) {
                            $query->where('tested_date', '>=', $data['tested_from']);
                        }

                        if (! empty($data['tested_to'])) {
                            $query->where('tested_date', '<=', $data['tested_to']);
                        }

                        if (! empty($data['tested_type'])) {
                            $query->where('tested_type', $data['tested_type']);
                        }

                        if (! empty($data['Item']) && ! empty($data['Plant'])) {
                            $pumpMasterIds = PumpTestingMaster::where('item_id', $data['Item'])
                                ->where('plant_id', $data['Plant'])
                                ->pluck('id')
                                ->toArray();

                            if (! empty($pumpMasterIds)) {
                                $query->whereIn('pump_testing_master_id', $pumpMasterIds);
                            }
                        }

                        if (! empty($data['description'])) {
                            $pId = $data['Plant'] ?? null;
                            $descIds = Item::where('description', 'like', '%'.$data['description'].'%')->whereHas('pumpTestingMasters', function ($query) use ($pId) {
                                if ($pId) {
                                    $query->where('plant_id', $pId);
                                }
                            })->pluck('id')->toArray();

                            if (! empty($descIds)) {
                                $pumpMasterIds = PumpTestingMaster::whereIn('item_id', $descIds)
                                    ->where('plant_id', $data['Plant'])
                                    ->pluck('id')
                                    ->toArray();

                                if (! empty($pumpMasterIds)) {
                                    $query->whereIn('pump_testing_master_id', $pumpMasterIds);
                                }
                            }
                        }

                        if (! empty($data['pump_serial_number'])) {
                            $query->where('pump_serial_number', 'like', '%'.$data['pump_serial_number'].'%');
                        }

                        if (! empty($data['created_by'])) {
                            $query->where('created_by', $data['created_by']);
                        }

                        if (! empty($data['created_from'])) {
                            $query->where('created_at', '>=', $data['created_from']);
                        }

                        if (! empty($data['created_to'])) {
                            $query->where('created_at', '<=', $data['created_to']);
                        }

                        if (! empty($data['updated_by'])) {
                            $query->where('updated_by', $data['updated_by']);
                        }

                        if (! empty($data['updated_from'])) {
                            $query->where('updated_at', '>=', $data['updated_from']);
                        }

                        if (! empty($data['updated_to'])) {
                            $query->where('updated_at', '<=', $data['updated_to']);
                        }
                    })
                    ->indicateUsing(function (array $data) {
                        $indicators = [];

                        if (! empty($data['Plant'])) {
                            $indicators[] = 'Plant Name: '.Plant::where('id', $data['Plant'])->value('name');
                        } else {
                            $userHas = Filament::auth()->user()->plant_id;

                            if ($userHas && strlen($userHas) > 0) {
                                return 'Plant: Choose plant to filter records.';
                            }
                        }

                        if (! empty($data['tested_from'])) {
                            $indicators[] = 'Tested From: '.$data['tested_from'];
                        }

                        if (! empty($data['tested_to'])) {
                            $indicators[] = 'Tested To: '.$data['tested_to'];
                        }

                        if (! empty($data['tested_type'])) {
                            $indicators[] = 'Tested Type: '.$data['tested_type'];
                        }

                        if (! empty($data['Item']) && ! empty($data['Plant'])) {
                            $itemCode = Item::find($data['Item'])->code ?? 'Unknown';
                            $indicators[] = 'Pump Code: '.$itemCode;
                        }

                        if (! empty($data['description'])) {
                            $indicators[] = 'Pump Type: '.$data['description'];
                        }

                        if (! empty($data['pump_serial_number'])) {
                            $indicators[] = 'Pump Serial Number: '.$data['pump_serial_number'];
                        }

                        if (! empty($data['created_by'])) {
                            $indicators[] = 'Created By: '.$data['created_by'];
                        }

                        if (! empty($data['created_from'])) {
                            $indicators[] = 'Created From: '.$data['created_from'];
                        }

                        if (! empty($data['created_to'])) {
                            $indicators[] = 'Created To: '.$data['created_to'];
                        }

                        if (! empty($data['updated_by'])) {
                            $indicators[] = 'Updated By: '.$data['updated_by'];
                        }

                        if (! empty($data['updated_from'])) {
                            $indicators[] = 'Updated From: '.$data['updated_from'];
                        }

                        if (! empty($data['updated_to'])) {
                            $indicators[] = 'Updated To: '.$data['updated_to'];
                        }

                        return $indicators;
                    }),
            ])
            ->filtersFormMaxHeight('280px')
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
            ])
            ->headerActions([
                ExportAction::make()
                    ->label('Export Pump Testing Entries')
                    ->color('warning')
                    ->exporter(PumpTestingEntryExporter::class)
                    ->visible(function () {
                        return Filament::auth()->user()->can('view export pump testing entries');
                    }),
            ])
            ->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\ListPumpTestingEntries::route('/'),
            'create' => Pages\CreatePumpTestingEntry::route('/create'),
            'view' => Pages\ViewPumpTestingEntry::route('/{record}'),
            'edit' => Pages\EditPumpTestingEntry::route('/{record}/edit'),
        ];
    }

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