<?php

namespace App\Filament\Imports;

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

class EquipmentMasterImporter extends Importer
{
    protected static ?string $model = EquipmentMaster::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('plant')
                ->requiredMapping()
                ->exampleHeader('Plant Name')
                ->example('Ransar Industries-I')
                ->label('Plant Name')
                ->relationship(resolveUsing:'name')
                ->rules(['required']),
            ImportColumn::make('machine')
                ->requiredMapping()
                ->exampleHeader('Machine Name')
                ->example('1006378')
                ->label('Machine Name')
                ->relationship(resolveUsing:'name')
                ->rules(['required']),
            ImportColumn::make('name')
                ->label('Name')
                ->exampleHeader('Name')
                ->example('463605 E-E-078'),
            ImportColumn::make('description')
                ->label('Description')
                ->exampleHeader('Description')
                ->example('FIN.6INCH.HOUSING LOWER 150R3+ CI RUN-OUT CHECKING FIXTURE'),
            ImportColumn::make('make')
                ->label('Make')
                ->exampleHeader('Make')
                ->example('Ok'),
            ImportColumn::make('model')
                ->label('Model')
                ->exampleHeader('Model')
                ->example('Ok'),
            ImportColumn::make('equipment_number')
                ->label('Equipment Number')
                ->exampleHeader('Equipment Number')
                ->example('463605 E-E-078'),
            ImportColumn::make('instrument_serial_number')
                ->label('Instrument Serial Number')
                ->exampleHeader('Instrument Serial Number')
                ->example('131548498'),
            ImportColumn::make('calibrated_on')
                ->label('Calibrated On')
                ->exampleHeader('Calibrated On')
                ->example('01-09-2025 08:00:00')
                ->requiredMapping()
                ->rules(['required', 'datetime']),
            ImportColumn::make('frequency')
                ->label('Frequency')
                ->exampleHeader('Frequency')
                ->example('15')
                ->requiredMapping()
                ->numeric()
                ->rules(['required', 'integer']),
            ImportColumn::make('next_calibration_date')
                ->label('Next Calibration Date')
                ->exampleHeader('Next Calibration Date')
                ->requiredMapping()
                ->example('16-09-2025 08:00:00')
                ->rules(['required', 'datetime']),
            ImportColumn::make('calibrated_by')
                ->label('Calibration By')
                ->example('Sri Venkateswara Tools')
                ->exampleHeader('Calibration By'),
            ImportColumn::make('calibration_certificate')
                ->label('Calibration Certificate')
                ->example('1231CRI651')
                ->exampleHeader('Calibration Certificate'),
            ImportColumn::make('created_by')
                ->label('Created By')
                ->example('RAS00296')
                ->exampleHeader('Created By'),
        ];
    }

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

        return new EquipmentMaster();
    }

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