<?php

namespace Tests\Feature;

use App\Services\SystemHealthService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;

class SystemHealthTest extends TestCase
{
    use RefreshDatabase;

    public function test_health_snapshot_reports_core_services_without_external_device_calls(): void
    {
        Cache::put(SystemHealthService::HEARTBEAT_KEY, now()->toIso8601String(), 60);

        $health = app(SystemHealthService::class)->snapshot();
        $checks = collect($health['checks'])->keyBy('key');

        $this->assertSame('healthy', $checks['database']['status']);
        $this->assertSame('healthy', $checks['cache']['status']);
        $this->assertSame('healthy', $checks['scheduler']['status']);
        $this->assertSame('healthy', $checks['queue']['status']);
        $this->assertArrayHasKey('mikrotik_total', $health['metrics']);
        $this->assertArrayHasKey('olt_total', $health['metrics']);
    }

    public function test_missing_scheduler_heartbeat_is_reported_as_warning(): void
    {
        Cache::forget(SystemHealthService::HEARTBEAT_KEY);

        $scheduler = collect(app(SystemHealthService::class)->snapshot()['checks'])
            ->firstWhere('key', 'scheduler');

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