<?php

namespace App\Filament\Imports;

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

class VehicleEntryImporter extends Importer
{
    protected static ?string $model = VehicleEntry::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('vehicle_number')
                ->requiredMapping()
                ->exampleHeader('VEHICLE NUMBER')
                ->example('ABC123')
                ->label('VEHICLE NUMBER')
                ->rules(['required']),
            ImportColumn::make('uuid')
                ->label('UUID')
                ->exampleHeader('UUID')
                ->example('ABC123'),
            ImportColumn::make('boom_opened')
                ->exampleHeader('BOOM OPENED')
                ->example('Auto')
                ->label('BOOM OPENED'),
            ImportColumn::make('entry_time')
                ->requiredMapping()
                ->exampleHeader('ENTRY TIME')
                ->example('2023-01-01 09:00:00')
                ->label('ENTRY TIME'),
            ImportColumn::make('exit_time')
                ->exampleHeader('EXIT TIME')
                ->example('2023-01-01 17:00:00')
                ->label('EXIT TIME'),
            ImportColumn::make('duration')
                ->exampleHeader('DURATION (HOURS)')
                ->example('8')
                ->label('DURATION (HOURS)'),
            ImportColumn::make('type')
                ->exampleHeader('TYPE')
                ->example('TRUCK')
                ->label('TYPE'),
            ImportColumn::make('created_by')
                ->exampleHeader('CREATED BY')
                ->example('John Doe')
                ->label('CREATED BY'),
            ImportColumn::make('updated_by')
                ->exampleHeader('UPDATED BY')
                ->example('Jane Doe')
                ->label('UPDATED BY'),


        ];
    }

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

        return new VehicleEntry();
    }

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