<?php

namespace Tests\Unit;

use App\Models\User;
use Carbon\Carbon;
use Tests\TestCase;

class UserTest extends TestCase
{
    protected function tearDown(): void
    {
        Carbon::setTestNow();

        parent::tearDown();
    }

    public function test_is_role_returns_true_for_matching_role(): void
    {
        $user = new User(['role' => 'sekretariat']);

        $this->assertTrue($user->isRole('sekretariat'));
    }

    public function test_is_role_returns_false_for_non_matching_role(): void
    {
        $user = new User(['role' => 'user']);

        $this->assertFalse($user->isRole('sekretariat'));
    }

    public function test_must_change_password_when_password_changed_at_is_empty(): void
    {
        $user = new User(['password_changed_at' => null]);

        $this->assertTrue($user->mustChangePassword());
    }

    public function test_must_change_password_when_password_is_older_than_three_months(): void
    {
        Carbon::setTestNow(Carbon::parse('2026-07-08 10:00:00'));

        $user = new User([
            'password_changed_at' => Carbon::parse('2026-04-07 09:59:59'),
        ]);

        $this->assertTrue($user->mustChangePassword());

        Carbon::setTestNow();
    }

    public function test_must_change_password_on_exact_three_month_boundary(): void
    {
        Carbon::setTestNow(Carbon::parse('2026-07-08 10:00:00'));

        $user = new User([
            'password_changed_at' => Carbon::parse('2026-04-08 10:00:00'),
        ]);

        $this->assertTrue($user->mustChangePassword());

        Carbon::setTestNow();
    }

    public function test_password_is_still_fresh_before_three_months(): void
    {
        Carbon::setTestNow(Carbon::parse('2026-07-08 10:00:00'));

        $user = new User([
            'password_changed_at' => Carbon::parse('2026-04-08 10:00:01'),
        ]);

        $this->assertFalse($user->mustChangePassword());

        Carbon::setTestNow();
    }
}
