<?php

namespace Tests\Unit\Models;

use App\Enums\ApprovalStatusEnum;
use App\Models\RouteArticle;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class RouteArticleDecisionTest extends TestCase
{
    use RefreshDatabase;

    public function test_approving_stamps_the_decision_time(): void
    {
        /** @var RouteArticle $routeArticle */
        $routeArticle = RouteArticle::factory()->create([
            'approval_status' => ApprovalStatusEnum::PENDING,
            'decided_at' => null,
        ]);

        $this->freezeTime(function () use ($routeArticle) {
            $routeArticle->approve();

            $this->assertSame(
                now()->format('Y-m-d H:i:s'),
                $routeArticle->fresh()->decided_at->format('Y-m-d H:i:s')
            );
        });
    }

    public function test_rejecting_stamps_the_decision_time(): void
    {
        /** @var RouteArticle $routeArticle */
        $routeArticle = RouteArticle::factory()->create([
            'approval_status' => ApprovalStatusEnum::PENDING,
            'decided_at' => null,
        ]);

        $this->freezeTime(function () use ($routeArticle) {
            $routeArticle->reject();

            $this->assertSame(
                now()->format('Y-m-d H:i:s'),
                $routeArticle->fresh()->decided_at->format('Y-m-d H:i:s')
            );
        });
    }

    public function test_re_approving_does_not_move_the_decision_time(): void
    {
        /** @var RouteArticle $routeArticle */
        $routeArticle = RouteArticle::factory()->create([
            'approval_status' => ApprovalStatusEnum::PENDING,
            'decided_at' => null,
        ]);

        $routeArticle->approve();
        $first = $routeArticle->fresh()->decided_at;

        $this->travel(1)->hours();
        $routeArticle->fresh()->approve();

        $this->assertSame(
            $first->format('Y-m-d H:i:s'),
            $routeArticle->fresh()->decided_at->format('Y-m-d H:i:s')
        );
    }

    public function test_re_rejecting_does_not_move_the_decision_time(): void
    {
        /** @var RouteArticle $routeArticle */
        $routeArticle = RouteArticle::factory()->create([
            'approval_status' => ApprovalStatusEnum::PENDING,
            'decided_at' => null,
        ]);

        $routeArticle->reject();
        $first = $routeArticle->fresh()->decided_at;

        $this->travel(1)->hours();
        $routeArticle->fresh()->reject();

        $this->assertSame(
            $first->format('Y-m-d H:i:s'),
            $routeArticle->fresh()->decided_at->format('Y-m-d H:i:s')
        );
    }

    public function test_rejecting_an_approved_article_moves_the_decision_time(): void
    {
        /** @var RouteArticle $routeArticle */
        $routeArticle = RouteArticle::factory()->create([
            'approval_status' => ApprovalStatusEnum::PENDING,
            'decided_at' => null,
        ]);

        $routeArticle->approve();
        $first = $routeArticle->fresh()->decided_at;

        $this->travel(1)->hours();
        $routeArticle->fresh()->reject();

        $this->assertTrue($routeArticle->fresh()->decided_at->greaterThan($first));
    }
}
