<?php

require_once __DIR__ . '/../config/app.php';
require_once __DIR__ . '/../config/database.php';

function loadTrainingData(): array
{
    $db = getDB();
    $stmt = $db->query('SELECT pekerjaan_ortu, penghasilan_ortu, jumlah_saudara, kepemilikan_kps, kepemilikan_kip, kelas FROM data_latih');
    return $stmt->fetchAll();
}

function runClassification(array $input): array
{
    require_once __DIR__ . '/../classes/KelayakanRules.php';
    require_once __DIR__ . '/../classes/NaiveBayes.php';

    $rulesResult = KelayakanRules::classify($input);
    $training = loadTrainingData();
    $nb = new NaiveBayes($training);
    $nbResult = $nb->predict($input);

    return [
        'kelas' => $rulesResult['kelas'],
        'prob_layak' => $nbResult['prob_layak'],
        'prob_tidak_layak' => $nbResult['prob_tidak_layak'],
    ];
}
