<?php

namespace App\Filament\Imports;

use App\Models\InvoiceValidation;
use App\Models\Item;
use App\Models\Plant;
use App\Models\StickerMaster;
use Carbon\Carbon;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Filament\Facades\Filament;
use Str;

class SapInvoiceValidationImporter extends Importer
{
    protected static ?string $model = InvoiceValidation::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('plant')
                ->requiredMapping()
                ->exampleHeader('PLANT CODE')
                ->example('2040')
                ->label('PLANT CODE')
                ->relationship(resolveUsing: 'code')
                ->rules(['required']),
            ImportColumn::make('invoice_number')
                ->requiredMapping()
                ->exampleHeader('INVOICE NUMBER')
                ->example('INV000001')
                ->label('INVOICE NUMBER')
                ->rules(['required']),
            ImportColumn::make('item_reference') // stickerMaster
                ->requiredMapping()
                ->exampleHeader('ITEM CODE')
                ->example('123456')
                ->label('ITEM CODE')
                // ->relationship() // resolveUsing: 'items.code'
                ->rules(['required']),
            ImportColumn::make('serial_number')
                ->requiredMapping()
                ->exampleHeader('SERIAL NUMBER')
                ->example('12345678901234')
                ->label('SERIAL NUMBER'),
            ImportColumn::make('scanned_status_set')
                ->requiredMapping()
                ->exampleHeader('PUMPSET SCANNED STATUS')
                ->example('1')
                ->label('PUMPSET SCANNED STATUS'),
            ImportColumn::make('created_at')
                ->requiredMapping()
                ->exampleHeader('CREATED AT')
                ->example('19-06-2026 15:00:00')
                ->label('CREATED AT')
                ->rules(['required']),
            ImportColumn::make('operator_id')
                ->requiredMapping()
                ->exampleHeader('OPERATOR ID')
                ->example('USER00001')
                ->label('OPERATOR ID')
                ->rules(['required']),
        ];
    }

    public function resolveRecord(): ?InvoiceValidation
    {
        // return InvoiceValidation::firstOrNew([
        //     // Update existing records, matching them by `$this->data['column_name']`
        //     'email' => $this->data['email'],
        // ]);

        $warnMsg = [];
        $plantId = null;
        $stickId = null;

        $plantCod = $this->data['plant'];
        $invoiceNumber = strtoupper(trim($this->data['invoice_number'])) ?? null;
        $iCode = strtoupper(trim($this->data['item_reference'])) ?? null;
        $serialNumber = trim($this->data['serial_number']) ?? null;
        $curPumpSetQr = trim($this->data['scanned_status_set']) ?? null;
        $curScanStatus = null;
        $loadRate = 0;
        $operatorId = trim($this->data['operator_id']);
        $createdAt = $this->data['created_at'];
        $createdBy = Filament::auth()->user()?->name;
        $updatedBy = $createdBy;

        $packCnt = 0;
        $scanCnt = 0;
        $hasPumpSetQr = null;
        $hadPumpSetQr = null;

        if ($plantCod == null || $plantCod == '') {
            $warnMsg[] = "Plant code can't be empty!";
        } elseif ($invoiceNumber == null || $invoiceNumber == '') {
            $warnMsg[] = "Invoice number can't be empty!";
        } elseif ($iCode == null || $iCode == '') {
            $warnMsg[] = "Item code can't be empty!";
        } elseif ($serialNumber == null || $serialNumber == '') {
            $warnMsg[] = "Serial number can't be empty!";
        } elseif ($curPumpSetQr != null && $curPumpSetQr != '' && $curPumpSetQr != '1' && $curPumpSetQr != 1) {
            $warnMsg[] = 'PumpSet scanned status is invalid!';
        } elseif ($operatorId == null || $operatorId == '') {
            $warnMsg[] = "Operator ID can't be empty!";
        } elseif ($createdAt == null || $createdAt == '') {
            $warnMsg[] = "Created at timestamp can't be empty!";
        }

        if (Str::length($plantCod) > 0) {
            if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
                $warnMsg[] = 'Invalid plant code found!';
            } elseif ($plantCod == '2040') { // 2040
                $plant = Plant::where('code', $plantCod)->first();
                if (! $plant) {
                    $warnMsg[] = 'Plant code not found!';
                } else {
                    $plantId = $plant->id;
                }
            } else {
                $warnMsg[] = "Unknown plant code '$plantCod' found!";
            }
        }

        if (Str::length($invoiceNumber) > 0 && ! ctype_alnum($invoiceNumber)) {
            $warnMsg[] = "Invalid invoice number '$invoiceNumber' found!";
        } elseif (Str::length($iCode) > 0 && (Str::length($iCode) < 6 || ! ctype_alnum($iCode))) {
            $warnMsg[] = "Invalid item code '$iCode' found!";
        } elseif ($plantId) {
            $itemCode = Item::where('code', $iCode)->first();
            if (! $itemCode) {
                $warnMsg[] = 'Item code not found in item master!';
            } else {
                $itemCode = Item::where('code', $iCode)->where('plant_id', $plantId)->first();
                if (! $itemCode) {
                    $warnMsg[] = 'Item code not found in item master for the given plant!';
                } else {
                    $itemId = $itemCode->id;
                    $itemCode = StickerMaster::where('item_id', $itemId)->first();
                    if (! $itemCode) {
                        $warnMsg[] = 'Item code not found in sticker master!';
                    } else {
                        if ($plantId) {
                            $itemCode = StickerMaster::where('item_id', $itemId)->where('plant_id', $plantId)->first();
                            if (! $itemCode) {
                                $warnMsg[] = 'Item code not found in sticker master for the given plant!';
                            } elseif ($itemCode->material_type != '' && $itemCode->material_type != null) {
                                $stickId = null;
                                $warnMsg[] = 'Material invoice item code found!';
                            } else {
                                $stickId = $itemCode->id;
                                $loadRate = $itemCode->load_rate ?? 0;
                                $invalidPackage = false;

                                $hasMotorQr = $itemCode->tube_sticker_motor ?? null;
                                $hasPumpQr = $itemCode->tube_sticker_pump ?? null;
                                $hasPumpSetQr = $itemCode->tube_sticker_pumpset ?? null;
                                $hasCapacitorQr = $itemCode->panel_box_code ?? null;

                                if (! $hasMotorQr && ! $hasPumpQr && ! $hasPumpSetQr) {// && ! $hasCapacitorQr
                                    $hasMotorQr = $itemCode->pack_slip_motor ?? null;
                                    $hasPumpQr = $itemCode->pack_slip_pump ?? null;
                                    $hasPumpSetQr = $itemCode->pack_slip_pumpset ?? null;
                                } else {
                                    if (! $hasPumpSetQr && ! $hasPumpQr) {
                                        $hasPumpQr = $itemCode->pack_slip_pump ?? null;
                                    }

                                    $hasTubeMotorQr = $itemCode->tube_sticker_motor ?? null;
                                    $hasPackMotorQr = $itemCode->pack_slip_motor ?? null;
                                    $hasTubePumpSetQr = $itemCode->tube_sticker_pumpset ?? null;
                                    $hasPackPumpSetQr = $itemCode->pack_slip_pumpset ?? null;
                                    if ($hasTubeMotorQr != $hasPackMotorQr || $hasTubePumpSetQr != $hasPackPumpSetQr) {
                                        $invalidPackage = true;
                                    }
                                }

                                if ($hasMotorQr || $hasPumpQr || ! $hasPumpSetQr || $hasCapacitorQr || $invalidPackage) {
                                    $stickId = null;
                                    $warnMsg[] = "Item code doesn't have valid package type to proceed!";
                                } else {
                                    $packCnt++;
                                }
                            }
                        }
                    }
                }
            }
        }

        if ($stickId) {
            $record = InvoiceValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)->first();

            if ($record) {
                if ($record->sticker_master_id != $stickId) {
                    $stickId = null;
                    $warnMsg[] = 'Item code mismatch with existing record!';
                } else {
                    $record = InvoiceValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)
                        ->whereHas('stickerMasterRelation.item', function ($query) use ($plantId, $iCode) {
                            $query->where('plant_id', $plantId)->where('code', $iCode);
                        })
                        ->first();

                    if ($record) {
                        $hadPumpSetQr = $record->scanned_status_set ?? null;

                        if ($hadPumpSetQr && $hasPumpSetQr) {
                            $curPumpSetQr = $hadPumpSetQr;
                        }

                        $warnMsg[] = 'Invoice Record Item ID : '.$record->sticker_master_id.' Master Item ID : '.$stickId;
                        if ($record->invoice_number != $invoiceNumber) {
                            $stickId = null;
                            $warnMsg[] = 'Invoice number mismatch with existing record!';
                        } elseif ($record->scanned_status == 'Scanned') {
                            $stickId = null;

                            return null;
                        } else {
                            if ($hasPumpSetQr) {
                                $scanCnt = $curPumpSetQr ? $scanCnt + 1 : $scanCnt;
                                $record->scanned_status_set = $curPumpSetQr;
                                if ($packCnt == $scanCnt) {
                                    $record->scanned_status = 'Scanned';
                                }
                                $record->upload_status = 'Y';
                                $record->updated_by = $updatedBy;
                                $record->save();

                                return null;
                            }
                        }
                    }
                }
            }
        }

        if ($stickId) {
            $formats = ['d-m-Y H:i', 'd-m-Y H:i:s']; // '07-05-2025 08:00' or '07-05-2025 08:00:00'
            $cDateTime = null;

            foreach ($formats as $format) {
                try {
                    $cDateTime = Carbon::createFromFormat($format, $createdAt);
                    break;
                } catch (\Exception $e) {
                    // $warnMsg[] = "Date format mismatch with format: $format";
                }
            }

            if (! isset($cDateTime)) {
                $warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
            }
        }

        if (! empty($warnMsg)) {
            throw new RowImportFailedException(implode(', ', $warnMsg));
        }

        if ($stickId) {
            if ($hasPumpSetQr) {
                $scanCnt = $curPumpSetQr ? $scanCnt + 1 : $scanCnt;

                if ($packCnt == $scanCnt) {
                    $curScanStatus = 'Scanned';
                } else {
                    $curScanStatus = null;
                }
            }
            // $curScanStatus

            InvoiceValidation::updateOrCreate([
                'plant_id' => $plantId,
                'sticker_master_id' => $stickId,
                'serial_number' => $serialNumber,
            ],
                [
                    'invoice_number' => $invoiceNumber,
                    'scanned_status_set' => $curPumpSetQr,
                    'scanned_status' => $curScanStatus,
                    'load_rate' => $loadRate,
                    'upload_status' => 'Y',
                    'operator_id' => $operatorId,
                    'created_by' => $createdBy,
                    'created_at' => $cDateTime->format('Y-m-d H:i:s'),
                    'updated_by' => $updatedBy,
                ]);
        }

        return null;
        // return new InvoiceValidation;
    }

    public static function getCompletedNotificationBody(Import $import): string
    {
        $body = 'Your sap invoice validation import has completed and '.number_format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.';

        if ($failedRowsCount = $import->getFailedRowsCount()) {
            $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.';
        }

        return $body;
    }
}
