<?php

namespace App\Livewire\Monitoring;

use App\Helpers\SystemHealthHelper;
use App\Models\AuditLog;
use App\Models\FingerprintMachine;

use App\Models\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class SystemMonitoringIndex extends Component
{
    use AuthorizesRequests;

    public array $dbHealth = [];
    public array $cronHealth = [];

    public function mount(): void
    {
        $this->refreshHealthChecks();
    }

    public function refreshHealthChecks(): void
    {
        $this->authorize('viewAny', User::class);

        $this->dbHealth = SystemHealthHelper::checkDatabaseConnection();
        $this->cronHealth = SystemHealthHelper::checkCronjobStatus();
        session()->flash('toast', ['type' => 'info', 'message' => 'Uji kesehatan sistem (Database & Cronjob) diperbarui.']);
    }

    public function render()
    {
        $this->authorize('viewAny', User::class);

        $machines = FingerprintMachine::all();

        return view('livewire.monitoring.system-monitoring-index', [
            'machines' => $machines,
        ])->layout('components.layouts.app');
    }
}
