<?php
/**
 * API - Create Deposit Request
 */
header('Content-Type: application/json');

require_once '../../config/config.php';
require_once '../../config/database.php';
require_once '../../includes/functions.php';
require_once '../../includes/Auth.php';

if (!auth()->isUserLoggedIn()) {
    jsonError('Unauthorized', 401);
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    jsonError('Method not allowed', 405);
}

$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;

$amount = (float)($input['amount'] ?? 0);
$method = $input['method'] ?? '';
$transactionId = trim($input['transaction_id'] ?? '');

if ($amount < 500) {
    jsonError('Minimum deposit is ₹500');
}

if (empty($transactionId)) {
    jsonError('Transaction ID is required');
}

$depositId = 'DEP' . strtoupper(substr(md5(uniqid()), 0, 10));

dbInsert('deposits', [
    'deposit_id' => $depositId,
    'user_id' => auth()->getUserId(),
    'amount' => $amount,
    'payment_method' => $method,
    'transaction_id' => $transactionId,
    'status' => 'pending'
]);

jsonSuccess('Deposit request submitted', ['deposit_id' => $depositId]);
