<?php

namespace Tests\Unit\Rules;

use App\Models\Project;

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

class AllMergedTiffsPresentRuleTest extends TestCase
{
    use RefreshDatabase;

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

    /**
     * @test
     */
    public function validatesCorrectValue()
    {
        $project = Project::create([
            'name'           => 'Test project',
            'mail_template'  => 'Test mail template',
            'mail_subject'   => 'Test mail subject',
            'mail_frequency' => 'Test mail frequency',
            'mail_day'       => 'Friday',
            'download_path'  => 'test_project',
        ]);

        $projectMock = Mockery::mock($project)->makePartial();

        $projectMock->shouldReceive('getMergedTiffList')->andReturn(
            collect([
                "chemba/merged_tiff/2021-01-04.tif",
                "chemba/merged_tiff/2021-01-05.tif",
                "chemba/merged_tiff/2021-01-06.tif",
                "chemba/merged_tiff/2021-01-07.tif",
                "chemba/merged_tiff/2021-01-08.tif",
                "chemba/merged_tiff/2021-01-09.tif",
                "chemba/merged_tiff/2021-01-10.tif",
            ]));

        $rule = new AllMergedTiffsPresentRule($projectMock);

        $val = Validator::make(
            ['attribute' => ['year' => '2021', 'week' => '1']],
            ['attribute' => $rule]
        );

        $this->assertTrue($val->passes());
    }

    public function testInvalidatesIncorrectValue()
    {
        $project = Project::create([
            'name'           => 'Test project',
            'mail_template'  => 'Test mail template',
            'mail_subject'   => 'Test mail subject',
            'mail_frequency' => 'Test mail frequency',
            'mail_day'       => 'Friday',
            'download_path'  => 'test_project',
        ]);

        $projectMock = Mockery::mock($project)->makePartial();

        $projectMock->shouldReceive('getMergedTiffList')->andReturn(
            collect([
//                "chemba/merged_tiff/2021-01-04.tif",
                "chemba/merged_tiff/2021-01-05.tif",
                "chemba/merged_tiff/2021-01-06.tif",
                "chemba/merged_tiff/2021-01-07.tif",
                "chemba/merged_tiff/2021-01-08.tif",
                "chemba/merged_tiff/2021-01-09.tif",
                "chemba/merged_tiff/2021-01-10.tif",
            ]));


        $rule = new AllMergedTiffsPresentRule($projectMock);

        $val = Validator::make(
            ['attribute' => ['year' => '2021', 'week' => '1']],
            ['attribute' => $rule]
        );

        $this->assertFalse($val->passes());
        $this->assertEquals(
            'Missing merged tiffs: 2021-01-04',
            $val->errors()->first()
        );
    }

}
