<?php

namespace App\Filament\Resources;

use App\Filament\Exports\WeightValidationExporter;
use App\Filament\Imports\WeightValidationImporter;
use App\Filament\Resources\WeightValidationResource\Pages;
use App\Filament\Resources\WeightValidationResource\RelationManagers;
use App\Models\Item;
use App\Models\Line;
use App\Models\Plant;
use App\Models\QualityValidation;
use App\Models\StickerMaster;
use App\Models\WeightValidation;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Table;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use Str;
use Filament\Tables\Filters\Filter;

class WeightValidationResource extends Resource
{
    protected static ?string $model = WeightValidation::class;

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

    protected static ?string $navigationGroup = 'Weight';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('plant_id')
                    ->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::pluck('name', 'id')->toArray();
                    })
                    ->required(),
                Forms\Components\Select::make('item_id')
                    ->relationship('item', 'code')
                    ->label('Item Code')
                    ->searchable()
                    ->required(),
                Forms\Components\TextInput::make('obd_number')
                    ->label('OBD Number')
                    ->required(),
                Forms\Components\TextInput::make('line_number')
                    ->label('Line Number')
                    ->required(),
                Forms\Components\TextInput::make('batch_number')
                    ->label('Batch Number')
                    ->required(),
                Forms\Components\TextInput::make('obd_weight')
                    ->label('Actual Weight')
                    ->required(),
                Forms\Components\TextInput::make('vehicle_number')
                    ->label('Vehicle Number'),
                Forms\Components\TextInput::make('heat_number')
                    ->label('Heat Number'),
                Forms\Components\TextInput::make('bundle_number')
                    ->label('Bundle Number'),
                Forms\Components\TextInput::make('picked_weight')
                    ->label('Picked Weight'),
                Forms\Components\TextInput::make('scanned_by')
                    ->label('Scanned By')
            ]);
    }

    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')
                    ->alignCenter()
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('item.code')
                    ->label('Item Code')
                    ->alignCenter()
                    ->searchable(),
                Tables\Columns\TextColumn::make('obd_number')
                    ->label('OBD Number')
                    ->alignCenter()
                    ->searchable(),
                Tables\Columns\TextColumn::make('line_number')
                    ->label('Line Number')
                    ->alignCenter(),
                Tables\Columns\TextColumn::make('batch_number')
                    ->label('Batch Number')
                    ->alignCenter(),
                Tables\Columns\TextColumn::make('obd_weight')
                    ->label('Actual Weight')
                    ->alignCenter(),
                Tables\Columns\TextColumn::make('vehicle_number')
                    ->label('Vehicle Number')
                    ->alignCenter()
                    ->searchable(),
                Tables\Columns\TextColumn::make('heat_number')
                    ->label('Heat Number')
                    ->alignCenter(),
                Tables\Columns\TextColumn::make('bundle_number')
                    ->label('Bundle Number')
                    ->alignCenter(),
                Tables\Columns\TextColumn::make('picked_weight')
                    ->label('Picked Weight')
                    ->alignCenter(),
                Tables\Columns\TextColumn::make('scanned_by')
                    ->label('Scanned By')
                    ->alignCenter()
                    ->searchable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Create At')
                    ->dateTime()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('updated_at')
                    ->label('Updated At')
                    ->dateTime()
                    ->alignCenter()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('deleted_at')
                    ->label('Deleted At')
                    ->dateTime()
                    ->alignCenter()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            // ->filters([
            //     Tables\Filters\TrashedFilter::make(),
            // ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
                Filter::make('advanced_filters')
                    ->label('Advanced Filters')
                    ->form([
                        Select::make('Plant')
                            ->label('Select Plant')
                            ->nullable()
                            // ->options(function () {
                            //     return Plant::pluck('name', 'id');
                            // })
                            ->options(function (callable $get) {
                                $userHas = Filament::auth()->user()->plant_id;
                                return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
                            })
                            ->reactive(),
                            // ->afterStateUpdated(function ($state, callable $set, callable $get) {
                            //     $set('sticker_master_id', null);
                            //     $set('sap_msg_status', null);
                            // }),
                            Select::make('Item Code')
                            ->label('Search by Item Code')
                            ->nullable()
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');
                                if (!$plantId) {
                                    return [];
                                }
                                return Item::where('plant_id', $plantId)->pluck('code', 'id');
                            })
                            ->searchable()
                            ->reactive(),
                        TextInput::make('Obd Number')
                            ->label('OBD Number')
                            ->placeholder('Enter OBD Number'),

                        Select::make('Line')
                            ->label('Line')
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');

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

                                // Get unique line_numbers for the selected plant_id
                                return WeightValidation::where('plant_id', $plantId)
                                    ->distinct()
                                    ->orderBy('line_number')
                                    ->pluck('line_number', 'line_number')
                                    ->toArray();
                            })
                            ->reactive(),

                        TextInput::make('Batch')
                            ->label('Batch Number')
                            ->placeholder('Enter Batch Number'),

                        TextInput::make('Actual Weight')
                            ->label('Actual Weight')
                            ->placeholder('Enter Actual Weight'),

                        TextInput::make('Vehicle Number')
                            ->label('Vehicle Number')
                            ->placeholder('Enter Vehicle Number'),

                        TextInput::make('Heat Number')
                            ->label('Heat Number')
                            ->placeholder('Enter Heat Number'),

                        TextInput::make('Bundle Number')
                            ->label('Bundle Number')
                            ->placeholder('Enter Bundle Number'),

                        TextInput::make('Scanned By')
                            ->label('Scanned By')
                            ->placeholder('Enter Scanned By'),

                        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['Item Code']) && empty($data['Line']) && empty($data['Obd Number']) && empty($data['Batch']) && empty($data['Actual Weight']) && empty($data['Vehicle Number']) && empty($data['Heat Number']) && empty($data['Bundle Number']) && empty($data['Scanned By']) && empty($data['created_from']) && empty($data['created_to'])) {
                        return $query->whereRaw('1 = 0');
                    }

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

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

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

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

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

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

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

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

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

                    if (!empty($data['Scanned By'])) {
                        $query->where('scanned_by', $data['Scanned 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']);
                    }
                })
                ->indicateUsing(function (array $data) {
                    $indicators = [];

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

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

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

                    if (!empty($data['Obd Number'])) {
                        $indicators[] = 'OBD Number: ' .  $data['Obd Number'];
                    }

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

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

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

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

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

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

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

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

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

                    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([
                Tables\Actions\Action::make('Import OBD Number')
                    ->label('Import OBD Weight Invoice')
                    ->form([
                        Select::make('plant_id')
                            // ->options(Plant::pluck('name', 'id')->toArray())
                            ->options(function (callable $get) {
                                $userHas = Filament::auth()->user()->plant_id;
                                return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
                            })
                            ->label('Select Plant')
                            ->required()
                            ->default(function () {
                                return optional(WeightValidation::latest()->first())->plant_id;
                            })
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $set('invoice_obd_number', null);
                            })
                            ->reactive(),
                        FileUpload::make('invoice_obd_number')
                            ->label('Import OBD Weight Invoice')
                            // ->required()
                            ->preserveFilenames() // <- this keeps the original filename
                            ->storeFiles(false) // prevent auto-storing, we will store manually
                            ->reactive()
                            ->required()
                            ->disk('local') //'local' refers to the local storage disk defined in config/filesystems.php, typically pointing to storage/app.
                            ->visible(fn (Get $get) => !empty($get('plant_id')))
                            ->directory('uploads/temp'),
                    ])
                    ->action(function (array $data) {
                        $uploadedFile = $data['invoice_obd_number'];

                        $disk = Storage::disk('local');

                        $plantId = $data['plant_id'];

                        // Get original filename
                        $originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx

                        $originalNameOnly = pathinfo($originalName, PATHINFO_FILENAME);

                        // Store manually using storeAs to keep original name
                        $path = $uploadedFile->storeAs('uploads/temp', $originalName, 'local'); // returns relative path
                        // uploads/temp/3RA0018735.xlsx

                        $fullPath = Storage::disk('local')->path($path);

                        // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/3RA0018735.xlsx

                        $totQuan = WeightValidation::where('obd_number',$originalNameOnly)->where('plant_id', $plantId)->count();
                        $scanSQuan = WeightValidation::where('obd_number',$originalNameOnly)->where('plant_id', $plantId)->whereNotNull('vehicle_number')->where('vehicle_number', '!=', '')->count();

                        if($totQuan == $scanSQuan && $totQuan > 0) {
                            Notification::make()
                                ->title('Completed: OBD invoice')
                                ->body('OBD invoice already completed the scanning process for selected plant.')
                                ->danger()
                                ->send();

                            if ($disk->exists($path))
                            {
                                $disk->delete($path);
                            }
                            return;
                        }

                        // if($totQuan > 0) {
                        //     Notification::make()
                        //         ->title('OBD invoice already exist in database for selected plant.')
                        //         ->danger()
                        //         ->send();

                        //     if ($disk->exists($path))
                        //     {
                        //         $disk->delete($path);
                        //     }
                        //     return;
                        // }

                        if ($fullPath && file_exists($fullPath)) {
                            $rows = Excel::toArray(null, $fullPath)[0];

                            if((count($rows) - 1) <= 0) {
                                Notification::make()
                                    ->title('Records Not Found')
                                    ->body("Import the valid 'OBD Invoice' file to proceed..!")
                                    ->danger()
                                    ->seconds(2)
                                    ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }
                                return;
                            }

                            $invalidMatCodes = [];
                            $invalidLines = [];
                            $duplicateLines = [];
                            $invalidBatch = [];
                            // $invalidHeat = [];
                            $invalidWeight = [];
                            $materialCodes = [];
                            $lineNumbers = [];
                            $validRowsFound = false;

                            foreach ($rows as $index => $row)
                            {
                                if ($index === 0) continue;  // Skip header

                                $materialCode = trim($row[0]);
                                $lineNumber = trim($row[1]);
                                $batchNumber = trim($row[2]);
                                // $heatNumber = trim($row[3]);
                                $actualWeight = trim($row[3]);

                                if (empty($materialCode)) {
                                    continue;
                                }

                                if (!empty($materialCode)) {
                                    if(Str::length($materialCode) < 6 || !ctype_alnum($materialCode))
                                    {
                                        $invalidMatCodes[] = $materialCode;
                                        continue;
                                    }
                                    else
                                    {
                                        $materialCodes[] = $materialCode;

                                        $validData = true;
                                        if(Str::length($lineNumber) < 1 || !is_numeric($lineNumber))
                                        {
                                            $validData = false;
                                            $invalidLines[] = $materialCode;
                                        }
                                        else if (in_array($lineNumber, $lineNumbers))
                                        {
                                            $duplicateLines[] = $materialCode;
                                        }
                                        else
                                        {
                                            $lineNumbers[] = $lineNumber;
                                        }
                                        if(Str::length($batchNumber) < 8 || !is_numeric($batchNumber))//ctype_alnum
                                        {
                                            $validData = false;
                                            $invalidBatch[] = $materialCode;
                                        }
                                        // if(Str::length($heatNumber) < 4)
                                        // {
                                        //     $validData = false;
                                        //     $invalidHeat[] = $materialCode;
                                        // }
                                        if(Str::length($actualWeight) < 1 || !is_numeric($actualWeight))//ctype_alnum
                                        {
                                            $validData = false;
                                            $invalidWeight[] = $materialCode;
                                        }

                                        if ($validData)
                                        {
                                            $validRowsFound = true;
                                        }
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            $uniqueInvalidCodes = array_unique($invalidMatCodes);
                            if (!empty($uniqueInvalidCodes)) {
                                Notification::make()
                                    ->title('Invalid: Item Codes')
                                    ->body('The following item codes should contain minimum 6 digit alpha numeric values:<br>' . implode(', ', $uniqueInvalidCodes))
                                    ->danger()
                                    ->seconds(2)
                                    ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }
                                return;
                            }

                            $uniqueInvalidLines = array_unique($invalidLines);
                            if (!empty($uniqueInvalidLines)) {
                                Notification::make()
                                ->title('Invalid: Line Numbers')
                                ->body('Line number should contain minimum 1 digit numeric values!<br>Following item codes has invalid line number:<br>' . implode(', ', $uniqueInvalidLines))
                                ->danger()
                                ->seconds(2)
                                ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }
                                return;
                            }

                            $uniqueDupLines = array_unique($duplicateLines);
                            if (!empty($uniqueDupLines)) {
                                Notification::make()
                                ->title('Duplicate: Line Numbers')
                                ->body('The following item codes contains duplicate line numbers in invoice excel:<br>' . implode(', ', $uniqueDupLines))
                                ->danger()
                                ->seconds(2)
                                ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }
                                return;
                            }

                            $uniqueInvalidBatch = array_unique($invalidBatch);
                            if (!empty($uniqueInvalidBatch)) {
                                Notification::make()
                                ->title('Invalid: Batch Numbers')
                                ->body('Batch number should contain minimum 8 digit numeric values!<br>Following item codes has invalid batch number:<br>' . implode(', ', $uniqueInvalidBatch))
                                ->danger()
                                ->seconds(2)
                                ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }
                                return;
                            }

                            // $uniqueInvalidHeat = array_unique($invalidHeat);
                            // if (!empty($uniqueInvalidHeat)) {
                            //     Notification::make()
                            //     ->title('Invalid: Heat Numbers')
                            //     ->body('Heat number should contain minimum 4 characters!<br>Following item codes has invalid heat number:<br>' . implode(', ', $uniqueInvalidHeat))
                            //     ->danger()
                            //     ->seconds(2)
                            //     ->send();

                            //     if ($disk->exists($path)) {
                            //         $disk->delete($path);
                            //     }
                            //     return;
                            // }

                            $uniqueInvalidWeight = array_unique($invalidWeight);
                            if (!empty($uniqueInvalidWeight)) {
                                Notification::make()
                                ->title('Invalid: Actual Weights')
                                ->body('Actual weight should contain minimum 1 digit numeric value!<br>Following item codes has invalid actual weight:<br>' . implode(', ', $uniqueInvalidWeight))
                                ->danger()
                                ->seconds(2)
                                ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }
                                return;
                            }

                            $uniqueCodes = array_unique($materialCodes);

                            //itemNotFound.
                            $matchedItems = Item::whereIn('code', $uniqueCodes)->get();

                            // // Get all codes that exist in the database for the given plant_id
                            // $existingCodes = StickerMaster::where('plant_id', $plantId)
                            //     ->whereHas('item', function ($query) use ($uniqueCodes) {
                            //         $query->whereIn('code', $uniqueCodes);
                            //     })
                            //     ->with('item') // Eager load for performance
                            //     ->get()
                            //     ->pluck('item.code')
                            //     ->toArray();

                            $matchedCodes = $matchedItems->pluck('code')->toArray(); //item.code
                            $missingCodes = array_diff($uniqueCodes, $matchedCodes);

                            if (!empty($missingCodes)) {
                                $missingCount = count($missingCodes);

                                $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:<br>' . implode(', ', $missingCodes);

                                Notification::make()
                                ->title('Unknown: Item Codes')
                                ->body($message)
                                ->danger()
                                ->seconds(2)
                                ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }
                                return;
                            }

                            //plantNotFound
                            $matchedItems = Item::whereIn('code', $uniqueCodes)->where('plant_id', $plantId)->get();

                            $matchedCodes = $matchedItems->pluck('code')->toArray();
                            $missingCodes = array_diff($uniqueCodes, $matchedCodes);

                            if (!empty($missingCodes)) {
                                $missingCount = count($missingCodes);

                                $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:<br>' . implode(', ', $missingCodes);

                                Notification::make()
                                    ->title('Unknown: Item Codes')
                                    ->body($message)
                                    ->danger()
                                    ->seconds(2)
                                    ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }
                                return;
                            }

                            if (!$validRowsFound) {
                                Notification::make()
                                    ->title('Invalid OBD Invoice')
                                    ->danger()  // This makes the notification red to indicate an error
                                    ->body('Uploaded excel sheet is empty or<br>contains no valid data.')
                                    ->seconds(2)
                                    ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }
                                return;
                            }

                            $updateInv = WeightValidation::where('plant_id', $plantId)->where('obd_number', $originalNameOnly)->first()?->exists();

                            $availLines = WeightValidation::where('plant_id', $plantId)->where('obd_number', $originalNameOnly)->where(function($query) {
                                $query->whereNull('vehicle_number')->orWhere('vehicle_number', '');
                            })->pluck('line_number')->toArray();

                            $updated = WeightValidation::where('plant_id', $plantId)->where('obd_number', $originalNameOnly)->where(function($query) {
                                $query->whereNull('vehicle_number')->orWhere('vehicle_number', '');
                            })->count();

                            WeightValidation::where('plant_id', $plantId)->where('obd_number', $originalNameOnly)->where(function($query) {
                                $query->whereNull('vehicle_number')->orWhere('vehicle_number', '');
                            })
                            ->forceDelete();

                            $inserted = 0;
                            // $updated = 0;
                            foreach ($rows as $index => $row)
                            {
                                // Skip header
                                if ($index === 0) { continue; }

                                $materialCode = trim($row[0]);
                                $lineNumber = trim($row[1]);
                                $batchNumber = trim($row[2]);
                                // $heatNumber = trim($row[3]);
                                $actualWeight = trim($row[3]);

                                if (empty($materialCode) || Str::length($materialCode) < 6) {
                                    continue;
                                }

                                if (!empty($materialCode)) {
                                    $recordExist = WeightValidation::where('plant_id', $plantId)->where('obd_number', $originalNameOnly)->where('line_number', $lineNumber)->first();

                                    $masItem = Item::where('plant_id', $plantId)->where('code', $materialCode)->first();
                                    if($recordExist) {
                                        $skipUpd = $recordExist->vehicle_number ?? null;
                                        if($skipUpd)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            if ($masItem) {
                                                $recordExist->update([
                                                    'item_id' => $masItem->id,
                                                    'batch_number' => $batchNumber,
                                                    //'heat_number' => $heatNumber,
                                                    'obd_weight' => $actualWeight,
                                                    'updated_at' => now(),
                                                ]);
                                                $updated++;
                                            }
                                        }
                                    }
                                    else {
                                        if ($masItem) {
                                            WeightValidation::create([
                                                'item_id' => $masItem->id,
                                                'plant_id' => $plantId,
                                                'obd_number' => $originalNameOnly,
                                                'line_number' => $lineNumber,
                                                'batch_number' => $batchNumber,
                                                //'heat_number' => $heatNumber,
                                                'obd_weight' => $actualWeight,
                                            ]);
                                            if (in_array($lineNumber, $availLines))
                                            {
                                                continue;
                                            }
                                            else
                                            {
                                                $inserted++;
                                            }
                                        }
                                    }
                                }
                            }

                            if($updateInv) {
                                if ($updated > 0) {
                                    Notification::make()
                                        ->title("Start the scanning process!")
                                        ->body("'$updated' OBD lines were updated and<br>'$inserted' OBD lines were inserted for<br>imported OBD Invoice : '$originalNameOnly'.")
                                        ->info()
                                        // ->success()
                                        ->seconds(1)
                                        ->send();

                                    // Update total quantity in the form
                                    $totalQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->count();
                                    $scannedQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->whereNotNull('vehicle_number')->where('vehicle_number', '!=', '')->count();

                                    if ($totalQuantity === $scannedQuantity)
                                    {
                                        if ($disk->exists($path)) {
                                            $disk->delete($path);
                                        }
                                        // $this->dispatch('refreshCompletedInvoice', invoiceNumber: $originalNameOnly, plantId: $plantId);
                                    }
                                    else
                                    {
                                        if ($disk->exists($path)) {
                                            $disk->delete($path);
                                        }
                                        // $this->dispatch('refreshInvoiceData', invoiceNumber: $originalNameOnly, plantId: $plantId);
                                    }
                                }
                                else if ($inserted > 0) {
                                    Notification::make()
                                        ->title("Start the scanning process!")
                                        ->body("'$inserted' OBD lines were inserted for imported OBD Invoice : '$originalNameOnly'.")
                                        ->info()
                                        // ->success()
                                        ->seconds(1)
                                        ->send();

                                    // Update total quantity in the form
                                    $totalQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->count();
                                    $scannedQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->whereNotNull('vehicle_number')->where('vehicle_number', '!=', '')->count();

                                    if ($totalQuantity === $scannedQuantity)
                                    {
                                        if ($disk->exists($path)) {
                                            $disk->delete($path);
                                        }
                                        // $this->dispatch('refreshCompletedInvoice', invoiceNumber: $originalNameOnly, plantId: $plantId);
                                    }
                                    else
                                    {
                                        if ($disk->exists($path)) {
                                            $disk->delete($path);
                                        }
                                        // $this->dispatch('refreshInvoiceData', invoiceNumber: $originalNameOnly, plantId: $plantId);
                                    }
                                }
                                else {
                                    Notification::make()
                                        ->title("Import Failed: OBD Invoice")
                                        ->body("No exist records were updated for imported OBD Invoice : '$originalNameOnly'.")
                                        ->danger()
                                        ->seconds(2)
                                        ->send();

                                    $totalQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->count();
                                    $scannedQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->whereNotNull('vehicle_number')->where('vehicle_number', '!=', '')->count();

                                    if ($disk->exists($path)) {
                                        $disk->delete($path);
                                    }
                                    // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $originalNameOnly, plantId: $plantId);
                                    return;
                                }
                            }
                            else {
                                if ($inserted > 0) {
                                    Notification::make()
                                        ->title("Start the scanning process!")
                                        ->body("'$inserted' OBD lines were inserted for imported OBD Invoice : '$originalNameOnly'.")
                                        ->info()
                                        // ->success()
                                        ->seconds(1)
                                        ->send();

                                    // Update total quantity in the form
                                    $totalQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->count();
                                    $scannedQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->whereNotNull('vehicle_number')->where('vehicle_number', '!=', '')->count();

                                    if ($totalQuantity === $scannedQuantity)
                                    {
                                        if ($disk->exists($path)) {
                                            $disk->delete($path);
                                        }
                                        // $this->dispatch('refreshCompletedInvoice', invoiceNumber: $originalNameOnly, plantId: $plantId);
                                    }
                                    else
                                    {
                                        if ($disk->exists($path)) {
                                            $disk->delete($path);
                                        }
                                        // $this->dispatch('refreshInvoiceData', invoiceNumber: $originalNameOnly, plantId: $plantId);
                                    }
                                }
                                else {
                                    Notification::make()
                                        ->title("Import Failed: OBD Invoice")
                                        ->body("No new records were inserted for imported OBD Invoice : '$originalNameOnly'.")
                                        ->danger()
                                        ->seconds(2)
                                        ->send();

                                    $totalQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->count();
                                    $scannedQuantity = WeightValidation::where('obd_number', $originalNameOnly)->where('plant_id', $plantId)->whereNotNull('vehicle_number')->where('vehicle_number', '!=', '')->count();

                                    if ($disk->exists($path)) {
                                        $disk->delete($path);
                                    }
                                    // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $originalNameOnly, plantId: $plantId);
                                    return;
                                }
                            }
                        }
                    })
                    ->visible(function() {
                        return Filament::auth()->user()->can('view import obd number validations');
                    }),
                ImportAction::make()
                    ->label('Import OBD Invoice')
                    ->color('warning')
                    ->importer(WeightValidationImporter::class)
                    ->visible(function() {
                        return Filament::auth()->user()->can('view import weight validation');
                    }),
                ExportAction::make()
                    ->label('Export OBD Invoices')
                    ->color('warning')
                    ->exporter(WeightValidationExporter::class)
                    ->visible(function() {
                        return Filament::auth()->user()->can('view export weight validation');
                    }),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListWeightValidations::route('/'),
            'create' => Pages\CreateWeightValidation::route('/create'),
            'view' => Pages\ViewWeightValidation::route('/{record}'),
            'edit' => Pages\EditWeightValidation::route('/{record}/edit'),
        ];
    }

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