'DB connection failed']); exit; } $action = $_GET['action'] ?? 'winners'; switch($action) { case 'winners': $isp = $_GET['isp'] ?? null; $sql = "SELECT id, isp_target, send_method, inbox_rate, return_path, from_name, from_email, reply_to, subject_template, header_1_name, header_1_value, header_2_name, header_2_value, header_3_name, header_3_value, header_4_name, header_4_value, header_5_name, header_5_value, content_type, encoding, charset, domain_used, ip_used, total_sent FROM admin.brain_configs WHERE status='winner'"; $params = []; if ($isp) { $sql .= " AND isp_target ILIKE $1"; $params[] = '%'.$isp.'%'; } $sql .= " ORDER BY inbox_rate DESC LIMIT 20"; $r = $params ? pg_query_params($db, $sql, $params) : pg_query($db, $sql); $rows = []; if($r) while ($row = pg_fetch_assoc($r)) $rows[] = $row; echo json_encode(['success'=>true,'count'=>count($rows),'winners'=>$rows]); break; case 'stats': $r = pg_query($db, "SELECT COUNT(*) FILTER(WHERE status='winner') as winners, COUNT(*) as total, ROUND(AVG(inbox_rate)::numeric,1) as avg_rate FROM admin.brain_configs"); echo json_encode(['success'=>true,'stats'=>pg_fetch_assoc($r)]); break; case 'inject': $id = intval($_GET['id'] ?? 0); if (!$id) { echo json_encode(['error'=>'Missing id']); exit; } $r = pg_query_params($db, "SELECT * FROM admin.brain_configs WHERE id=$1", [$id]); $row = pg_fetch_assoc($r); echo json_encode(['success'=>true,'config'=>$row]); break; default: echo json_encode(['error'=>'Unknown action']); }