<?php

namespace App\Imports;

use App\Models\Category;
use App\Models\Product;
use App\Models\Carmodel;
use App\Models\Carvarient;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use DB;

class ImportProduct implements ToModel ,WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function startRow(): int
    {
        return 7;
    }
    public function model(array $row)
    {
       
         $Carmodel = (object)'';
        if(isset($row['car_model']) && !empty($row['car_model'])){
            $Carmodel = Carmodel::select('id')->where(['name' => $row['car_model']])->first();
            if(!$Carmodel){
                session(['error' => 'No car model Found on records!']);
                return null;
            }
        }else{
            $Carmodel->id = null;
        }

        // var_dump($row);
        /*var_dump($row['car_veriant']);
        die('s');*/

        $Carvarient = (object)'';
        if(isset($row['car_veriant']) && !empty($row['car_veriant'])){
            $Carvarient = Carvarient::select('id')->where(['name' => $row['car_veriant']])->first();
            if(!$Carvarient){
                session(['error' => 'No Carvarient Found on records!']);
                return null;
            }
        }else{
            $Carvarient->id = null;
        }

        $interior_color = (object)'';
        if(isset($row['interior_color']) && !empty($row['interior_color'])){
            $interior_color = DB::table('interiorcolor')->where(['name' => $row['interior_color']])->first();
            if(!$interior_color){
                session(['error' => 'No tone selection Found on records!']);
                return null;
            }
        }else{
            $interior_color->id = null;
        }



        $exteriorcolor = (object)'';
        if(isset($row['exterior_color']) && !empty($row['exterior_color'])){
            $exteriorcolor = DB::table('exteriorcolor')->where(['name' => $row['exterior_color']])->first();
            if(!$exteriorcolor){
                session(['error' => 'No tone selection Found on records!']);
                return null;
            }
        }else{
            $exteriorcolor->id = null;
        }

        $exist_data = Product::select('id')->where(['car_model' => $Carmodel->id,'veriant' => $Carvarient->id,'interior_color' => $interior_color->id,'exterior_color' => $exteriorcolor->id,'status' => 'active'])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some Car Master is already available on records!']);
        }else{
            $data = [
                'car_model' => $Carmodel->id,
                'veriant' => $Carvarient->id,
                'interior_color' => $interior_color->id,
                'exterior_color' => $exteriorcolor->id,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth()->user()->id
            ];         
        }
        $products = [];
        if(!empty($data)){
            $products = Product::create($data);               
        }        
        return $products; 
        
    }
}
