<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');

define('DB_HOST', 'localhost');
define('DB_USER', 'kdsmf_eppd');
define('DB_PASS', '8DL!AesMeEt(%mgc');
define('DB_NAME', 'kdsmf_epd');

$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error) {
    die(json_encode(['status' => 'error', 'message' => 'Connection failed: ' . $conn->connect_error]));
}

$wallet  = trim($_POST['wallet']  ?? '');
$amount  = floatval($_POST['amount']  ?? 0);
$network = trim($_POST['network'] ?? 'BSC-BEP20');
$tx_hash = trim($_POST['tx_hash'] ?? '');

// ── Validation ──────────────────────────────────────
if (empty($wallet)) {
    echo json_encode(['status' => 'error', 'message' => 'Wallet missing']); exit;
}
if (empty($tx_hash) || strlen($tx_hash) < 5) {
    echo json_encode(['status' => 'error', 'message' => 'Transaction hash required']); exit;
}
if ($amount < 1) {
    echo json_encode(['status' => 'error', 'message' => 'Invalid amount']); exit;
}

// ── Duplicate TX check ──────────────────────────────
$s = $conn->prepare("SELECT id FROM deposits WHERE tx_hash = ?");
$s->bind_param("s", $tx_hash);
$s->execute();
if ($s->get_result()->num_rows > 0) {
    echo json_encode(['status' => 'success', 'message' => 'Deposit already submitted!']);
    exit;
}
$s->close();

// ── User fetch ──────────────────────────────────────
$s = $conn->prepare("SELECT name, account_id FROM users WHERE LOWER(wallet) = LOWER(?)");
$s->bind_param("s", $wallet);
$s->execute();
$user = $s->get_result()->fetch_assoc();
$s->close();

if (!$user) {
    echo json_encode(['status' => 'error', 'message' => 'User not found. Please login again.']); exit;
}

$name       = $user['name']       ?? '';
$account_id = $user['account_id'] ?? '';

// Package type
$package = 'standard';
if ($amount <= 60)  $package = 'starter';
elseif ($amount <= 250) $package = 'basic';
elseif ($amount <= 500) $package = 'standard';
else $package = 'premium';

// ── INSERT — status = pending ───────────────────────
$sql = "INSERT INTO deposits  
            (wallet, amount, package_type, network, tx_hash, notes, status, created_at)
        VALUES  
            (?, ?, ?, ?, ?, 'User submitted', 'pending', NOW())";

$stmt = $conn->prepare($sql);
if (!$stmt) {
    echo json_encode(['status' => 'error', 'message' => 'Query error: ' . $conn->error]); exit;
}

// Yahan $network variable 'BSC (BEP20)' ko handle karega
$stmt->bind_param("sdsss", $wallet, $amount, $package, $network, $tx_hash);

$stmt->bind_param("sdsss", $wallet, $amount, $package, $network, $tx_hash);

if ($stmt->execute()) {
    $stmt->close();
    $conn->close();
    echo json_encode([
        'status'  => 'success',
        'message' => 'Deposit submitted! Pending admin approval.',
        'data'    => ['amount' => $amount, 'package' => $package]
    ]);
} else {
    $err = $conn->error;
    $stmt->close();
    $conn->close();
    echo json_encode(['status' => 'error', 'message' => 'DB insert error: ' . $err]);
}
?>