'failure', 'message' => 'Unauthorized access. Admin role required.']); exit; } try { $countryCode = filterRequest('country_code'); $limit = filterRequest('limit', 'int') ?? 100; $sql = "SELECT * FROM pricing_stability_log"; $params = []; if ($countryCode) { $sql .= " WHERE country_code = :country"; $params[':country'] = strtoupper($countryCode); } $sql .= " ORDER BY evaluated_at DESC LIMIT :limit"; $stmt = $con->prepare($sql); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); foreach ($params as $key => $val) { $stmt->bindValue($key, $val); } $stmt->execute(); $log = $stmt->fetchAll(PDO::FETCH_ASSOC); // ملخص سريع لآخر تصنيف لكل دولة $stmtLatest = $con->query(" SELECT l1.* FROM pricing_stability_log l1 INNER JOIN ( SELECT country_code, MAX(evaluated_at) AS max_time FROM pricing_stability_log GROUP BY country_code ) l2 ON l1.country_code = l2.country_code AND l1.evaluated_at = l2.max_time "); $latestPerCountry = $stmtLatest->fetchAll(PDO::FETCH_ASSOC); jsonSuccess([ 'log' => $log, 'latest_per_country' => $latestPerCountry, ]); } catch (Exception $e) { error_log("[get_pricing_stability_log.php] Error: " . $e->getMessage()); jsonError("Failed to fetch pricing stability log: " . $e->getMessage()); }