<?php
require_once("_bootstrap.php");
$id=(int)$_SESSION['user_id'];
$metrics=$conn->query("SELECT COUNT(*) trades,COALESCE(SUM(net_pl),0) profit,COALESCE(AVG(net_pl>0)*100,0) win_rate FROM trade_sessions WHERE user_id=$id AND created_at>=DATE_SUB(NOW(),INTERVAL 30 DAY)")->fetch_assoc();
$user=$conn->query("SELECT copy_trading_enabled,copy_provider_enabled,copy_provider_admin_approved FROM users WHERE id=$id")->fetch_assoc();
$automatic=(int)$metrics['trades']>=20&&(float)$metrics['profit']>0&&(float)$metrics['win_rate']>=55;
$eligible=(bool)$user['copy_trading_enabled']&&($automatic||(bool)$user['copy_provider_admin_approved']);
echo json_encode(["success"=>true,"provider_enabled"=>(bool)$user['copy_provider_enabled'],"admin_approved"=>(bool)$user['copy_provider_admin_approved'],"eligible"=>$eligible,"metrics"=>["trades"=>(int)$metrics['trades'],"profit"=>(float)$metrics['profit'],"win_rate"=>(float)$metrics['win_rate']],"requirements"=>["trades"=>20,"positive_profit"=>true,"win_rate"=>55]]);
?>
