<?php
session_start();$origin=$_SERVER['HTTP_ORIGIN']??'';$allowed=["https://enerliths.com","http://localhost:5173"];if(in_array($origin,$allowed,true))header("Access-Control-Allow-Origin: $origin");header("Vary: Origin");header("Access-Control-Allow-Credentials: true");header("Access-Control-Allow-Headers: Content-Type");header("Access-Control-Allow-Methods: GET, POST, OPTIONS");header("Content-Type: application/json");if($_SERVER['REQUEST_METHOD']==='OPTIONS')exit();if(!isset($_SESSION['user_id'])){http_response_code(401);echo json_encode(["success"=>false,"message"=>"Please sign in first."]);exit;}require_once("../config/db.php");
$conn->query("CREATE TABLE IF NOT EXISTS copy_trading_settings (id TINYINT PRIMARY KEY, enabled TINYINT(1) NOT NULL DEFAULT 1)");$conn->query("INSERT IGNORE INTO copy_trading_settings (id,enabled) VALUES (1,1)");$conn->query("CREATE TABLE IF NOT EXISTS copy_subscriptions (id INT AUTO_INCREMENT PRIMARY KEY, follower_id INT NOT NULL UNIQUE, leader_id INT NOT NULL, active TINYINT(1) NOT NULL DEFAULT 1, activation_fee_paid TINYINT(1) NOT NULL DEFAULT 0, activation_paid_at TIMESTAMP NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX (leader_id))");$feeColumn=$conn->query("SHOW COLUMNS FROM copy_subscriptions LIKE 'activation_fee_paid'");if($feeColumn&&$feeColumn->num_rows===0)$conn->query("ALTER TABLE copy_subscriptions ADD COLUMN activation_fee_paid TINYINT(1) NOT NULL DEFAULT 0, ADD COLUMN activation_paid_at TIMESTAMP NULL");$conn->query("CREATE TABLE IF NOT EXISTS copy_trading_activation_fees (id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL UNIQUE, amount DECIMAL(10,2) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");$columns=$conn->query("SHOW COLUMNS FROM users LIKE 'copy_trading_enabled'");if($columns&&$columns->num_rows===0)$conn->query("ALTER TABLE users ADD COLUMN copy_trading_enabled TINYINT(1) NOT NULL DEFAULT 1");$providerColumn=$conn->query("SHOW COLUMNS FROM users LIKE 'copy_provider_enabled'");if($providerColumn&&$providerColumn->num_rows===0)$conn->query("ALTER TABLE users ADD COLUMN copy_provider_enabled TINYINT(1) NOT NULL DEFAULT 0");$approvalColumn=$conn->query("SHOW COLUMNS FROM users LIKE 'copy_provider_admin_approved'");if($approvalColumn&&$approvalColumn->num_rows===0)$conn->query("ALTER TABLE users ADD COLUMN copy_provider_admin_approved TINYINT(1) NOT NULL DEFAULT 0");function input_json(){return json_decode(file_get_contents('php://input'),true)?:[];}
?>
