<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/helpers/csrf.php';
require_once __DIR__ . '/../../app/helpers/crypto_tools.php';
require_once __DIR__ . '/../../app/middleware/auth.php';

$conn = require __DIR__ . '/../../app/config/database.php';
require_login();
$admin = current_user();

$assets = [];
$res = $conn->query("SELECT id,symbol,name FROM crypto_assets WHERE enabled=1 ORDER BY symbol ASC");
while ($r = $res->fetch_assoc()) $assets[] = $r;

$settings = faucet_settings($conn);

$title = 'Crypto Admin';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/admin_sidebar.php';
?>

<main class="flex-grow-1" style="margin-left:240px; min-height:100vh;">
<div class="container-fluid py-4">

  <div class="d-flex justify-content-between align-items-center mb-3">
    <div>
      <h3 class="fw-bold mb-0">Crypto Admin Console</h3>
      <div class="ab-muted">Debit/credit, reserves, faucet, rates, multisig approvals.</div>
    </div>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-secondary" href="crypto_multisig_review.php">Multi-sig Queue</a>
      <a class="btn btn-outline-secondary" href="crypto_txn_review.php">Send Approvals</a>
    </div>
  </div>

  <?php if ($m = flash('msg')): ?><div class="alert alert-success ab-card"><?= e($m) ?></div><?php endif; ?>
  <?php if ($m = flash('err')): ?><div class="alert alert-danger ab-card"><?= e($m) ?></div><?php endif; ?>

  <div class="row g-4">

    <!-- Adjust Wallet -->
    <div class="col-lg-6">
      <div class="card ab-card p-4">
        <h5 class="fw-bold mb-2">Admin Credit / Debit</h5>
        <form method="post" action="crypto_wallet_adjust.php" class="row g-3">
          <?= csrf_field() ?>

          <div class="col-md-6">
            <label class="form-label">User ID</label>
            <input name="user_id" class="form-control" required>
          </div>

          <div class="col-md-6">
            <label class="form-label">Asset</label>
            <select name="asset_id" class="form-select" required>
              <?php foreach ($assets as $a): ?>
                <option value="<?= (int)$a['id'] ?>"><?= e($a['symbol']) ?> — <?= e($a['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>

          <div class="col-md-6">
            <label class="form-label">Mode</label>
            <select name="mode" class="form-select">
              <option value="credit">Credit</option>
              <option value="debit">Debit (Burn)</option>
            </select>
          </div>

          <div class="col-md-6">
            <label class="form-label">Source</label>
            <select name="source" class="form-select">
              <option value="platform">Platform</option>
              <option value="cold">Cold Reserve</option>
            </select>
          </div>

          <div class="col-md-6">
            <label class="form-label">Amount</label>
            <input name="amount" type="number" step="0.00000001" class="form-control" required>
          </div>

          <div class="col-md-6">
            <label class="form-label">Note (optional)</label>
            <input name="note" class="form-control">
          </div>

          <div class="col-12">
            <button class="btn btn-success">Submit</button>
          </div>
        </form>
      </div>
    </div>

    <!-- Faucet + Rates quick links -->
    <div class="col-lg-6">
      <div class="card ab-card p-4">
        <h5 class="fw-bold mb-2">Faucet & Rates</h5>
        <div class="ab-muted mb-3">
          Faucet: <?= $settings['enabled'] ? 'ENABLED' : 'DISABLED' ?> • Per claim: <?= e((string)$settings['per_claim']) ?> • Daily limit: <?= e((string)$settings['daily_limit']) ?>
        </div>
        <div class="d-flex flex-wrap gap-2">
          <a class="btn btn-outline-primary" href="crypto_rates.php">Manage Exchange Rates</a>
          <a class="btn btn-outline-primary" href="crypto_reserve.php">Manage Cold Reserve</a>
          <a class="btn btn-outline-primary" href="crypto_faucet_admin.php">Manage Faucet</a>
        </div>
      </div>
    </div>

  </div>

</div>
</main>

<?php include __DIR__ . '/../../views/partials/foot.php'; ?>
