<?php

namespace App\Filament\Resources;

use App\Filament\Exports\ProductionOrderExporter;
use App\Filament\Imports\ProductionOrderImporter;
use App\Filament\Resources\ProductionOrderResource\Pages;
use App\Models\Item;
use App\Models\Line;
use App\Models\Plant;
use App\Models\ProductionOrder;
use Carbon\Carbon;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\Section;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Filters\Filter;
use Str;


class ProductionOrderResource extends Resource
{
    protected static ?string $model = ProductionOrder::class;

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

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

    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')
                            ->searchable()
                            ->options(function (callable $get) {
                                $userHas = Filament::auth()->user()->plant_id;

                                return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
                            })
                            ->disabled(fn (string $context): bool => $context == 'edit')
                            ->dehydrated(true)
                            ->default(function () {
                                $userHas = Filament::auth()->user()->plant_id;

                                return ($userHas && strlen($userHas) > 0) ? $userHas : optional(ProductionOrder::latest()->first())->plant_id;
                            })
                            ->reactive()
                            ->extraAttributes(fn ($get) => [
                                'class' => $get('poPlantError') ? 'border-red-500' : '',
                            ])
                            ->hint(fn ($get) => $get('poPlantError') ? $get('poPlantError') : null)
                            ->hintColor('danger')
                            ->required()
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('item_id', null);
                                $set('quantity', null);
                                $set('show_extra_fields', false);
                                $set('start_date', null);
                                $set('end_date', null);
                                $set('production_order', null);
                                $set('from_serial_number', null);
                                $set('to_serial_number', null);
                            }),
                        Forms\Components\Select::make('item_id')
                            ->label('Item Code')
                            ->searchable()
                            ->reactive()
                            ->options(function (callable $get) {
                                $plantId = $get('plant_id');
                                if (empty($plantId)) {
                                    return [];
                                }

                                return Item::where('plant_id', $plantId)->pluck('code', 'id');
                            })
                            ->disabled(fn (Get $get) => ! empty($get('id')))
                            ->default(function () {
                                $userHas = Filament::auth()->user()->plant_id;

                                return ($userHas && strlen($userHas) > 0) ? optional(ProductionOrder::where('plant_id', $userHas)->latest()->first())->item_id : optional(ProductionOrder::latest()->first())->item_id;
                            })
                            ->required()
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('quantity', null);
                                $set('show_extra_fields', false);
                                $set('start_date', null);
                                $set('end_date', null);
                                $set('production_order', null);
                                $set('from_serial_number', null);
                                $set('to_serial_number', null);
                            }),
                        Forms\Components\TextInput::make('quantity')
                            ->label('Quantity')
                            ->reactive()
                            ->integer()
                            ->required()
                            ->readOnly(fn ($get) => ($get('plant_id') == null || $get('item_id') == null))
                            ->disabled(fn (string $context): bool => $context === 'edit')
                            ->afterStateUpdated(function ($state, callable $set, $get) {
                                if (! empty($state) && $state > 0) {
                                    $set('show_extra_fields', true);
                                    $now = Carbon::now();
                                    $plantId = $get('plant_id');
                                    $quantity = $get('quantity');
                                    $set('start_date', null);
                                    $set('end_date', null);
                                    $plantCode = Plant::find($plantId)->code;

                                    $year = $now->format('y'); // Year (last 2 digits)
                                    $month = $now->format('m');

                                    $monthText = strtoupper($now->format('M')); // APR
                                    $monthNumber = '';

                                    foreach (str_split($monthText) as $char) {
                                        $curSeq = ord($char) - 64;
                                        $monthNumber .= str_pad($curSeq, 2, '0', STR_PAD_LEFT);
                                    }

                                    $prefix = $year.$monthNumber;

                                    $last = ProductionOrder::where('production_order', 'like', "{$prefix}%")->orderByDesc('production_order')->first(); // ProductionOrder::where('production_order', 'like', $prefix.'%')->orderBy('production_order', 'desc')->first();

                                    if ($last) {
                                        $lastSeq = substr($last->production_order, -4);
                                        $nextSeq = str_pad(((int) $lastSeq + 1), 4, '0', STR_PAD_LEFT);
                                    } else {
                                        $nextSeq = '0001';
                                    }

                                    $productionOrder = $prefix.$nextSeq;

                                    $set('production_order', $productionOrder);

                                    $prefixSerial = $plantCode.$year.$month;

                                    $last = ProductionOrder::where('to_serial_number', 'like', "{$prefixSerial}%")->orderByDesc('to_serial_number')->first();

                                    if ($last) {
                                        $lastSeq = (int) substr($last->to_serial_number, -6);
                                        $startSeq = $lastSeq + 1;
                                    } else {
                                        $startSeq = 1;
                                    }

                                    $endSeq = $startSeq + $quantity - 1;

                                    $fromSerial = $prefixSerial.str_pad($startSeq, 6, '0', STR_PAD_LEFT);
                                    $toSerial = $prefixSerial.str_pad($endSeq, 6, '0', STR_PAD_LEFT);

                                    $set('from_serial_number', $fromSerial);
                                    $set('to_serial_number', $toSerial);

                                } else {
                                    $set('show_extra_fields', false);

                                    $set('production_order', null);
                                    $set('start_date', null);
                                    $set('end_date', null);
                                    $set('from_serial_number', null);
                                    $set('to_serial_number', null);
                                }
                            }),
                        Forms\Components\Hidden::make('show_extra_fields')
                            ->default(false),
                        Forms\Components\DateTimePicker::make('start_date')
                            ->label('Start Date')
                            ->reactive()
                            // ->minDate(function () {
                            //     return Carbon::now()->addMinute();
                            // })
                            ->afterStateUpdated(function ($state, callable $set) {
                                $set('end_date', null);
                            }),
                        Forms\Components\DateTimePicker::make('end_date')
                            ->label('End Date')
                            ->reactive(),
                        // ->rule('after:start_date')
                        // ->minDate(function (callable $get) {
                        //     $startDate = $get('start_date');
                        //     if ($startDate) {
                        //         return Carbon::parse($startDate)->addMinute();
                        //     }

                        //     return Carbon::now()->addMinute();
                        // }),
                        Forms\Components\TextInput::make('production_order')
                            ->label('Production Order')
                            ->readOnly()
                            ->visible(fn ($get) => $get('quantity') > 0),
                        Forms\Components\TextInput::make('from_serial_number')
                            ->label('From Serial Number')
                            ->readOnly()
                            ->visible(fn ($get) => $get('quantity') > 0),
                        Forms\Components\TextInput::make('to_serial_number')
                            ->label('To Serial Number')
                            ->readOnly()
                            ->visible(fn ($get) => $get('quantity') > 0),
                        Forms\Components\Hidden::make('created_by')
                            ->label('Created By')
                            ->default(Filament::auth()->user()?->name),
                        Forms\Components\Hidden::make('updated_by')
                            ->label('Updated By')
                            ->default(Filament::auth()->user()?->name),
                        Forms\Components\View::make('forms.components.save-production-order-button'),
                    ])
                    ->columns(5),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('No.')
                    ->label('No.')
                    ->alignCenter()
                    ->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()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('item.code')
                    ->label('Item Code')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('item.description')
                    ->label('Item Description')
                    ->searchable()
                    ->alignCenter()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('item.uom')
                    ->label('Unit of Measure')
                    ->searchable()
                    ->alignCenter()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('quantity')
                    ->label('Quantity')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('start_date')
                    ->label('Start Date')
                    ->searchable()
                    ->alignCenter()
                    ->dateTime()
                    ->sortable(),
                Tables\Columns\TextColumn::make('end_date')
                    ->label('End Date')
                    ->searchable()
                    ->alignCenter()
                    ->dateTime()
                    ->sortable(),
                Tables\Columns\TextColumn::make('production_order')
                    ->label('Production Order')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('from_serial_number')
                    ->label('From Serial Number')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('to_serial_number')
                    ->label('To Serial Number')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Created At')
                    ->dateTime()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_by')
                    ->label('Created By')
                    ->searchable()
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('updated_at')
                    ->label('Updated At')
                    ->dateTime()
                    ->alignCenter()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_by')
                    ->label('Updated By')
                    ->searchable()
                    ->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(),
                Filter::make('advanced_filters')
                    ->label('Advanced Filters')
                    ->form([
                        Select::make('Plant')
                            ->label('Search by Plant Name')
                            ->nullable()
                            ->searchable()
                            ->reactive()
                            ->options(function (callable $get) {
                                $userHas = Filament::auth()->user()->plant_id;

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

                                // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
                            })
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $set('Item', null);
                                $set('Line', null);
                            }),
                        Select::make('Item')
                            ->label('Search by Item Code')
                            ->nullable()
                            ->searchable()
                            ->reactive()
                            ->options(function (callable $get) {
                                $plantId = $get('Plant');

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

                                return Item::whereHas('productionOrders', function ($query) use ($plantId) {
                                    if ($plantId) {
                                        $query->where('plant_id', $plantId);
                                    }
                                })->pluck('code', 'id');
                            })
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $set('production_order', null);
                            }),
                        TextInput::make('production_order')
                            ->label('Production Order')
                            ->reactive()
                            ->placeholder('Enter Production Order')
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $set('Rework', null);
                            }),
                        TextInput::make('from_serial_number')
                            ->label('From Serial Number')
                            ->reactive()
                            ->placeholder('Enter From Serial Number'),
                        TextInput::make('to_serial_number')
                            ->label('To Serial Number')
                            ->reactive()
                            ->placeholder('Enter To Serial Number'),
                        DateTimePicker::make('created_from')
                            ->label('Created From')
                            ->placeholder('Select From DateTime')
                            ->reactive()
                            ->native(false),
                        DateTimePicker::make('created_to')
                            ->label('Created To')
                            ->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']) && empty($data['production_order']) && empty($data['from_serial_number']) && empty($data['to_serial_number']) && empty($data['created_from']) && empty($data['created_to'])) {
                            return $query->whereRaw('1 = 0');
                        }

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

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

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

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

                        if (!empty($data['from_serial_number']) && !empty($data['to_serial_number'])) {

                            $from = $data['from_serial_number'];
                            $to   = $data['to_serial_number'];

                            $query->where(function ($q) use ($from, $to) {
                                $q->whereBetween('from_serial_number', [$from, $to])
                                ->orWhereBetween('to_serial_number', [$from, $to]);
                            });
                        }

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

                        if (! empty($data['created_to'])) {
                            $query->where('created_at', '<=', $data['created_to']);
                        }
                        // $query->orderBy('created_at', 'asc');
                    })
                    ->indicateUsing(function (array $data) {
                        $indicators = [];

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

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

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

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

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

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

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

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

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

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListProductionOrders::route('/'),
            'create' => Pages\CreateProductionOrder::route('/create'),
            'view' => Pages\ViewProductionOrder::route('/{record}'),
            'edit' => Pages\EditProductionOrder::route('/{record}/edit'),
        ];
    }

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