<?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_id')
                ->requiredMapping()
                ->numeric()
                ->rules(['required', 'integer']),
            ImportColumn::make('item_id')
                ->requiredMapping()
                ->numeric()
                ->rules(['required', 'integer']),
            ImportColumn::make('sticker1'),
            ImportColumn::make('sticker1_machine_id')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('sticker1_print_ip'),
            ImportColumn::make('sticker2'),
            ImportColumn::make('sticker2_machine_id')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('sticker2_print_ip'),
            ImportColumn::make('sticker3'),
            ImportColumn::make('sticker3_machine_id')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('sticker3_print_ip'),
            ImportColumn::make('sticker4'),
            ImportColumn::make('sticker4_machine_id')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('sticker4_print_ip'),
            ImportColumn::make('sticker5'),
            ImportColumn::make('sticker5_machine_id')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('sticker5_print_ip'),
            ImportColumn::make('sticker6'),
            ImportColumn::make('sticker6_machine_id')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('sticker6_print_ip'),
            ImportColumn::make('sticker7'),
            ImportColumn::make('sticker7_machine_id')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('sticker7_print_ip'),
            ImportColumn::make('sticker8'),
            ImportColumn::make('sticker8_machine_id')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('sticker8_print_ip'),
            ImportColumn::make('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;
    }
}
