<?php

namespace App\Imports;

use App\Models\BusinessOrIndustry;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;

class BusinessIndustriesImport implements ToCollection
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function collection(Collection $rows)
    {
        foreach ($rows->skip(1) as $row){
            $clean_nop = preg_replace('/[^A-Za-z0-9]/', '', $row[2]);
            if (!BusinessOrIndustry::where('nop', $clean_nop)->exists()) {
                BusinessOrIndustry::create([
                    'nama_kecamatan'     => $row[0],
                    'nama_kelurahan'     => $row[1],
                    'nop'                => $clean_nop, // Store cleaned 'nop'
                    'nama_wajib_pajak'   => $row[3],
                    'alamat_wajib_pajak' => $row[4], 
                    'alamat_objek_pajak' => $row[5], 
                    'luas_bumi'          => $row[6], 
                    'luas_bangunan'      => $row[7], 
                    'njop_bumi'          => $row[8], 
                    'njop_bangunan'      => $row[9], 
                    'ketetapan'          => $row[10], 
                    'tahun_pajak'        => $row[11], 
                ]);
            }
        }
    }
}
