<?php

namespace Tests\Feature\Api\V1\Auth;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class SessionTimeoutControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_config_returns_timeout_values(): void
    {
        $user = $this->seedUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/session/timeout-config');

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $this->assertArrayHasKey('timeout', $response->json('data.config'));
        $this->assertArrayHasKey('keep_alive_url', $response->json('data.config'));
    }

    public function test_check_timeout_returns_active_status(): void
    {
        $user = $this->seedUser();
        Sanctum::actingAs($user);

        $response = $this
            ->withSession(['last_activity' => time() - 10])
            ->getJson('/api/v1/session/check-timeout');

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $response->assertJsonPath('data.session.status', 'active');
    }

    public function test_check_timeout_returns_warning_status(): void
    {
        $user = $this->seedUser();
        Sanctum::actingAs($user);

        $response = $this
            ->withSession(['last_activity' => time() - 1000])
            ->getJson('/api/v1/session/check-timeout');

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $response->assertJsonPath('data.session.status', 'warning');
    }

    public function test_check_timeout_returns_expired_status(): void
    {
        $user = $this->seedUser();
        Sanctum::actingAs($user);

        $response = $this
            ->withSession(['last_activity' => time() - 5000])
            ->getJson('/api/v1/session/check-timeout');

        $response->assertStatus(401);
        $response->assertJsonPath('status', false);
        $response->assertJsonPath('errors.status', 'expired');
    }

    public function test_ping_updates_activity(): void
    {
        $user = $this->seedUser();
        Sanctum::actingAs($user);

        $response = $this
            ->withSession(['last_activity' => time() - 10])
            ->postJson('/api/v1/session/ping');

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $response->assertJsonPath('data.session.status', 'active');
    }

    public function test_ping_returns_expired_when_missing_activity(): void
    {
        $user = $this->seedUser();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/session/ping');

        $response->assertStatus(401);
        $response->assertJsonPath('status', false);
    }

    public function test_requires_authentication(): void
    {
        $response = $this->getJson('/api/v1/session/timeout-config');

        $response->assertStatus(401);
    }

    private function seedUser(): User
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '9999999999',
            'email' => 'session@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);

        return User::query()->findOrFail($userId);
    }
}
