<?php

namespace App\Filament\Resources;

use App\Filament\Exports\ProductCharacteristicsMasterExporter;
use App\Filament\Imports\ProductCharacteristicsMasterImporter;
use App\Filament\Resources\ProductCharacteristicsMasterResource\Pages;
use App\Models\Item;
use App\Models\Line;
use App\Models\Machine;
use App\Models\Plant;
use App\Models\ProductCharacteristicsMaster;
use App\Models\WorkGroupMaster;
use Closure;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Validation\Rule;
use Str;

class ProductCharacteristicsMasterResource extends Resource
{
    protected static ?string $model = ProductCharacteristicsMaster::class;

    protected static ?string $navigationGroup = 'Process Order';

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('plant_id')
                    ->label('Plant Name')
                    ->relationship('plant', 'name')
                    ->searchable()
                    ->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();
                    })
                    ->disabled(fn (Get $get) => ! empty($get('id')))
                    ->default(function () {
                        $userHas = Filament::auth()->user()->plant_id;

                        return ($userHas && strlen($userHas) > 0) ? $userHas : optional(ProductCharacteristicsMaster::latest()->first())->plant_id;
                    })
                    ->afterStateUpdated(function ($state, callable $set) {
                        $set('updated_by', Filament::auth()->user()?->name);
                        $set('item_id', null);
                        $set('line_id', null);
                        $set('work_group_master_id', null);
                        $set('machine_id', null);
                    })
                    ->reactive()
                    ->required(),
                Forms\Components\Select::make('item_id')
                    ->label('Item Code')
                    // ->relationship('item', 'code')
                    ->searchable()
                    ->reactive()
                    ->options(function (callable $get) {
                        $plantId = $get('plant_id');
                        if (empty($plantId)) {
                            return [];
                        }

                        return Item::where('plant_id', $plantId)->pluck('code', 'id');
                    })
                    ->disabled(fn (Get $get) => ! empty($get('id')))
                    ->afterStateUpdated(function ($state, callable $set) {
                        $set('updated_by', Filament::auth()->user()?->name);
                        $set('line_id', null);
                        $set('work_group_master_id', null);
                        $set('machine_id', null);
                    })
                    ->rules([
                        function (callable $get) {
                            return Rule::unique('product_characteristics_masters', 'item_id')
                                ->where('plant_id', $get('plant_id'))
                                ->where('line_id', $get('line_id'))
                                ->where('work_group_master_id', $get('work_group_master_id'))
                                ->where('machine_id', $get('machine_id'))
                                ->where('characteristics_type', $get('characteristics_type'))
                                ->where('name', $get('name'))
                                ->ignore($get('id'));
                        },
                        // function (callable $get): Closure {
                        //     return function (string $attribute, $value, Closure $fail) use ($get) {
                        //         $rework = $get('rework_status');
                        //         if ($value && Str::contains($value, '.') && $rework == 0) {
                        //             $fail("Rework status should be 'Yes' for rework coil number '{$value}'!");
                        //         }
                        //     };
                        // },
                    ])
                    ->required(),
                Forms\Components\Select::make('line_id')
                    ->label('Line Name')
                    ->reactive()
                    ->searchable()
                    ->options(function (callable $get) {
                        $plantId = $get('plant_id');
                        if (empty($plantId)) {
                            return [];
                        }

                        return Line::where('plant_id', $plantId)->pluck('name', 'id');
                    })
                    // ->disabled(fn (Get $get) => ! empty($get('id')))
                    ->afterStateUpdated(function ($state, callable $set, callable $get) {
                        $set('updated_by', Filament::auth()->user()?->name);
                        $set('work_group_master_id', null);
                        $set('machine_id', null);
                    })
                    // ->relationship('line', 'name'),
                    ->required(),
                Forms\Components\Select::make('work_group_master_id')
                    ->label('Group Work Center')
                    ->required()
                    ->reactive()
                    ->searchable()
                    ->options(function (callable $get) {
                        if (! $get('plant_id') || ! $get('line_id')) {
                            return [];
                        }

                        $line = Line::find($get('line_id'));
                        $workGroupIds = [];
                        for ($i = 1; $i <= $line->no_of_operation; $i++) {
                            $column = "work_group{$i}_id";
                            if (! empty($line->$column)) {
                                $workGroupIds[] = $line->$column;
                            }
                        }

                        return WorkGroupMaster::where('plant_id', $get('plant_id'))->whereIn('id', $workGroupIds)->pluck('name', 'id')->toArray();
                    })
                    // ->disabled(fn (Get $get) => ! empty($get('id')))
                    ->afterStateUpdated(function ($state, callable $set, callable $get) {
                        $lineId = $get('line_id');
                        $set('updated_by', Filament::auth()->user()?->name);
                        $set('machine_id', null);
                        if (! $lineId) {
                            $set('mGroupWorkError', 'Please select a line first.');

                            return;
                        } else {
                            // $grpWrkCnr = Line::find($lineId)->group_work_center;
                            // if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1)
                            // {
                            //     $set('mGroupWorkError', 'Please select a group work center line.');
                            //     $set('line_id', null);
                            //     return;
                            // }
                            $set('mGroupWorkError', null);
                        }
                    })
                    ->extraAttributes(fn ($get) => [
                        'class' => $get('mGroupWorkError') ? 'border-red-500' : '',
                    ])
                    // ->dehydrateStateUsing(fn ($state) => null)
                    ->hint(fn ($get) => $get('mGroupWorkError') ? $get('mGroupWorkError') : null)
                    ->hintColor('danger'),
                Forms\Components\Select::make('machine_id')
                    ->label('Work Center')
                    // ->relationship('machine', 'name'),
                    ->reactive()
                    ->searchable()
                    ->options(function (callable $get) {
                        $plantId = $get('plant_id');
                        $lineId = $get('line_id');
                        $workGroupId = $get('work_group_master_id');

                        if (empty($plantId) || empty($lineId) || empty($workGroupId)) {
                            return [];
                        }

                        return Machine::where('plant_id', $plantId)
                            ->where('line_id', $lineId)
                            ->where('work_group_master_id', $workGroupId)
                            ->pluck('work_center', 'id');
                    })
                    // ->disabled(fn (Get $get) => ! empty($get('id')))
                    ->afterStateUpdated(function ($state, callable $set, callable $get) {
                        if (! $get('plant_id') || ! $get('line_id') || ! $get('work_group_master_id')) {
                            $set('machine_id', null);
                        }
                        $set('updated_by', Filament::auth()->user()?->name);
                    })
                    ->required(),
                Forms\Components\Select::make('characteristics_type')
                    ->label('Characteristics Type')
                    ->options([
                        'Product' => 'Product',
                        'Process' => 'Process',
                    ])
                    ->reactive()
                    ->searchable()
                    // ->preload()
                    ->disabled(fn (Get $get) => ! empty($get('id') && ! Filament::auth()->user()->hasRole('Super Admin')))
                    ->afterStateUpdated(function ($state, callable $set) {
                        $set('updated_by', Filament::auth()->user()?->name);
                    })
                    ->required(),
                Forms\Components\TextInput::make('name')
                    ->label('Characteristics Name')
                    ->reactive()
                    ->afterStateUpdated(function ($state, callable $set) {
                        $set('updated_by', Filament::auth()->user()?->name);
                    }),
                Forms\Components\TextInput::make('category')
                    ->label('Category')
                    ->reactive()
                    ->afterStateUpdated(function ($state, callable $set) {
                        $set('updated_by', Filament::auth()->user()?->name);
                    }),
                Forms\Components\Select::make('inspection_type')
                    ->label('Inspection Type')
                    ->searchable()
                    ->options([
                        'Visual' => 'Visual',
                        'Value' => 'Value',
                    ])
                    ->reactive()
                    // ->preload()
                    ->disabled(fn (Get $get) => ! empty($get('id') && ! Filament::auth()->user()->hasRole('Super Admin')))
                    ->afterStateUpdated(function ($state, callable $set) {
                        if ($state == 'Visual') {
                            $set('lower', 0);
                            $set('middle', 0);
                            $set('upper', 0);
                        }
                        $set('updated_by', Filament::auth()->user()?->name);
                    })
                    ->required(),

                // Forms\Components\Select::make('result')
                //     ->label('Visual Type')
                //     ->reactive()
                //     ->options(function (callable $get) {
                //         if ($get('inspection_type') == 'Visual') {
                //             return [
                //                 'Ok' => 'OK',
                //                 'NotOk' => 'Not Ok',
                //             ];
                //         }
                //         return []; //empty options if type ≠ Visual
                //     })
                //     ->afterStateUpdated(function ($state, $set) {
                //         $set('result', $state); // store in form state only
                //         session()->put('temp_result', $state);
                //     })
                //     ->hidden(fn (callable $get) => $get('inspection_type') != 'Visual'),

                Forms\Components\TextInput::make('lower')
                    ->label('Lower')
                    ->numeric()
                    ->reactive()
                    ->default(0.0)
                    ->minValue(0.0)
                    ->maxValue(fn (Get $get) => $get('upper') ?? PHP_INT_MAX)
                    // ->afterStateUpdated(fn ($state, callable $set, callable $get) => $set('middle', ($state + $get('upper')) / 2))
                    ->afterStateUpdated(function ($state, callable $set, callable $get) {
                        $set('middle', ($state + $get('upper')) / 2);
                        $set('updated_by', Filament::auth()->user()?->name);
                    })
                    // ->visible(fn (callable $get) => $get('inspection_type') == 'Value')
                    ->readOnly(fn (callable $get) => $get('inspection_type') != 'Value'),
                Forms\Components\TextInput::make('upper')
                    ->label('Upper')
                    ->numeric()
                    ->reactive()
                    ->default(0.0)
                    ->minValue(fn (Get $get) => $get('lower') ?? 0)
                    ->maxValue(PHP_INT_MAX)
                    // ->afterStateUpdated(fn ($state, callable $set, callable $get) => $set('middle', ($get('lower') + $state) / 2))
                    ->afterStateUpdated(function ($state, callable $set, callable $get) {
                        $set('middle', ($get('lower') + $state) / 2);
                        $set('updated_by', Filament::auth()->user()?->name);
                    })
                    // ->visible(fn (callable $get) => $get('inspection_type') == 'Value')
                    ->readOnly(fn (callable $get) => $get('inspection_type') != 'Value'),
                Forms\Components\TextInput::make('middle')
                    ->label('Middle')
                    ->readOnly()
                    ->numeric()
                    ->reactive()
                    ->default(0.0)
                    // ->rule(function (callable $get) {
                    //     return function (string $attribute, $value, Closure $fail) use ($get) {
                    //         $lower = $get('lower');
                    //         $upper = $get('upper');
                    //         $middle = $value;

                    //         if (! is_null($lower) && ! is_null($upper) && ! is_null($middle)) {
                    //             if ((($lower <= $middle || $middle >= $upper) && $lower != $upper) || (($lower < $middle || $middle > $upper) && $lower == $upper)) {
                    //                 $fail('Middle must be between Lower and Upper (Lower ≤ Middle ≤ Upper).');
                    //             }
                    //         }
                    //     };
                    // })
                    ->dehydrateStateUsing(fn ($state, Get $get) => ($get('lower') + $get('upper')) / 2)
                    ->afterStateUpdated(function ($state, callable $set) {
                        $set('updated_by', Filament::auth()->user()?->name);
                    }),
                // ->visible(fn (callable $get) => $get('inspection_type') == '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),
                Forms\Components\TextInput::make('id')
                    ->hidden()
                    ->readOnly(),
            ]);
    }

    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('plant.name')
                    ->label('Plant Name')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('item.code')
                    ->label('Item Code')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('item.description')
                    ->label('Item Description')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('category')
                    ->label('Category')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('line.name')
                    ->label('Line Name')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('workGroupMaster.name')
                    ->label('Group Work Center')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('machine.work_center')
                    ->label('Work Center')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('characteristics_type')
                    ->label('Characteristics Type')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('name')
                    ->label('Characteristics Name')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('inspection_type')
                    ->label('Inspection Type')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('lower')
                    ->label('Lower')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('middle')
                    ->label('Middle')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('upper')
                    ->label('Upper')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Created At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_by')
                    ->label('Created By')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('updated_at')
                    ->label('Updated At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_by')
                    ->label('Updated By')
                    ->alignCenter()
                    ->searchable()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('deleted_at')
                    ->label('Deleted At')
                    ->alignCenter()
                    ->dateTime()
                    ->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()
                            ->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('productCharacteristicsMasters', 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('Line', null);
                                $set('work_group_master', null);
                                $set('Machine', null);
                            }),
                        Select::make('Line')
                            ->label('Search by Line Name')
                            ->nullable()
                            ->searchable()
                            ->reactive()
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');

                                if (empty($plantId)) {
                                    return [];
                                }

                                return Line::whereHas('productCharacteristicsMasters', function ($query) use ($plantId) {
                                    if ($plantId) {
                                        $query->where('plant_id', $plantId);
                                    }
                                })->pluck('name', 'id');
                            })
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $set('work_group_master', null);
                                $set('Machine', null);
                            }),
                        Select::make('Item')
                            ->label('Search by Item Code')
                            ->nullable()
                            ->searchable()
                            ->reactive()
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');

                                if (empty($plantId)) {
                                    return [];
                                }

                                return Item::whereHas('productCharacteristicsMasters', function ($query) use ($plantId) {
                                    if ($plantId) {
                                        $query->where('plant_id', $plantId);
                                    }
                                })->pluck('code', 'id');
                            }),
                        // ->afterStateUpdated(function ($state, callable $set, callable $get) {
                        //     $set('process_order', null);
                        // }),
                        Select::make('work_group_master')
                            ->label('Search by Group Work Center')
                            ->nullable()
                            ->searchable()
                            ->reactive()
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');

                                if (empty($plantId)) {
                                    return [];
                                }

                                return WorkGroupMaster::whereHas('productCharacteristicsMasters', function ($query) use ($plantId) {
                                    if ($plantId) {
                                        $query->where('plant_id', $plantId);
                                    }
                                })->pluck('name', 'id');
                                // return $plantId ? Item::where('plant_id', $plantId)->pluck('code', 'id') : [];
                            })
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $set('Machine', null);
                            }),
                        Select::make('Machine')
                            ->label('Search by Work Center')
                            ->nullable()
                            ->searchable()
                            ->reactive()
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');
                                $lineId = $get('Line');
                                $workGroupMasterId = $get('work_group_master');

                                if (empty($plantId) || empty($lineId) || empty($workGroupMasterId)) {
                                    return [];
                                }

                                return Machine::whereHas('productCharacteristicsMasters', function ($query) use ($plantId, $lineId, $workGroupMasterId) {
                                    if ($plantId && $lineId && $workGroupMasterId) {
                                        $query->where('plant_id', $plantId)->where('line_id', $lineId)->where('work_group_master_id', $workGroupMasterId);
                                    }
                                })->pluck('work_center', 'id');
                                // return $plantId ? Item::where('plant_id', $plantId)->pluck('code', 'id') : [];
                            }),
                        // ->afterStateUpdated(function ($state, callable $set, callable $get) {
                        //     $set('process_order', null);
                        // }),
                        // TextInput::make('process_order')
                        //     ->label('Process Order')
                        //     ->placeholder('Enter Process Order'),
                        TextInput::make('name')
                            ->label('characteristics Name')
                            ->reactive()
                            ->placeholder(placeholder: 'Enter characteristics Name'),
                        Select::make('characteristics_type')
                            ->label('Search by Characteristics Type')
                            ->nullable()
                            ->searchable()
                            ->reactive()
                            ->options([
                                'Product' => 'Product',
                                'Process' => 'Process',
                            ]),
                        Select::make('inspection_type')
                            ->label('Search by Inspection Type')
                            ->nullable()
                            ->searchable()
                            ->reactive()
                            ->options([
                                'Visual' => 'Visual',
                                'Value' => 'Value',
                            ]),
                        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),
                    ])
                    ->query(function ($query, array $data) {
                        // Hide all records initially if no filters are applied
                        if (empty($data['Plant']) && empty($data['Line']) && empty($data['Item']) && empty($data['work_group_master']) && empty($data['Machine']) && empty($data['name']) && empty($data['characteristics_type']) && empty($data['inspection_type']) && empty($data['created_from']) && empty($data['created_to'])) {
                            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['Line'])) {
                            $query->where('line_id', $data['Line']);
                        }

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

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

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

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

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

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

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

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

                        // $query->orderBy('created_at', 'asc');
                    })
                    ->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['Line'])) {
                            $indicators[] = 'Line Name: '.Line::where('id', $data['Line'])->value('name');
                        }

                        if (! empty($data['Item'])) {
                            $indicators[] = 'Item Name: '.Item::where('id', $data['Item'])->value('code');
                        }

                        if (! empty($data['work_group_master'])) {
                            $indicators[] = 'Group Work Center: '.WorkGroupMaster::where('id', $data['work_group_master'])->value('name');
                        }

                        if (! empty($data['Machine'])) {
                            $indicators[] = 'Work Center: '.Machine::where('id', $data['Machine'])->value('work_center');
                        }

                        if (! empty($data['name'])) {
                            $indicators[] = 'Characteristics Name: '.$data['name'];
                        }

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

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

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

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

                        return $indicators;
                    }),
            ])
            ->filtersFormMaxHeight('280px')
            ->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(),
                ]),
            ])
            ->headerActions([
                ImportAction::make()
                    ->label('Import Product Characteristics Masters')
                    ->color('warning')
                    ->importer(ProductCharacteristicsMasterImporter::class)
                    ->visible(function () {
                        return Filament::auth()->user()->can('view import product characteristics master');
                    }),
                ExportAction::make()
                    ->label('Export Product Characteristics Masters')
                    ->color('warning')
                    ->exporter(ProductCharacteristicsMasterExporter::class)
                    ->visible(function () {
                        return Filament::auth()->user()->can('view export product characteristics master');
                    }),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListProductCharacteristicsMasters::route('/'),
            'create' => Pages\CreateProductCharacteristicsMaster::route('/create'),
            'view' => Pages\ViewProductCharacteristicsMaster::route('/{record}'),
            'edit' => Pages\EditProductCharacteristicsMaster::route('/{record}/edit'),
        ];
    }

    public static function getNavigationLabel(): string
    {
        return 'Characteristics';
    }

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