<?php

namespace App\Filament\Imports;

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

class StickerDetailImporter extends Importer
{
    protected static ?string $model = StickerDetail::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('stickerStructureDetail')
                ->requiredMapping()
                ->relationship()
                ->rules(['required']),
            ImportColumn::make('design_element_type'),
            ImportColumn::make('element_id'),
            ImportColumn::make('element_type'),
            ImportColumn::make('string_value'),
            ImportColumn::make('string_font'),
            ImportColumn::make('string_size'),
            ImportColumn::make('element_colour'),
            ImportColumn::make('string_align'),
            ImportColumn::make('string_x_value'),
            ImportColumn::make('string_y_value'),
            ImportColumn::make('shape_name'),
            ImportColumn::make('shape_pen_size'),
            ImportColumn::make('shape_x1_value'),
            ImportColumn::make('shape_y1_value'),
            ImportColumn::make('shape_x2_value'),
            ImportColumn::make('shape_y2_value'),
            ImportColumn::make('image_path'),
            ImportColumn::make('image_type'),
            ImportColumn::make('image_x'),
            ImportColumn::make('image_y'),
            ImportColumn::make('image_width'),
            ImportColumn::make('image_height'),
            ImportColumn::make('qr_value'),
            ImportColumn::make('qr_align'),
            ImportColumn::make('qr_size'),
            ImportColumn::make('qr_x_value'),
            ImportColumn::make('qr_y_value'),
            ImportColumn::make('created_by'),
            ImportColumn::make('updated_by'),
        ];
    }

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

        return new StickerDetail();
    }

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