<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\ToModel;
use Session;

class ImportTestMethod implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {   
        if ($row[0] != 'Test Method Name (required field)' && 
            $row[1] != 'Details' && 
            $row[2] != 'Status (Active/Inactive)' ) {

            if($row[0] != ''){

                if ($row[2] == '' || $row[2] == 'Active' || $row[2] == 'Inactive') {

                    $test_method_data = Session::get('TestMethodData');
                    $exist_test_method_name = array_column($test_method_data, 0);

                    if (in_array($row[0], $exist_test_method_name)){
                        $row['status'] = 'duplicate';
                        $test_method_data[] = $row;

                        Session::put('TestMethodData', $test_method_data);
                    } else {
                        $row['status'] = true;
                        $test_method_data[] = $row;

                        Session::put('TestMethodData', $test_method_data);
                    }
                } else {
                    throw new \Exception('Invalid Status.');
                }
            } else {
                throw new \Exception('Test Method name is required.');
            }
        }

        return;
    }
}