<?php

namespace App\Filament\Imports;

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

class MfmParameterImporter extends Importer
{
    protected static ?string $model = MfmParameter::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('deviceName')
                ->requiredMapping()
                ->exampleHeader('Device Name')
                ->example('REG001')
                ->label('Device Name')
                ->relationship(resolveUsing: 'name')
                ->rules(['required']),
            ImportColumn::make('mfmMeter')
                ->requiredMapping()
                ->exampleHeader('Mfm Meter Sequence')
                ->example('1')
                ->label('Mfm Meter Sequence')
                ->relationship(resolveUsing: 'sequence')
                ->rules(['required']),
            ImportColumn::make('name')
                ->requiredMapping()
                ->exampleHeader('Parameter Name')
                ->example('apparent_energy_received')
                ->label('Parameter Name')
                ->rules(['required']),
            ImportColumn::make('register_id')
                ->requiredMapping()
                ->exampleHeader('Register ID')
                ->example('2715')
                ->label('Register ID')
                ->rules(['required']),
            ImportColumn::make('identifier')
                ->requiredMapping()
                ->exampleHeader('Identifier')
                ->example('Kvah')
                ->label('Identifier')
                ->rules(['required']),
            ImportColumn::make('byte_to_convert')
                ->requiredMapping()
                ->exampleHeader('Byte To Convert')
                ->example('1')
                ->label('Byte To Convert')
                ->rules(['required', 'integer']),
            ImportColumn::make('type_to_convert')
                ->requiredMapping()
                ->exampleHeader('Type To Convert')
                ->example('FLOAT32')
                ->label('Type To Convert')
                ->rules(['required']),
            ImportColumn::make('decimal_to_display')
                ->requiredMapping()
                ->exampleHeader('Decimal To Display')
                ->example('2')
                ->label('Decimal To Display')
                ->rules(['required', 'integer']),

        ];
    }

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

        return new MfmParameter;
    }

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