<?php

namespace App\Imports;

use App\Result;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;

class ResultPrepareImport implements ToCollection, WithHeadingRow, WithBatchInserts, WithChunkReading
{
  /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function collection(Collection $rows)
    {

        $uniqueMatrics = $rows->unique('matric')->pluck('matric')->toArray();
         
        $result = $rows->first()->only(['session','semester']);
          
        Result::whereIn('matric', $uniqueMatrics)
            ->where('session', $result['session'])
            ->where('semester', $result['semester'])
            ->delete();
 
    }

    public function batchSize(): int
    {
        return 1000;
    }

    public function chunkSize(): int
    {
        return 1000;
    }

}
