<?php

namespace Tests\Unit\Rules;

use App\Models\Project;

use App\Rules\AllMergedTiffsPresentRule;
use App\Rules\DownloadDateRangeRule;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Validator;
use Mockery;
use Tests\TestCase;

class DownloadDateRangeRuleTest extends TestCase
{
    use RefreshDatabase;

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

    /**
     * @test
     */
    public function validatesCorrectValue()
    {
        $val = Validator::make(
            ['dateRange' => '2023/12/26 to 2024/01/02'],
            ['dateRange' => new DownloadDateRangeRule()]
        );
        $this->assertTrue($val->passes());

        $this->assertEmpty(
            $val->errors()
        );
    }

    public function testInvalidatesIncorrectValue()
    {
        Carbon::setTestNow(Carbon::create(2021, 1, 1));

        $val = Validator::make(
            ['dateRange' => '2023/12/26 to 2024/01/02'],
            ['dateRange' => new DownloadDateRangeRule()]
        );
        $this->assertFalse($val->passes());

        $this->assertEquals(
            'Date range cannot be in the future.',
            $val->errors()->first()
        );
    }

    public function testInvalidFormat()
    {
        $val = Validator::make(
            ['dateRange' => '2023/12/26 tot 2024/01/02'],
            ['dateRange' => new DownloadDateRangeRule()]
        );
        $this->assertFalse($val->passes());

        $this->assertEquals(
            'Date range must be in the format YYYY/MM/DD to YYYY/MM/DD.',
            $val->errors()->first()
        );
    }

    public function testEndDateBeforeStartDate()
    {
        $val = Validator::make(
            ['dateRange' =>  '2024/01/02 to 2023/12/26'],
            ['dateRange' => new DownloadDateRangeRule()]
        );
        $this->assertFalse($val->passes());

        $this->assertEquals(
            'Start date must be before end date.',
            $val->errors()->first()
        );
    }
}
