<?php

namespace Tests\Unit\Imports;

use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDate;
use Tests\TestCase;
use function PHPUnit\Framework\assertEquals;

class HarvestDataImportTest extends TestCase
{
    private $base_path;
    private $excelData;

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub

        $this->base_path = base_path('tests/__fixtures__/harvest.xlsx');
        $this->excelData = Excel::toCollection(new \App\Imports\ExcelFileImport(), $this->base_path);
    }

    public function test_it_gets_data_from_excel_file()
    {
        $season_start_index = $this->excelData->first()->first()->search('season_start');
        $min = now();
        collect($this->excelData->first()->slice(1))->each(function ($value) use (&$min, $season_start_index) {
            $min = min($min, Carbon::instance(SharedDate::excelToDateTimeObject($value[$season_start_index])));
        });
        $this->assertTrue($min->equalTo('2023-01-12'));
    }

    public function test_it_gets_the_min_value()
    {
        $season_start_index = $this->excelData->first()->first()->search('season_start');
        $min = now();
        collect($this->excelData->first()->slice(1))->each(function ($value) use (&$min, $season_start_index) {
            $min = min($min, Carbon::instance(SharedDate::excelToDateTimeObject($value[$season_start_index])));
        });

        assertEquals(Carbon::parse('2023-01-12'), $min);
    }

    public function test_it_gets_the_min_value_using_reduce()
    {
        $season_start_index = $this->excelData->first()->first()->search('season_start');
        $min = collect($this->excelData->first()->slice(1))->reduce(function ($carry, $value, $key) use ($season_start_index) {
            return min($carry, Carbon::instance(SharedDate::excelToDateTimeObject($value[$season_start_index])));
        }, now());
        assertEquals(Carbon::parse('2023-01-12'), $min);
    }


}
