<?php
require_once APP_PATH . '/Core/BaseController.php';
require_once APP_PATH . '/Models/RecommendationEngine.php';
require_once APP_PATH . '/Core/CSRF.php';

class AdmissionController extends BaseController {

    private RecommendationEngine $engine;

    public function __construct() {
        $this->engine = new RecommendationEngine();
    }

    // ── GET /admission-analyzer ──────────────────────────────────
    public function index(): void {
        $db = Database::getInstance();
        $stmt = $db->prepare("SELECT * FROM wassce_results WHERE user_id = ?");
        $stmt->execute([Auth::id()]);
        $wassce = $stmt->fetchAll();

        $analysisResult = null;
        if (!empty($wassce)) {
            $analysisResult = $this->engine->analyzeAdmission(Auth::id());
        }

        $this->view('admission.index', [
            'title'       => 'Admission Eligibility Analyzer — BOAFO',
            'wassce'      => $wassce,
            'analysis'    => $analysisResult,
        ]);
    }

    // ── POST /api/admission/analyze ──────────────────────────────
    public function analyze(): void {
        CSRF::check();
        $result = $this->engine->analyzeAdmission(Auth::id());
        $this->json(['success'=>true,'data'=>$result]);
    }
}
