<?php

namespace App\Filament\Imports;

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

class ConfigurationImporter extends Importer
{
    protected static ?string $model = Configuration::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('c_type')
                ->requiredMapping()
                ->exampleHeader('Type')
                ->example(['Testing Panel Readings'])
                ->label('Type')
                ->rules(['required']),
            ImportColumn::make('c_group')
                ->requiredMapping()
                ->exampleHeader('Group')
                ->example(['LMC_WINDED'])
                ->label('Group')
                ->rules(['required']),
            ImportColumn::make('c_name')
                ->requiredMapping()
                ->exampleHeader('Name')
                ->example(['MOTOR_PHASE'])
                ->label('Name')
                ->rules(['required']),
            ImportColumn::make('c_value')
                ->requiredMapping()
                ->exampleHeader('Value')
                ->example(['Single'])
                ->label('Value')
                ->rules(['required']),
            ImportColumn::make('line')
                ->requiredMapping()
                ->relationship(resolveUsing: 'name')
                ->exampleHeader('Line')
                ->example(['4 inch pump line'])
                ->label('Line')
                ->rules(['required']),
            ImportColumn::make('plant')
                ->requiredMapping()
                ->relationship(resolveUsing: 'code')
                ->exampleHeader('Plant')
                ->example(['1000'])
                ->label('Plant')
                ->rules(['required']),
        ];
    }

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

        return new Configuration;
    }

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