<?php

namespace App\Imports;

use App\Models\Extension;
use App\Models\Position;
use App\Models\Prefix;
use App\Models\Sector;
use App\Models\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class EmployeesImport implements ToModel, WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        if($row['id'] !== null && !User::whereEmail($row['email'])->exists()){

            $prefix_id = Prefix::whereName($row['name_prefix'])->first()->id ?? '1';
            $position_id = Position::whereName($row['position'])->first()->id ?? '1';
            $sector_id = Sector::whereName($row['sector_business_unit'])->first()->id ?? '1';
            $extension_id = Extension::whereName($row['extension'])->first()->id ?? '1';


            return new User([
                'the_id' => $row['id'],
                'prefix_id' => $prefix_id,
                'position_id' => $position_id,
                'sector_id' => $sector_id,
                'extension_id' => $extension_id,
                'name_ar' => $row['arabic_name'],
                'name_en' => $row['name'],
                'mobile' => $row['mobile_number'],
                'telephone' => $row['work_tel'],
                'fax' => $row['fax'],
                'address' => $row['alaanoan'],
                'website' => $row['website'],
                'moto_text' => $row['moto_text'],
                'location' => $row['location'],
                'email' => $row['email'],
                'password' => bcrypt(1234)
            ]);
        }

    }
}
