getMessage() . "\n"); } echo "Starting Gemini Market Advisor Engine v2...\n"; // 1. إنشاء جدول الإحصائيات الذكية إذا لم يكن موجوداً $sqlInit = " CREATE TABLE IF NOT EXISTS `gemini_market_insights` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `insight_html` TEXT NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; "; $con->exec($sqlInit); // 2. سحب المعادلات المُطوّرة (مع الفئات والمؤشرات) $stmt = $con->query(" SELECT csf.*, (SELECT surge_multiplier FROM competitor_surge_insights WHERE competitor_name = csf.competitor_name AND country_code = csf.country_code ORDER BY detected_at DESC LIMIT 1) as recent_surge FROM competitor_secret_formulas csf WHERE csf.tier IS NOT NULL ORDER BY csf.country_code, csf.competitor_name, csf.tier "); $formulas = $stmt->fetchAll(PDO::FETCH_ASSOC); if (empty($formulas)) { echo "No enriched formulas found. Run pricing-engine first.\n"; exit; } // 3. تمرير البيانات المُثراة إلى Gemini $geminiService = new SiroGeminiService(); echo "Sending enriched data to Gemini AI for strategic analysis...\n"; $result = $geminiService->analyzeCompetitorFormulas($formulas); if ($result && $result['status'] === 'success') { $htmlReport = $result['html_report']; $stmtInsert = $con->prepare("INSERT INTO gemini_market_insights (insight_html) VALUES (:html)"); $stmtInsert->execute([':html' => $htmlReport]); echo "Gemini analysis saved successfully! Admin can view the strategic report.\n"; } else { echo "Failed to get analysis from Gemini. Check API keys and logs.\n"; }