<?php

namespace App\Imports;

use App\Models\Accessory;
use App\Models\Branch;
use App\Models\CarExchange;
use App\Models\ExtandWarranty;
use App\Models\Fasttag;
use App\Models\Finance;
use App\Models\Insurance;
use App\Models\Lead;
use App\Models\OBF;
use App\Models\Product;
use App\Models\Tax;
use App\Models\User;
use App\Models\Inventory;
use App\Models\Category;
use App\Models\Fuletype;
use App\Models\Carmodel;
use App\Models\Carvarient;
use App\Models\Interiorcolor;
use App\Models\Exteriorcolor;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithProgressBar;
use DB;
use Illuminate\Support\Arr;

class ImportInventory implements ToModel , WithStartRow , SkipsOnError, SkipsOnFailure ,WithProgressBar
{
    use Importable ,SkipsErrors ,SkipsFailures ;
    /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function startRow(): int
    {
        return 2;
    }
    public function model(array $row)
    {      

        /*var_dump($row[0]);
        die('s');
        */
        //Category
        /*$Category = (object)'';
        if(isset($row[0]) && !empty($row[0])){
            $Category = Category::select('id')->where(['name' => $row[0]])->first();
            if(!$Category){
                session(['error' => 'No Category Found on records!']);
                return null;
            }
        }else{
            $Category->id = null;
        }*/

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

        /*var_dump($Carmodel->id);
        die('s');*/

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

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

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

        //Product
        $Product = (object)'';
        if($Carmodel->id != null && $Carvarient->id != null && $Interiorcolor->id != null && $Exteriorcolor->id != null){
            $Product = Product::select('id')->where(['car_model' => $Carmodel->id,'veriant' => $Carvarient->id,'interior_color' => $Interiorcolor->id,'exterior_color' => $Exteriorcolor->id])->first();
            if(!$Product){
                session(['error' => 'No Product Found on records!']);
                return null;
            }
        }else{
            $Product->id = null;
        }

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

        //fuel type
        $fuel = (object)'';
        if(isset($row[14]) && !empty($row[14])){
            $fuel = Fuletype::select('id')->where(['title' => $row[14]])->first();

            if(!$fuel){
                session(['error' => 'No Fuel Type Found on records!']);
                return null;
            }
        }else{
            $fuel->id = null;
        }
        /*var_dump($row);
        die('s');*/

        // $exist_data = Inventory::select('id')->where(['name' => $Product->id,'branch_id' => $branch->id,'status' => 'active'])->first();
        $exist_data = Inventory::select('id')->where(['vin_number' => $row[9],'status' => 'active'])->first();
                       
       $data = [];
       if($exist_data){
           // session(['error' => 'Some Inventory is already available on records!']);
     
        $exist_data->name = $Product->id;
        $exist_data->branch_id = $branch->id;
        $exist_data->kin_invoice_date = date("Y-m-d",strtotime($row[5]));
        $exist_data->key_number = $row[6];
        $exist_data->engine_number = $row[7];
       // $exist_data->chassis_number = $row[8];
        $exist_data->vin_number = $row[9];
        $exist_data->ex_showroom_price = $row[10];
        $exist_data->purchase_price = $row[12];
        $exist_data->kin_margin = $row[13];
        $exist_data->fuel_type = $fuel->id;
        $exist_data->status = 'active';
        $exist_data->created_at = date('Y-m-d H:i:s');
        $exist_data->created_by = auth()->user()->id;
        $exist_data->updated_at = date('Y-m-d H:i:s');
        $exist_data->updated_by = auth()->user()->id;
        $exist_data->save();
        // die('sss');
           // return null;
       }else{  
        
        $data = [            
            //'category_id' => $Category->id,
            'name' => $Product->id,
            'branch_id' => $branch->id,
            'kin_invoice_date' => date("Y-m-d",strtotime($row[5])),
            'key_number' => $row[6],
            'engine_number' => $row[7],
          //  'chassis_number' => $row[8],
            'vin_number' => $row[9],
            'ex_showroom_price' => $row[10],   
            'purchase_price' => $row[12], 
            'kin_margin' => $row[13],
            'fuel_type' => $fuel->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
        ];

        }

        // die('sto[p');

        /*var_dump($data);
        die('s');*/
        
        /*$obf = Inventory::create($data);
        session(['success' => 'Record Imported Succesfully']);
        return $obf;*/

        $Inventory = [];
        if(!empty($data)){
             $Inventory = Inventory::create($data);
        }        
        return $Inventory;     
    }


    
    public function rules(): array
    {
        return [
            '*.10' => ['email' ,'unique:users,email']
        ];
    }
} 
