<?php

namespace App\Filament\Imports;

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

class MfmMeterImporter extends Importer
{
    protected static ?string $model = MfmMeter::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('Device Name')
                ->requiredMapping()
                ->exampleHeader('Device Name')
                ->example('REG001')
                ->label('Device Name')
                ->relationship(resolveUsing:'name')
                ->rules(['required']),
            ImportColumn::make('sequence')
                ->requiredMapping()
                ->exampleHeader('Sequence')
                ->example('1')
                ->label('Sequence')
                ->rules(['required']),
            ImportColumn::make('name')
                ->requiredMapping()
                ->exampleHeader('Meter Name')
                ->example('Display SSB')
                ->label('Meter Name')
                ->rules(['required']),
            ImportColumn::make('created_by')
                ->requiredMapping()
                ->exampleHeader('Created By')
                ->example('Admin')
                ->label('Created By')
                ->rules(['required']),
        ];
    }

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

        return new MfmMeter();
    }

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