<?php
// ─────────────────────────────────────────────
// api/analyze.php
// KI-Analyse starten (manuell oder via Queue)
// ─────────────────────────────────────────────

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/ai_engine.php';

c2c_cors_headers();
require_api_token();

$clientId = (int)($_GET['client_id'] ?? ($_POST['client_id'] ?? 0));
if (!$clientId) json_response(['error' => 'client_id fehlt'], 422);

try {
    $analysis = C2CAIEngine::analyzeClient($clientId);
    json_response([
        'success'    => true,
        'analysis_id'=> $analysis['id'],
        'summary'    => $analysis['profile_summary'],
        'pathways'   => count($analysis['pathways'] ?? []) . ' Einwanderungswege gefunden',
        'recommended'=> $analysis['recommended_pathway'] ?? '',
        'crs_estimate'=> $analysis['crs_estimate'] ?? null,
    ]);
} catch (Exception $e) {
    error_log("Analyze Error client $clientId: " . $e->getMessage());
    json_response(['error' => $e->getMessage()], 500);
}
