<?php
header('Content-Type: application/json');

$base = realpath(__DIR__ . '/../../../');
if (file_exists($base . '/app/session.php')) include $base . '/app/session.php';
if (file_exists($base . '/config/db.php')) include $base . '/config/db.php';
if (file_exists($base . '/config/deposit_lib.php')) include $base . '/config/deposit_lib.php';

if (!isset($link)) {
    http_response_code(500);
    echo json_encode(['ok' => false, 'error' => 'Database not available']);
    exit;
}

$body = json_decode(file_get_contents('php://input'), true);
if (!$body) {
    http_response_code(400);
    echo json_encode(['ok' => false, 'error' => 'Invalid JSON']);
    exit;
}

$tx_hash = isset($body['tx_hash']) ? trim($body['tx_hash']) : '';
$amount_usd = isset($body['amount_usd']) ? floatval($body['amount_usd']) : 0;
$amount_crypto = isset($body['amount_crypto']) ? trim((string) $body['amount_crypto']) : '0';
$coin = isset($body['coin']) ? strtolower(trim($body['coin'])) : '';
$credit_coin = isset($body['credit_coin']) ? strtolower(trim($body['credit_coin'])) : $coin;
$purpose = isset($body['purpose']) ? trim($body['purpose']) : 'deposit';
$chain_id = isset($body['chain_id']) ? intval($body['chain_id']) : 0;
$recipient = isset($body['recipient']) ? trim($body['recipient']) : '';

if ($purpose !== 'swap') $purpose = 'deposit';
if ($credit_coin === '') $credit_coin = $coin;

if ($tx_hash === '' || $coin === '' || $amount_usd <= 0) {
    http_response_code(400);
    echo json_encode(['ok' => false, 'error' => 'tx_hash, coin, and amount_usd required']);
    exit;
}

$user_id = isset($_SESSION['user_id']) ? intval($_SESSION['user_id']) : 0;
if ($user_id <= 0) {
    http_response_code(401);
    echo json_encode(['ok' => false, 'error' => 'Not authenticated']);
    exit;
}

$addresses = deposit_platform_addresses();
if (empty($addresses[$coin])) {
    http_response_code(400);
    echo json_encode(['ok' => false, 'error' => 'Unsupported coin']);
    exit;
}

$expected = $addresses[$coin];
if ($recipient === '') $recipient = $expected;
if (!deposit_addresses_match($recipient, $expected)) {
    http_response_code(400);
    echo json_encode(['ok' => false, 'error' => 'Recipient must be the platform wallet for this asset']);
    exit;
}
$recipient = $expected;

deposit_ensure_table($link);

$stmt = mysqli_prepare($link, "INSERT INTO deposits (user_id, tx_hash, coin, credit_coin, purpose, chain_id, recipient, amount_usd, amount_crypto, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending')
ON DUPLICATE KEY UPDATE created_at = created_at");
if (!$stmt) {
    http_response_code(500);
    echo json_encode(['ok' => false, 'error' => 'DB prepare failed']);
    exit;
}

mysqli_stmt_bind_param(
    $stmt,
    'issssisds',
    $user_id,
    $tx_hash,
    $coin,
    $credit_coin,
    $purpose,
    $chain_id,
    $recipient,
    $amount_usd,
    $amount_crypto
);

if (mysqli_stmt_execute($stmt)) {
    echo json_encode(['ok' => true, 'tx_hash' => $tx_hash, 'recipient' => $recipient]);
} else {
    http_response_code(500);
    echo json_encode(['ok' => false, 'error' => 'DB insert failed']);
}
mysqli_stmt_close($stmt);
