<?php

namespace App\Filament\Pages;

use Filament\Pages\Page;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use BackedEnum;
use Filament\Support\Icons\Heroicon;

class EvaluasiModel extends Page
{
    protected string $view = 'filament.pages.evaluasi-model';

    protected static ?string $navigationLabel = 'Evaluasi Model ML';

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedChartBar;

    protected static ?string $title = 'Evaluasi Model: TF-IDF & Naive Bayes';

    protected static ?string $slug = 'evaluasi-model';

    protected static ?int $navigationSort = 10;

    public array $evaluasiData = [];
    public string $error = '';

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

    public function loadEvaluasi(): void
    {
        try {
            $response = Http::timeout(30)->get('http://127.0.0.1:5000/evaluate');

            if ($response->successful()) {
                $this->evaluasiData = $response->json();
            } else {
                $this->error = 'Flask API mengembalikan status error: ' . $response->status();
            }
        } catch (\Exception $e) {
            Log::error('Evaluasi Model Error: ' . $e->getMessage());
            $this->error = 'Tidak dapat terhubung ke ML API (Flask). Pastikan server Flask berjalan di http://127.0.0.1:5000';
        }
    }

    public function refresh(): void
    {
        $this->loadEvaluasi();
    }
}
