<?php

namespace App\Imports;

use App\Models\Teammaster;
use App\Models\Carmodel;
use App\Models\Branch;
use App\Models\Department;
use App\Models\Carvarient;
use App\Models\Product;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use DB;

class ImportCarVarient implements ToModel ,WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function startRow(): int
    {
        return 6;
    }
    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;
        }

        $fuel_type = (object)'';
       if(isset($row['fuel_type']) && !empty($row['fuel_type'])){
           $fuel_type = DB::table('fule_type')->where(['title' => $row['fuel_type']])->first();
           if(!$fuel_type){
               session(['error' => 'No fule type Found on records!']);
               return null;
           }
       }else{
           $fuel_type->id = null;
       }

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

       $exterior_color = (object)'';
       $exterior_color_data_get = null;
       if(isset($row['exterior_color']) && !empty($row['exterior_color'])){

            $row['exterior_color'] = explode(',', $row['exterior_color']);

            $exterior_color_arr = [];
            if(!empty($row['exterior_color'])){
                foreach ($row['exterior_color'] as $value_new) {
                    $exterior_color = DB::table('exteriorcolor')->where(['name' => $value_new])->first();
                    if(!empty($exterior_color)){
                      array_push($exterior_color_arr, @$exterior_color->id);  
                    }
                }
                $exterior_color_data_get = implode(' ,', $exterior_color_arr);
            }else{
                $exterior_color_data_get = null;     
            }
          
       }else{
           $exterior_color_data_get = null;
       }

       $interiorcolor = (object)'';
       $interiorcolor_data_get = null;
       if(isset($row['interior_color']) && !empty($row['interior_color'])){

            $row['interior_color'] = explode(',', $row['interior_color']);

            $interiorcolor_arr = [];
            if(!empty($row['interior_color'])){
                foreach ($row['interior_color'] as $value_new) {
                    if(!empty($interiorcolor)){
                      $interiorcolor = DB::table('interiorcolor')->where(['name' => $value_new])->first();
                      array_push($interiorcolor_arr, @$interiorcolor->id);
                    }
                }
                $interiorcolor_data_get = implode(' ,', $interiorcolor_arr);
            }else{
                $interiorcolor_data_get = null;     
            }
          
       }else{
           $interiorcolor_data_get = null;
       }



      $tex_master = (object)'';

      if($row['tax'] != '-' && $row['tax'] != ''){
          if(isset($row['tax']) && !empty($row['tax'])){
             $tax_arr = [];

             // var_dump($row['tax']);

              $row['tax'] = str_replace("GST","",$row['tax']);
              $row['tax'] = str_replace("Compensation Cess","",$row['tax']);
              $row['tax'] = str_replace("-","",$row['tax']);

              $first_arr = explode('|', $row['tax']); 

              if(!empty($first_arr)){
                   $tex_master = DB::table('tex_master')->where(['gst' => trim(@$first_arr[0]),'compensation_cess' => trim(@$first_arr[1]),'status'=>'active'])->first();
                    if(!$tex_master){
                        session(['error' => 'No tax Found on records!']);
                        return null;
                    }
              }else{
                   $tex_master->id = null;
              }
          }else{
              $tex_master->id = null;
          }
      }else{
          $tex_master->id = null;
      }
       
      


       $colortone = (object)'';
       $colortone_data_get = null;
       if(isset($row['colortone']) && !empty($row['colortone'])){

            $row['colortone'] = explode(',', $row['colortone']);

            $colortone_arr = [];
            if(!empty($row['colortone'])){
                foreach ($row['colortone'] as $value_new) {
                    $colortone = DB::table('colortone')->where(['name' => $value_new])->first();
                    if(!empty($colortone_arr)){
                      array_push($colortone_arr, @$colortone->id);
                    }
                }
                $colortone_data_get = implode(' ,', $colortone_arr);
            }else{
                $colortone_data_get = null;     
            }
          
       }else{
           $colortone_data_get = null;
       }
        
        $exist_data = Carvarient::select('id')->where(['name' => $row['name'],'tex_master_id' => $tex_master->id,'status' => 'active'])->first();
        
        $data = [];
        if($exist_data){
            $data = [
              'name' => $row['name'],
              'car_model' => $Carmodel->id,
              'fule_type' => $fuel_type->id,
              'transmission' =>  $transmission->id,
              'hsn_code' => $row['hsn_code'],
              'exterior_color' => $exterior_color_data_get,
              'Interior_color' => $interiorcolor_data_get,
              'color_tone' => $colortone_data_get,
              'tex_master_id' => $tex_master->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
            ];
            Carvarient::where('id', $exist_data->id)->update($data);
            $insurance = Carvarient::find($exist_data->id);
        }else{  
            $data = [
              'name' => $row['name'],
              'car_model' => $Carmodel->id,
              'fule_type' => $fuel_type->id,
              'transmission' =>  $transmission->id,
              'hsn_code' => $row['hsn_code'],
              'exterior_color' => $exterior_color_data_get,
              'Interior_color' => $interiorcolor_data_get,
              'color_tone' => $colortone_data_get,
              'tex_master_id' => $tex_master->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
            ];
        }

        $insurance = [];
        if(!empty($data)){
            $insurance = Carvarient::create($data);

            $exterior_color_arr = [];
            if(!empty($exterior_color_data_get)){
                $exterior_color_arr = explode(',',$exterior_color_data_get);
            }

            $Interior_color_arr = [];
            if(!empty($interiorcolor_data_get)){
                $Interior_color_arr = explode(',',$interiorcolor_data_get);
            }

            if(!empty($exterior_color_arr)){
                foreach ($exterior_color_arr as $value_e) {
                    foreach ($Interior_color_arr as $value_i) {

                        if($value_i != '' && $value_e != ''){
                          $car_master_arr = [
                              'car_model' => $Carmodel->id,
                              'veriant' => $insurance->id,
                              'interior_color' => (int) $value_i,
                              'exterior_color' => (int) $value_e,
                              '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
                          ];
                          $car_master__id = Product::insertGetId($car_master_arr);    
                        }
                    }
                }
            }             
        }    
        return $insurance; 
    }
}
