<?php

namespace App\Filament\Imports;

use App\Models\WireMasterPacking;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;

class WireMasterPackingImporter extends Importer
{
    protected static ?string $model = WireMasterPacking::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('plant')
                ->requiredMapping()
                ->exampleHeader('PLANT CODE')
                ->example('1000')
                ->label('PLANT CODE')
                ->relationship(resolveUsing: 'code')
                ->rules(['required']),
            ImportColumn::make('item')
                ->requiredMapping()
                ->exampleHeader('ITEM CODE')
                ->example('630214')
                ->label('ITEM CODE')
                ->relationship(resolveUsing: 'code')
                ->rules(['required']),
            ImportColumn::make('customer_po_master_id')
                ->requiredMapping()
                ->exampleHeader('CUSTOMER PO NUMBER')
                ->example('PO12345')
                ->label('CUSTOMER PO NUMBER')
                ->numeric()
                ->rules(['required', 'integer']),
            ImportColumn::make('wire_packing_number')
                ->exampleHeader('WIRE PACKING NUMBER')
                ->example('WP001')
                ->label('WIRE PACKING NUMBER'),
            ImportColumn::make('process_order')
                ->exampleHeader('PROCESS ORDER')
                ->example('PO001')
                ->label('PROCESS ORDER'),
            ImportColumn::make('batch_number')
                ->exampleHeader('BATCH NUMBER')
                ->example('BN001')
                ->label('BATCH NUMBER'),
            ImportColumn::make('weight')
                ->exampleHeader('WEIGHT')
                ->example('100.5')
                ->label('WEIGHT'),
            ImportColumn::make('wire_packing_status')
                ->exampleHeader('WIRE PACKING STATUS')
                ->example('Active')
                ->label('WIRE PACKING STATUS'),
            ImportColumn::make('scanned_at')
                ->requiredMapping()
                ->rules(['required', 'datetime']),
            ImportColumn::make('created_by')
                ->exampleHeader('CREATED BY')
                ->example('John Doe')
                ->label('CREATED BY'),
            ImportColumn::make('updated_by')
                ->exampleHeader('UPDATED BY')
                ->example('Jane Smith')
                ->label('UPDATED BY'),
            ImportColumn::make('scanned_by')
                ->exampleHeader('SCANNED BY')
                ->example('John Doe')
                ->label('SCANNED BY'),
        ];
    }

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

        return new WireMasterPacking();
    }

    public static function getCompletedNotificationBody(Import $import): string
    {
        $body = 'Your wire master packing 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;
    }
}
