<?php

namespace Tests\Feature\Api\V1\System;

use App\Models\Stats;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class StatsControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_stats_require_authentication(): void
    {
        $this->getJson('/api/v1/stats')
            ->assertStatus(401);
    }

    public function test_stats_index_returns_rows_for_authenticated_user(): void
    {
        Stats::query()->create([
            'students' => 12,
            'teachers' => 3,
            'admins' => 2,
            'users' => 17,
        ]);

        Sanctum::actingAs(User::factory()->create());

        $response = $this->getJson('/api/v1/stats');

        $response->assertOk()
            ->assertJsonPath('status', true)
            ->assertJsonStructure(['status', 'message', 'data']);

        $this->assertSame(12, $response->json('data.0.students'));
    }

    public function test_stats_index_returns_empty_array_when_no_stats(): void
    {
        Sanctum::actingAs(User::factory()->create());

        $this->getJson('/api/v1/stats')
            ->assertOk()
            ->assertJsonPath('status', true)
            ->assertJsonPath('data', []);
    }
}
