<?php

namespace App\Filament\Imports;

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

class StickerMappingMasterImporter extends Importer
{
    protected static ?string $model = StickerMappingMaster::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('item')
                ->requiredMapping()
                ->exampleHeader('Item Code')
                ->example('630987')
                ->label('Item Code')
                ->relationship(resolveUsing:'code')
                ->rules(['required']),
            ImportColumn::make('sticker1')
                ->label('Sticker Label 1')
                ->exampleHeader('Sticker Label 1')
                ->example('Label 1')
                ->rules(['required']),
            ImportColumn::make('sticker2')
                ->label('Sticker Label 2')
                ->exampleHeader('Sticker Label 2')
                ->example('601468'),
            ImportColumn::make('sticker3')
                ->label('Sticker Label 3')
                ->exampleHeader('Sticker Label 3'),
                //->example(''),
            ImportColumn::make('sticker4')
                ->label('Sticker Label 4')
                ->exampleHeader('Sticker Label 4'),
            ImportColumn::make('sticker5')
                ->label('Sticker Label 5')
                ->exampleHeader('Sticker Label 5'),
            ImportColumn::make('created_by')
                ->label('Created By')
                ->exampleHeader('Created By'),
            // ImportColumn::make('updated_by'),
        ];
    }

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

        return new StickerMappingMaster();
    }

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