<?php
/**
 * Install C3P Capital tables (MT5 live reserve ledger).
 * php run-c3p-capital-install.php
 */
declare(strict_types=1);

if (!defined('ACC_BASE_PATH')) {
    define('ACC_BASE_PATH', __DIR__);
    require __DIR__ . '/standalone/bootstrap.php';
}

header('Content-Type: application/json; charset=utf-8');

global $wpdb;
$pre     = $wpdb->prefix;
$charset = 'DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';

require_once ACC_PATH . 'includes/helpers/c3p-capital.php';

$wpdb->query("CREATE TABLE IF NOT EXISTS {$pre}c3p_capital_state (
    account_login BIGINT UNSIGNED NOT NULL,
    saved_reserve DECIMAL(18,2) NOT NULL DEFAULT 0,
    cycle_anchor DECIMAL(18,2) NOT NULL DEFAULT 0,
    cycle_phase TINYINT NOT NULL DEFAULT 0,
    lot_scale_equity DECIMAL(18,2) NOT NULL DEFAULT 0,
    reserve_total_injected DECIMAL(18,2) NOT NULL DEFAULT 0,
    broker_name VARCHAR(64) NOT NULL DEFAULT '',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (account_login)
) {$charset}");

$wpdb->query("CREATE TABLE IF NOT EXISTS {$pre}c3p_capital_ledger (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    account_login BIGINT UNSIGNED NOT NULL,
    event_time DATETIME NOT NULL,
    entry_type VARCHAR(32) NOT NULL,
    amount DECIMAL(18,2) NOT NULL DEFAULT 0,
    saved_after DECIMAL(18,2) NOT NULL DEFAULT 0,
    balance_after DECIMAL(18,2) NOT NULL DEFAULT 0,
    margin_level DECIMAL(10,2) NOT NULL DEFAULT 0,
    symbol VARCHAR(32) NOT NULL DEFAULT '',
    cluster_tag VARCHAR(64) NOT NULL DEFAULT '',
    reason VARCHAR(255) NOT NULL DEFAULT '',
    deal_ticket BIGINT UNSIGNED NOT NULL DEFAULT 0,
    status VARCHAR(20) NOT NULL DEFAULT 'confirmed',
    meta_json JSON NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY account_login (account_login),
    KEY event_time (event_time),
    KEY entry_type (entry_type)
) {$charset}");

$wpdb->query("CREATE TABLE IF NOT EXISTS {$pre}c3p_capital_pending (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    account_login BIGINT UNSIGNED NOT NULL,
    pending_type ENUM('WITHDRAW','DEPOSIT','RECALL') NOT NULL,
    amount DECIMAL(18,2) NOT NULL,
    reason VARCHAR(255) NOT NULL DEFAULT '',
    cluster_tag VARCHAR(64) NOT NULL DEFAULT '',
    status ENUM('pending','matched','expired','cancelled') NOT NULL DEFAULT 'pending',
    matched_deal_ticket BIGINT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    expires_at DATETIME NULL,
    matched_at DATETIME NULL,
    PRIMARY KEY (id),
    KEY account_login_status (account_login, status)
) {$charset}");

echo wp_json_encode([
    'success' => true,
    'tables'  => [
        C3P_Capital::state_table(),
        C3P_Capital::ledger_table(),
        C3P_Capital::pending_table(),
    ],
    'api_key_hint' => defined('C3P_CAPITAL_API_KEY') ? '(set in config/env)' : 'missing',
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
