<?php

namespace App\Imports;

use App\Models\Attribute;
use App\Models\Lead;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class LeadsImportBackup implements ToModel, WithHeadingRow
{
    /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function model(array $row)
    {
        $project = Attribute::where(['type' => 'project', 'name' => $row['tower']])->first();
        $pt = Attribute::where(['type' => 'propertyType', 'name' => $row['property_type']])->first();
        return new Lead([
            'type' => 1,
            'name' => $row['name'] ?? null,
            'phone' => $row['number'] ?? null,
            'unit_no' => $row['unit_number'] ?? null,
            'district_id' => $project ? $project->parent_id : null,
            'project_id' => $project ? $project->id : null,
            'property_type_id' => $pt ? $pt->id : null,
            'lead_category_id' => 8666,
            'added_by' => auth()->id(),
            'reference_number' => generateReferenceNumber(auth()->id(), 'calling_databases')
        ]);
    }
}
