<?php

namespace App\Imports;

use App\Models\Cliente;
use Maatwebsite\Excel\Concerns\ToModel;

class ClientsImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        if (!isset($row[0]) || $row[0]=='nombre') {
            return null;
        }

        if (Cliente::where('cuit', $row[1])->exists()) {
            return null;   
        }
        return new Cliente([
            'name'        => $row[0],
            'cuit'        => $row[1],
            'iibb'        => $row[2],
            'type_client' => $this->getClientType($row[3]), 
            'email'       => $row[4], 
            'address'     => $row[5], 
            'location'    => $row[6], 
            'province'    => $row[7], 
            'web'         => $row[8], 
            'phone'       => $row[9], 
            'contact'     => $row[10]
        ]);
    }

    private function getClientType($client_type)
    {
        if ($client_type == 'Responsable Inscripto') {
            return 1;
        }
        if ($client_type == 'Monotributista') {
            return 2;
        }
        if ($client_type == 'Exento') {
            return 3;
        }
        if ($client_type == 'Consumidor Final') {
            return 4;
        }            
    }
}
