<?php
/**
 * ajaxCalls/getIncomeCalc.php
 * Returns full income breakdown for any working-plan package.
 * Called by frontend before showing purchase confirmation.
 */
header('Content-Type: application/json');
require_once '../../connection/db.php';
require_once '../../connection/MatrixEngine.php';

$pkgId = (int)($_GET['pkg_id'] ?? $_POST['pkg_id'] ?? 0);
if (!$pkgId) {
    echo json_encode(['status'=>'error','message'=>'pkg_id required']); exit;
}

$db  = getDB();
$q   = $db->prepare('SELECT * FROM meddolic_config_pool_list WHERE id=? AND poolStatus=1');
$q->execute([$pkgId]);
$pkg = $q->fetch();
if (!$pkg) {
    echo json_encode(['status'=>'error','message'=>'Package not found']); exit;
}

$engine  = new MatrixEngine();
$preview = $engine->calculateIncomePreview($pkgId, (float)$pkg['upgradePrice']);

echo json_encode(['status'=>'ok', 'package'=>$pkg, 'preview'=>$preview]);
