<?php

namespace App\Imports;

use App\Models\FormDataBackup;
use App\Models\Setting;
use App\Models\VerifiedData;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\Importable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Concerns\ToCollection;

class VerifiedDataImport implements ToCollection,SkipsEmptyRows,WithHeadingRow,WithValidation
{
    
    use Importable;
    public $status="error";
    public $success=0;
    public $error=0;
    public $message=[];

    public function rules(): array
    {
        return [
            '*.unique_id' => 'required',
            '*.name' => 'required',
            '*.phone' => 'required',
            '*.q_id' => 'required|exists:form_data,q_id,employee_status,0',
            // '*.email' => 'required',
        ];
    }

    // /**
    // * @param array $row
    // *
    // * @return \Illuminate\Database\Eloquent\Model|null
    // */
    // public function model(array $row)
    // {
    //     // dd($row);
    //     return new VerifiedData([
    //         'unique_id'     => $row['unique_id'],
    //         'name'          => $row['name'], 
    //         'phone'         => $row['phone'], 
    //         'q_id'          => $row['q_id'], 
    //         'imei_number'   => $row['imei_number'], 
    //         'created_at'    => now(), 
    //     ]);
    // }

    public function collection(Collection $rows)
    {
        //  $validator = Validator::make($rows->toArray(), [
        //     'unique_id' => 'required',
        //     'name' => 'required',
        //     'phone' => 'required',
        //     'q_id' => 'required',
        //     'imei_number' => 'required',
        //  ]);
  
        //  if ($validator->fails()){
        //     $this->status="error";
        //     $this->message= $validator->errors()->first();
        //     return "hello";
        // }

        $prize_week_id=Setting::first()->prize_week_id;

        

        foreach ($rows as $row) {
            try{

                VerifiedData::updateOrCreate([
                        'q_id'          => $row['q_id'], 
                        'unique_id'     => $row['unique_id'],
                    ],[
                        'name'          => $row['name'], 
                        'phone'         => $row['phone'], 
                        'email'         => $row['email'], 
                        'prize_week_id'    => $prize_week_id, 
                        'created_at'    => now(), 
                ]);
                FormDataBackup::updateOrCreate([
                        'q_id'          => $row['q_id'], 
                        'unique_id'     => $row['unique_id'],
                    ],[
                        'name'          => $row['name'], 
                        'phone'         => $row['phone'], 
                        'email'         => $row['email'], 
                        'created_at'    => now(), 
                        'verified_status'    => 1, 
                        'prize_week_id'    => $prize_week_id, 
                ]);

                $this->success++;
            }catch(\Exception $e){
                // $this->status=$e->getMessage();
                $this->error++;
            }
        }
        $this->status="success";

    }

}
