<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class DashboardMetricsTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
    }

    public function test_authenticated_admin_can_view_dashboard_with_all_metrics(): void
    {
        $admin = User::where('email', 'admin@smkn1gunungagung.sch.id')->first();
        $admin->update(['force_password_change' => false]);
        $this->actingAs($admin);

        $response = $this->get('/dashboard');
        $response->assertStatus(200);
        $response->assertSee('Dashboard Real-Time Kehadiran SMKN 1 Gunung Agung');
        $response->assertSee('Total Guru');
        $response->assertSee('Total Siswa');
        $response->assertSee('Total Mesin');
        $response->assertSee('Mesin Online');
        $response->assertSee('Mesin Offline');
        $response->assertSee('Sync Hari Ini');
        $response->assertSee('Guru Terlambat');
        $response->assertSee('Siswa Terlambat');
        $response->assertSee('Heatmap Kehadiran (30 Hari Terakhir)');
    }

    public function test_livewire_dashboard_renders_metrics_and_analytics(): void
    {
        $admin = User::where('email', 'admin@smkn1gunungagung.sch.id')->first();
        $admin->update(['force_password_change' => false]);
        $this->actingAs($admin);

        Livewire::test(\App\Livewire\Dashboard::class)
            ->assertViewHas('totalGuru')
            ->assertViewHas('totalSiswa')
            ->assertViewHas('totalMesin')
            ->assertViewHas('mesinOnline')
            ->assertViewHas('mesinOffline')
            ->assertViewHas('syncHariIni')
            ->assertViewHas('guruTerlambat')
            ->assertViewHas('siswaTerlambat')
            ->assertViewHas('heatmapKehadiran')
            ->assertViewHas('trendPersen');
    }
}
