<?php

namespace App\Filament\Imports;

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

class VisitorEntryImporter extends Importer
{
    protected static ?string $model = VisitorEntry::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('employeeMaster')
                ->requiredMapping()
                ->relationship()
                ->rules(['required']),
            ImportColumn::make('mobile_number')
                ->requiredMapping()
                ->numeric()
                ->rules(['required', 'integer']),
            ImportColumn::make('name'),
            ImportColumn::make('company'),
            ImportColumn::make('purpose_of_visit'),
            ImportColumn::make('type'),
            ImportColumn::make('photo'),
            ImportColumn::make('number_of_person')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('in_time')
                ->rules(['datetime']),
            ImportColumn::make('out_time')
                ->rules(['datetime']),
            ImportColumn::make('created_by'),
            ImportColumn::make('updated_by'),
            ImportColumn::make('register_id'),
            ImportColumn::make('valid_upto')
                ->rules(['datetime']),
        ];
    }

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

        return new VisitorEntry();
    }

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