<?php

namespace Tests\Unit\Services\Auth;

use App\Config\SessionTimeout;
use App\Services\Auth\SessionActivityService;
use App\Services\Auth\SessionTimeoutService;
use Mockery;
use Tests\TestCase;

class SessionTimeoutServiceTest extends TestCase
{
    public function test_check_timeout_returns_expired_when_missing_activity(): void
    {
        $activity = Mockery::mock(SessionActivityService::class);
        $activity->shouldReceive('hasLastActivity')->andReturn(false);
        $activity->shouldReceive('clearLastActivity')->once();
        $activity->shouldReceive('invalidate')->once();

        $service = new SessionTimeoutService($activity);
        $result = $service->checkTimeout();

        $this->assertSame('expired', $result['status']);
    }

    public function test_check_timeout_returns_warning(): void
    {
        $activity = Mockery::mock(SessionActivityService::class);
        $activity->shouldReceive('hasLastActivity')->andReturn(true);
        $activity->shouldReceive('getLastActivity')->andReturn(time() - (SessionTimeout::WARNING_THRESHOLD + 10));

        $service = new SessionTimeoutService($activity);
        $result = $service->checkTimeout();

        $this->assertSame('warning', $result['status']);
    }

    public function test_ping_returns_expired_when_timed_out(): void
    {
        $activity = Mockery::mock(SessionActivityService::class);
        $activity->shouldReceive('hasLastActivity')->andReturn(true);
        $activity->shouldReceive('getLastActivity')->andReturn(time() - (SessionTimeout::TIMEOUT_DURATION + 10));
        $activity->shouldReceive('clearLastActivity')->once();
        $activity->shouldReceive('invalidate')->once();

        $service = new SessionTimeoutService($activity);
        $result = $service->pingActivity();

        $this->assertSame('expired', $result['status']);
    }
}
