<?php

namespace App\Filament\Imports;

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

class RequestCharacteristicImporter extends Importer
{
    protected static ?string $model = RequestCharacteristic::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('plant')
                ->requiredMapping()
                ->exampleHeader('PLANT CODE')
                ->example('1000')
                ->label('PLANT CODE')
                ->relationship(resolveUsing: 'code')
                ->rules(['required']),
            ImportColumn::make('machine')
                ->requiredMapping()
                ->exampleHeader('WORK CENTER')
                ->example('RMGLAS01')
                ->label('WORK CENTER')
                ->relationship(resolveUsing: 'work_center')
                ->rules(['required']),
            ImportColumn::make('item')
                ->requiredMapping()
                ->exampleHeader('ITEM CODE')
                ->example('630214')
                ->label('ITEM CODE')
                ->relationship(resolveUsing: 'code')
                ->rules(['required']),
            ImportColumn::make('characteristicApproverMaster')
                ->requiredMapping()
                ->relationship()
                ->rules(['required']),
            ImportColumn::make('aufnr')
                ->label('AUFNR')
                ->exampleHeader('AUFNR')
                ->example('1234567'),
            ImportColumn::make('characteristic_name')
                ->label('CHARACTERISTIC NAME')
                ->exampleHeader('CHARACTERISTIC NAME')
                ->example('ZMM_PUMPSET_MODEL'),
            ImportColumn::make('current_value')
                ->label('CURRENT VALUE')
                ->exampleHeader('CURRENT VALUE')
                ->example('MVN-32/02 D R'),
            ImportColumn::make('update_value')
                ->label('UPDATE VALUE')
                ->exampleHeader('UPDATE VALUE')
                ->example('MVN-32/02 TR3'),
            ImportColumn::make('approver_status1')
                ->label('APPROVER STATUS 1')
                ->exampleHeader('APPROVER STATUS 1')
                ->example('Hold'),
            ImportColumn::make('approver_status2')
                ->label('APPROVER STATUS 2')
                ->exampleHeader('APPROVER STATUS 2')
                ->example('Approved'),
            ImportColumn::make('approver_status3')
                ->label('APPROVER STATUS 3')
                ->exampleHeader('APPROVER STATUS 3')
                ->example('Rejected'),
            ImportColumn::make('approver_remark1')
                ->label('APPROVER REMARK 1')
                ->exampleHeader('APPROVER REMARK 1')
                ->example('Hold for review'),
            ImportColumn::make('approver_remark2')
                ->label('APPROVER REMARK 2')
                ->exampleHeader('APPROVER REMARK 2')
                ->example('Approved with comments'),
            ImportColumn::make('approver_remark3')
                ->label('APPROVER REMARK 3')
                ->exampleHeader('APPROVER REMARK 3')
                ->example('Rejected due to incorrect value'),
            ImportColumn::make('work_flow_id')
                ->label('WORK FLOW ID')
                ->exampleHeader('WORK FLOW ID')
                ->example('WF-260303-0001'),
            ImportColumn::make('mail_status')
                ->label('MAIL STATUS')
                ->exampleHeader('MAIL STATUS')
                ->example('Sent'),
            ImportColumn::make('trigger_at')
                ->label('TRIGGER AT')
                ->exampleHeader('MAIL STATUS')
                ->rules(['datetime']),
            ImportColumn::make('approved1_at')
                ->label('APPROVED1 AT')
                ->exampleHeader('APPROVED1 AT')
                ->rules(['datetime']),
            ImportColumn::make('approved2_at')
                ->label('APPROVED2 AT')
                ->exampleHeader('APPROVED2 AT')
                ->rules(['datetime']),
            ImportColumn::make('approved3_at')
                ->label('APPROVED3 AT')
                ->exampleHeader('APPROVED3 AT')
                ->rules(['datetime']),
            ImportColumn::make('created_by')
                ->label('CREATED BY')
                ->exampleHeader('CREATED BY')
                ->example('RAW01234'),
            ImportColumn::make('updated_by')
                ->label('UPDATED BY')
                ->exampleHeader('UPDATED BY')
                ->example('RAW01234'),
            ImportColumn::make('model_type')
                ->label('MODEL TYPE')
                ->exampleHeader('MODEL TYPE')
                ->example('PUMP'),
        ];
    }

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

        return new RequestCharacteristic();
    }

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