<?php

namespace App\Filament\Resources;

use App\Filament\Exports\LocatorExporter;
use App\Filament\Imports\LocatorImporter;
use App\Filament\Resources\LocatorResource\Pages;
use App\Filament\Resources\LocatorResource\RelationManagers;
use App\Models\Locator;
use App\Models\PalletValidation;
use App\Models\Plant;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Tables\Actions\ImportAction;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Validation\Rule;
use Str;

class LocatorResource extends Resource
{
    protected static ?string $model = Locator::class;

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

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

    protected static ?int $navigationSort = 9;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('plant_id')
                    ->relationship('plant', 'name')
                    ->required()
                    // ->nullable(),
                    ->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::pluck('name', 'id')->toArray();
                    })
                    ->default(function () {
                        return optional(Locator::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('locPlantError', 'Please select a plant first.');
                            $set('locator_number', null);
                            $set('locator_quantity', 0);
                            $set('operator_id', Filament::auth()->user()?->name);
                            return;
                        }
                        else
                        {
                            $set('locPlantError', null);
                            $set('locator_number', null);
                            $set('locator_quantity', 0);
                            $set('operator_id', Filament::auth()->user()?->name);
                        }
                    })
                    ->extraAttributes(fn ($get) => [
                        'class' => $get('locPlantError') ? 'border-red-500' : '',
                    ])
                    ->hint(fn ($get) => $get('locPlantError') ? $get('locPlantError') : null)
                    ->hintColor('danger'),
                Forms\Components\TextInput::make('locator_number')
                    ->label('Locator Number')
                    ->minLength(7)
                    ->reactive()
                    ->required()
                    ->afterStateUpdated(function ($state, callable $set, callable $get) {
                        $plantId = $get('plant_id');
                        $locator = $get('locator_number');
                        if (!$plantId) {
                            $set('locNameError', 'Please select a plant first.');
                            $set('locator_number', null);
                            $set('locator_quantity', 0);
                            $set('operator_id', Filament::auth()->user()?->name);
                            return;
                        }
                        else if (!$locator || Str::length($locator) < 7) {
                            $set('locNameError', 'Please scan the valid locator number.');
                            $set('locator_quantity', 0);
                            $set('operator_id', Filament::auth()->user()?->name);
                            return;
                        }
                        else
                        {
                            $set('locNameError', null);
                            $set('locator_quantity', PalletValidation::where('locator_number', $locator)->where('plant_id', $plantId)->distinct('pallet_number')->count('pallet_number'));
                            $set('operator_id', Filament::auth()->user()?->name);
                        }
                    })
                    ->extraAttributes(fn ($get) => [
                        'class' => $get('locNameError') ? 'border-red-500' : '',
                    ])
                    ->hint(fn ($get) => $get('locNameError') ? $get('locNameError') : null)
                    ->hintColor('danger')
                    ->rule(function (callable $get) {
                        return Rule::unique('locators', 'locator_number')
                            ->where('plant_id', $get('plant_id'))
                            ->ignore($get('id')); // Ignore current record during updates
                    }),
                Forms\Components\TextInput::make('locator_quantity')
                    ->label('Locator Quantity')
                    ->required()
                    ->readOnly()
                    ->numeric()
                    ->default(0),
                Forms\Components\Hidden::make('operator_id')
                    ->default(Filament::auth()->user()?->name)
                    ->required(),
                Forms\Components\TextInput::make('id')
                    ->hidden()
                    ->readOnly(),
            ]);
    }

    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(),
                Tables\Columns\TextColumn::make('locator_number')
                    ->label('Locator Number')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('locator_quantity')
                    ->label('Locator Quantity')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('operator_id')
                    ->label('Created By')
                    ->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),
            ])
            ->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): void {
                            $set('locator_number', null);
                            $set('locator_quantity', null);
                            $set('created_from', null);
                            $set('created_to', null);
                            $set('created_by', null);
                            $set('updated_from', null);
                            $set('updated_to', null);
                        }),
                    Select::make('locator_number')
                        ->label('Locator Number')
                        ->options(function (callable $get) {
                            $plantId = $get('Plant');
                            if (!$plantId) {
                                return [];
                            }
                            return Locator::where('plant_id', $plantId)->orderBy('locator_number', 'asc')->get()->unique('locator_number')->pluck('locator_number', 'locator_number')->toArray();
                            // ->whereNotNull('locator_number')
                            // ->where('locator_number','!=', '')
                        })
                        ->searchable()
                        ->reactive(),
                    Select::make('locator_quantity')
                        ->label('Locator Quantity')
                         ->options([
                            0 => 0,
                            1 => 1,
                            2 => 2,
                        ])
                        ->reactive(),
                    DateTimePicker::make(name: 'created_from')
                        ->label('Created From')
                        ->placeholder(placeholder: 'Select From DateTime')
                        ->reactive()
                        ->native(false),
                    DateTimePicker::make('created_to')
                        ->label('Created To')
                        ->placeholder(placeholder: 'Select To DateTime')
                        ->reactive()
                        ->native(false),
                    TextInput::make('created_by')
                        ->label('Created By')
                        ->reactive()
                        ->placeholder(placeholder: 'Enter Created By'),
                    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['locator_number']) && $data['locator_quantity'] == null && empty($data['created_from']) && empty($data['created_to']) && empty($data['created_by']) && 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']);
                    }

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

                    if ($data['locator_quantity'] != null && $data['locator_quantity'] != '') { //isset($data['locator_quantity']) &&
                        $query->where('locator_quantity', $data['locator_quantity']);//(int)
                    }

                    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['created_by'])) {
                        $query->where('operator_id', $data['created_by']);
                    }

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

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

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

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

                    if ($data['locator_quantity'] != null && $data['locator_quantity'] != '') { //isset($data['locator_quantity']) &&
                        $indicators[] = 'Locator Quantity: ' . $data['locator_quantity'];
                    }

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

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

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

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

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

                    return $indicators;
                })
            ])
            ->filtersFormMaxHeight('280px')
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                    Tables\Actions\ForceDeleteBulkAction::make(),
                    Tables\Actions\RestoreBulkAction::make(),
                ]),
            ])
            ->headerActions([
                ImportAction::make()
                    ->label('Import Locators')
                    ->color('warning')
                    ->importer(LocatorImporter::class)
                    ->visible(function() {
                        return Filament::auth()->user()->can('view import locator');
                    }),
                ExportAction::make()
                    ->label('Export Locators')
                    ->color('warning')
                    ->exporter(LocatorExporter::class)
                    ->visible(function() {
                        return Filament::auth()->user()->can('view export locator');
                    }),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListLocators::route('/'),
            'create' => Pages\CreateLocator::route('/create'),
            'view' => Pages\ViewLocator::route('/{record}'),
            'edit' => Pages\EditLocator::route('/{record}/edit'),
        ];
    }

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