<?php

namespace App\Filament\Resources;

use App\Filament\Exports\ItemExporter;
use App\Filament\Imports\ItemImporter;
use App\Filament\Resources\ItemResource\Pages;
use App\Models\InvoiceValidation;
use App\Models\Item;
use App\Models\Plant;
use App\Models\StickerMaster;
use Filament\Actions\Exports\Enums\ExportFormat;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
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\Notifications\Notification;
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 Maatwebsite\Excel\Facades\Excel;

class ItemResource extends Resource
{
    protected static ?string $model = Item::class;

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

    protected static ?string $navigationGroup = 'Master Entries';

    protected static ?int $navigationSort = 6;

    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')
                            ->required()
                        // ->preload()
                        // ->nullable(),
                            ->searchable()
                            ->reactive()
                            ->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();
                            })
                            ->default(function () {
                                return optional(Item::latest()->first())->plant_id;
                            })
                            ->disabled(fn (Get $get) => ! empty($get('id')))
                        // ->afterStateUpdated(fn ($set) => $set('block_id', null) & $set('name', null) & $set('start_time', null) & $set('duration', null) & $set('end_time', null))
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $plantId = $get('plant_id');
                                // Ensure `linestop_id` is not cleared
                                if (! $plantId) {
                                    $set('iPlantError', 'Please select a plant first.');

                                    return;
                                } else {
                                    $set('iPlantError', null);
                                }
                            })
                            ->extraAttributes(fn ($get) => [
                                'class' => $get('iPlantError') ? 'border-red-500' : '',
                            ])
                            ->hint(fn ($get) => $get('iPlantError') ? $get('iPlantError') : null)
                            ->hintColor('danger'),
                        Forms\Components\TextInput::make('category')
                            ->label('Category')
                            ->placeholder('Scan the Category'),
                        Forms\Components\TextInput::make('code')
                            ->required()
                            ->placeholder('Scan the valid code')
                            ->autofocus(true)
                            // ->unique(ignoreRecord: true)
                            ->alphaNum()
                            ->minLength(6)
                            // ->autocapitalize('characters')
                            ->reactive()
                            ->disabled(fn (Get $get) => ! empty($get('id')))
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $code = $get('code');
                                // Ensure `linestop_id` is not cleared
                                if (! $code) {
                                    $set('iCodeError', 'Scan the valid code.');

                                    return;
                                } else {
                                    if (strlen($code) < 6) {
                                        $set('iCodeError', 'Item code must be at least 6 digits.');

                                        return;
                                    } elseif (! preg_match('/^[a-zA-Z0-9]{6,}$/', $code)) {
                                        $set('code', null);
                                        $set('iCodeError', 'Item code must contain only alpha-numeric characters.');

                                        return;
                                    }
                                    $set('iCodeError', null);
                                }
                            })
                            ->extraAttributes(fn ($get) => [
                                'class' => $get('iCodeError') ? 'border-red-500' : '',
                            ])
                            ->hint(fn ($get) => $get('iCodeError') ? $get('iCodeError') : null)
                            ->hintColor('danger')
                            ->rule(function (callable $get) {
                                return Rule::unique('items', 'code')
                                    ->where('plant_id', $get('plant_id'))
                                    ->ignore($get('id')); // Ignore current record during updates
                            }),
                        Forms\Components\TextInput::make('hourly_quantity')
                            ->required()
                            ->label('Hourly Quantity')
                            ->placeholder('Scan the valid quantity')
                            ->integer()
                            ->default(1)
                            ->minValue(1)
                            ->reactive()
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $hourQuan = $get('hourly_quantity');
                                // Ensure `linestop_id` is not cleared
                                if (! $hourQuan) {
                                    $set('iHourQuanError', 'Scan the valid hourly quantity.');

                                    return;
                                } else {
                                    if (! preg_match('/^[0-9]{1,}$/', $hourQuan)) {
                                        $set('hourly_quantity', null);
                                        $set('iHourQuanError', 'Quantity must be integer value.');

                                        return;
                                    }
                                    $set('iHourQuanError', null);
                                }
                            })
                            ->extraAttributes(fn ($get) => [
                                'class' => $get('iHourQuanError') ? 'border-red-500' : '',
                            ])
                            ->hint(fn ($get) => $get('iHourQuanError') ? $get('iHourQuanError') : null)
                            ->hintColor('danger'),
                        Forms\Components\TextInput::make('uom')
                            ->required()
                            ->label('Unit of Measure')
                            ->placeholder('Scan the valid uom'),

                        Forms\Components\TextInput::make('description')
                            ->placeholder('Scan the valid description')
                            ->required()
                            ->minLength(5)
                            ->columnSpan(['default' => 1, 'sm' => 1]),
                        // ->columnSpanFull(),
                        Forms\Components\TextInput::make('id')
                            ->hidden()
                            ->readOnly(),
                    ])
                    ->columns(3),
            ]);
    }

    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(fn ($record, $livewire, $column, $rowLoop) => $rowLoop->iteration),
                    ->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')
                    ->alignCenter()
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('category')
                    ->label('Category')
                    ->default('-')
                    ->alignCenter()
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('code')
                    ->label('Item Code')
                    ->alignCenter()
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('description')
                    ->label('Description')
                    ->alignCenter()
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('hourly_quantity')
                    ->label('Hourly Quantity')
                    ->numeric()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('uom')
                    ->label('Unit of Measure')
                    ->default('-')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Created At')
                    ->dateTime()
                    ->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),
            ])
            ->searchPlaceholder('Search Item Code')
            ->filters([
                Tables\Filters\TrashedFilter::make(),
                Filter::make('advanced_filters')
                    ->label('Advanced Filters')
                    ->form([
                        Select::make('Plant')
                            ->label('Search by Plant Name')
                            ->nullable()
                            ->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();
                                if ($userHas && strlen($userHas) > 0) {
                                    return Plant::where('id', $userHas)->pluck('name', 'id')->toArray();
                                } else {
                                    return Plant::whereHas('items', function ($query) {
                                        $query->whereNotNull('id');
                                    })->orderBy('code')->pluck('name', 'id')->toArray();
                                }
                            })
                            ->searchable()
                            ->reactive()
                            ->afterStateUpdated(function ($state, callable $set, callable $get): void {
                                $set('code', null);
                                $set('operator_id', null);
                            }),
                        Select::make('code')
                            ->label('Search by Code')
                            ->nullable()
                            // ->options(function (callable $get) {
                            //     $plantId = $get('Plant');
                            //     return $plantId
                            //         ? Item::where('plant_id', $plantId)->pluck('code', 'id')
                            //         : Item::pluck('code', 'id');
                            // })
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');

                                return $plantId ? Item::where('plant_id', $plantId)->pluck('code', 'id') : [];
                            })
                            ->searchable()
                            ->reactive(),
                        TextInput::make('description')
                            ->label('Search by Description')
                            ->placeholder(placeholder: 'Enter Description'),
                        TextInput::make('category')
                            ->label('Search by Category')
                            ->placeholder(placeholder: 'Enter Category'),
                        TextInput::make('uom')
                            ->label('Search by UOM')
                            ->placeholder(placeholder: 'Enter UOM'),
                        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),
                        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['code']) && empty($data['description']) && empty($data['uom']) && empty($data['category']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['updated_from']) && empty($data['updated_to'])) {
                            return $query->whereRaw('1 = 0');
                        }

                        if (! empty($data['Plant'])) { // $plant = $data['Plant'] ?? null
                            $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['code'])) {
                            $query->where('id', $data['code']);
                        }

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

                        if (! empty($data['uom'])) {
                            if ($data['uom'] == 'empty') {
                                $query->where(function ($q) {
                                    $q->where('uom', '')->orWhereNull('uom');
                                });
                            } else {
                                $query->where('uom', 'like', '%'.$data['uom'].'%');
                            }
                        }

                        if (! empty($data['category'])) {
                            if ($data['category'] == 'empty') {
                                $query->where(function ($q) {
                                    $q->where('category', '')->orWhereNull('category');
                                });
                            } else {
                                $query->where('category', '%'.$data['category'].'%');
                            }
                        }

                        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_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: '.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['code'])) {
                            $indicators[] = 'Item Code: '.Item::where('id', $data['code'])->value('code');
                        }

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

                        if (! empty($data['uom'])) {
                            $indicators[] = ($data['uom'] == 'empty') ? "UOM: ''" : 'UOM: '.$data['uom'];
                        }

                        if (! empty($data['category'])) {
                            $indicators[] = ($data['category'] == 'empty') ? "Category: ''" : 'Category: '.$data['category'];
                        }

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

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

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

                        if (! empty($data['updated_to'])) {
                            $indicators[] = 'To: '.$data['updated_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([
                // Tables\Actions\Action::make('Import Items')
                //     ->label('Import Items')
                //     ->form([
                //         Select::make('plant_id')
                //         // ->options(Plant::pluck('name', 'id')->toArray()) // Fetch plant names and IDs
                //         ->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();
                //         })
                //         ->label('Select Plant')
                //         ->required()
                //         // ->default(function () {
                //         //     return optional(InvoiceValidation::latest()->first())->plant_id;
                //         // })
                //         ->afterStateUpdated(function ($state, callable $set, callable $get) {
                //             $set('item_code_file', null);
                //         })
                //         ->reactive(),

                //         FileUpload::make('item_code_file')
                //             ->label('Master Item Code')
                //             // ->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['item_code_file'];

                //         $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

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

                //         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 'Serial Invoice' file to proceed..!")
                //                     ->danger()
                //                     ->send();

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

                //             $invalidCategory = [];
                //             $materialCodes = [];
                //             $description = [];
                //             $invalidHourlyQuantity = [];
                //             $invalidUOM = [];
                //             $invalidPlantName = [];
                //             $seenItems = [];
                //             $uniqueInvalidCodes = [];
                //             $uniqueDesc = [];

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

                //                 $category       = trim($row[0]);
                //                 $materialCode   = trim($row[1]);
                //                 $desc           = trim($row[2]);
                //                 $hourlyQuantity = trim($row[3]);
                //                 $uom            = trim($row[4]);
                //                 $plantName      = trim($row[5]);

                //                 if (strlen($materialCode) < 6) {
                //                     $uniqueInvalidCodes[] = $materialCode;
                //                 }
                //                 else if (strlen($desc) < 7) {
                //                     $uniqueDesc[] = $desc;
                //                 }

                //                 $key = $plantName . '|' . $materialCode;

                //                 if (isset($seenItems[$key]))
                //                 {
                //                     $materialCodes[] = $materialCode;
                //                 }
                //                 else
                //                 {
                //                     $seenItems[$key] = true;
                //                 }
                //             }

                //             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()
                //                 ->send();
                //                 if ($disk->exists($path)) {
                //                     $disk->delete($path);
                //                 }
                //                 return;
                //             }
                //             else if (!empty($uniqueMissingSerials)) {
                //                 Notification::make()
                //                     ->title('Missing Serial Numbers')
                //                     ->body("The following item codes doesn't have valid serial number:<br>" . implode(', ', $uniqueMissingSerials))
                //                     ->danger()
                //                     ->send();
                //                 if ($disk->exists($path)) {
                //                     $disk->delete($path);
                //                 }
                //                 return;
                //             }
                //             else if (!empty($uniqueSerialCodes)) {
                //                 Notification::make()
                //                 ->title('Invalid Serial Number')
                //                 ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:<br>' . implode(', ', $uniqueSerialCodes))
                //                 ->danger()
                //                 ->send();
                //                 if ($disk->exists($path)) {
                //                     $disk->delete($path);
                //                 }
                //                 return;
                //             }
                //             else if (!empty($duplicateSerialCodes)) {
                //                 Notification::make()
                //                 ->title('Duplicate Serial Numbers')
                //                 ->body('The following serial numbers are already exist in imported excel:<br>' . implode(', ', $duplicateSerialCodes))
                //                 ->danger()
                //                 ->send();
                //                 if ($disk->exists($path)) {
                //                     $disk->delete($path);
                //                 }
                //                 return;
                //             }

                //             $uniqueCodes = array_unique($materialCodes);

                //             $matchedItems = StickerMaster::with('item')
                //             ->whereHas('item', function ($query) use ($uniqueCodes) {
                //                 $query->whereIn('code', $uniqueCodes);
                //             })
                //             ->get();

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

                //             $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()
                //                     ->send();

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

                //             // Check which codes have a material_type set (not null)
                //             $invalidCodes = $matchedItems
                //                 ->filter(fn ($sticker) => !empty($sticker->material_type))  //filter invalid
                //                 ->pluck('item.code')
                //                 ->toArray();

                //             if (count($invalidCodes) > 10)
                //             {
                //                 Notification::make()
                //                     ->title('Invalid item codes found')
                //                     ->body('' . count($invalidCodes) . 'item codes found have material type.')
                //                     ->danger()
                //                     ->send();

                //                 if ($disk->exists($path)) {
                //                     $disk->delete($path);
                //                 }
                //                 return;
                //             }
                //             else if (count($invalidCodes) > 0)
                //             {
                //                 Notification::make()
                //                     ->title('Invalid item codes found')
                //                     ->body('Material invoice Item Codes found : ' . implode(', ', $invalidCodes))
                //                     ->danger()
                //                     ->send();

                //                 if ($disk->exists($path)) {
                //                     $disk->delete($path);
                //                 }
                //                 return;
                //             }
                //             else
                //             {
                //                  // Save full file path to session
                //                 session(['uploaded_invoice_path' => $fullPath]);
                //                 Notification::make()
                //                     ->title('Serial invoice imported successfully.')
                //                     ->success()
                //                     ->send();
                //             }
                //         }
                //     })
                //     ->visible(function() {
                //         return Filament::auth()->user()->can('view import serial invoice');
                //     }),
                ImportAction::make()
                    ->label('Import Items')
                    ->color('warning')
                    ->importer(ItemImporter::class)
                    ->visible(function () {
                        return Filament::auth()->user()->can('view import item');
                    }),
                // ->maxRows(100000),
                ExportAction::make()
                        // ->columnMapping(true)
                    ->label('Export Items')
                    ->color('warning')
                        // ->fileName("Items Report " . date('Y-m-d H:i:s'))
                    ->exporter(ItemExporter::class)
                    ->visible(function () {
                        return Filament::auth()->user()->can('view export item');
                    }),
                // ->formats([
                //     ExportFormat::Xlsx,
                //     ExportFormat::Csv,
                // ]),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListItems::route('/'),
            'create' => Pages\CreateItem::route('/create'),
            'view' => Pages\ViewItem::route('/{record}'),
            'edit' => Pages\EditItem::route('/{record}/edit'),
        ];
    }

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