<?php

namespace App\Imports;

use App\Helpers\Helper;
use App\Models\Number;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;

class NumberImportBase implements ToCollection
{
    use Helper;

    public $data;
    public $totalNumber = 0;
    public $message = '';
    public $invalidNumbers = [];
    public $validNumbers = [];

    protected $user_id = '';
    protected $randomNumber = '';
    protected $type = '';
    protected $operators = [];

    function __construct($randomNumber,$user_id, $type, $operators) {
        $this->user_id=$user_id;
        $this->randomNumber=$randomNumber;
        $this->type=$type;
        $this->operators=$operators;
    }

    public function collection(Collection $collection)
    {
        $this->data = $collection->toArray();

        try{
            $chunkArr = array_chunk($this->data, 10000);
            $countryCode = config('sms.country_code');
            $invalidNumbers = [];


            foreach ($chunkArr as $eacChunk){
                $insertArray = [];

                foreach ($eacChunk as $row){
                    if (isset($row[0]) && !empty($row[0])){
                        $number = $row[0];
                        $name = (isset($row[1]) && !empty($row[1])) ? $row[1] : null;
                        $length = strlen($number);

                        $this->totalNumber = $this->totalNumber+1;

                        $finalNumber = false;

                        if (str_starts_with($number, $countryCode)) {
                            if ($length == 13){
                                $finalNumber = $number;
                            }else{
                                $this->invalidNumbers[] = $number;
                            }
                        } else {
                            if ($length == 11){
                                $finalNumber = "$countryCode"."$number";
                            }elseif ($length == 10){
                                $finalNumber = "$countryCode"."0$number";
                            }
                            else{
                                $this->invalidNumbers[] = $number;
                            }
                        }

                        if ($finalNumber){
                            $operator = substr($finalNumber, 2, 3);
                            $findOperator = isset($this->operators[$operator]) ? $this->operators[$operator] : null;

                            if ($findOperator){
                                $this->validNumbers[] = $number;
                                $insertArray[] = [
                                    'user_id' => $this->user_id,
                                    'phone' => $finalNumber,
                                    'name' => $name,
                                    'type_id' => $this->randomNumber,
                                    'type' => $this->type,
                                    'operator_id' => $findOperator ? $findOperator->operator_id : 0,
                                    'status' => 0,
                                ];
                            }else{
                                $this->invalidNumbers[] = $number;
                            }
                        }
                    }
                }

                if (count($invalidNumbers) == 0 && count($insertArray) > 0){
                    DB::table('numbers')->insert($insertArray);
                }
            }

            $this->message = 'Uploaded';

        }catch (\Exception $exception){
            $this->totalNumber = '';
            $this->message = 'Not inserted';
        }

    }
}
