<?php
require_once("_bootstrap.php");$userId=(int)$_SESSION['user_id'];$setting=$conn->query("SELECT enabled FROM copy_trading_settings WHERE id=1")->fetch_assoc();$subscription=null;$sub=$conn->query("SELECT s.*,u.full_name leader_name FROM copy_subscriptions s JOIN users u ON u.id=s.leader_id WHERE s.follower_id=$userId AND s.active=1 LIMIT 1");if($sub&&$sub->num_rows)$subscription=$sub->fetch_assoc();$sql="SELECT u.id,u.full_name,COALESCE(SUM(ts.net_pl),0) profit,COUNT(ts.id) trades,COALESCE(AVG(ts.net_pl>0)*100,0) win_rate,(SELECT COUNT(*) FROM copy_subscriptions cs WHERE cs.leader_id=u.id AND cs.active=1) followers FROM users u LEFT JOIN trade_sessions ts ON ts.user_id=u.id AND ts.created_at>=DATE_SUB(NOW(),INTERVAL 30 DAY) WHERE u.id<>$userId AND u.role='user' AND COALESCE(u.copy_trading_enabled,1)=1 AND COALESCE(u.copy_provider_enabled,0)=1 GROUP BY u.id HAVING (trades>=20 AND profit>0 AND win_rate>=55) OR MAX(COALESCE(u.copy_provider_admin_approved,0))=1 ORDER BY profit DESC,trades DESC LIMIT 60";$traders=[];$r=$conn->query($sql);while($r&&($row=$r->fetch_assoc())){$profit=(float)$row['profit'];$risk=$profit>=20?'Low':($profit>=0?'Medium':'High');$initials='';foreach(preg_split('/\s+/',trim($row['full_name'])) as $word)$initials.=strtoupper(substr($word,0,1));$traders[]=["id"=>(int)$row['id'],"name"=>$row['full_name'],"initials"=>substr($initials,0,2),"profit"=>$profit,"trades"=>(int)$row['trades'],"win_rate"=>(float)$row['win_rate'],"followers"=>(int)$row['followers'],"risk"=>$risk];}echo json_encode(["success"=>true,"enabled"=>(bool)($setting['enabled']??0),"subscription"=>$subscription,"traders"=>$traders]);
?>
