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

session_boot();
require_login();

$conn = require __DIR__ . '/../../app/config/database.php';
require_admin($conn);

$userId = (int)($_GET['id'] ?? 0);
if (!$userId) redirect('customers.php');

/* Customer */
$st = $conn->prepare("SELECT * FROM users WHERE id=?");
$st->bind_param("i", $userId);
$st->execute();
$user = $st->get_result()->fetch_assoc();
if (!$user) die('User not found');

/* Accounts */
$accounts = [];
$res = $conn->query("SELECT * FROM accounts WHERE user_id={$userId} ORDER BY id DESC");
while ($r = $res->fetch_assoc()) $accounts[] = $r;

/* Ledger (last 200) */
$tx = [];
$res = $conn->query("
  SELECT le.*, lt.ref, lt.type, lt.status
  FROM ledger_entries le
  JOIN ledger_txn lt ON lt.id = le.txn_id
  WHERE le.account_id IN (SELECT id FROM accounts WHERE user_id={$userId})
  ORDER BY le.id DESC
  LIMIT 200
");
while ($r = $res->fetch_assoc()) $tx[] = $r;

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

<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"><?= e($user['full_name']) ?></h3>
      <div class="text-muted small"><?= e($user['email']) ?> • <?= e($user['phone'] ?? '-') ?></div>
    </div>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-secondary btn-sm" href="customers.php">Back</a>
      <a class="btn btn-outline-dark btn-sm" href="impersonate.php?id=<?= (int)$user['id'] ?>">Login as customer</a>
    </div>
  </div>

  <div class="row g-3">
    <div class="col-lg-4">
      <div class="card ab-card p-4">
        <div class="fw-bold mb-2">Accounts</div>
        <?php foreach ($accounts as $a): ?>
          <div class="border rounded p-2 mb-2">
            <div class="fw-semibold"><?= e($a['account_type']) ?> • <?= e($a['currency']) ?></div>
            <div class="text-muted small"><?= e($a['account_no']) ?></div>
          </div>
        <?php endforeach; ?>
        <?php if (!$accounts): ?>
          <div class="text-muted">No accounts found.</div>
        <?php endif; ?>
      </div>
    </div>

    <div class="col-lg-8">
      <div class="card ab-card p-3">
        <div class="fw-bold mb-2 px-2">Recent Ledger Entries</div>
        <div class="table-responsive">
          <table class="table table-sm align-middle mb-0">
            <thead>
              <tr>
                <th>Ref</th><th>Type</th><th>Dir</th><th>Amount</th><th>Status</th><th>Date</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($tx as $r): ?>
              <tr>
                <td class="fw-semibold"><?= e($r['ref']) ?></td>
                <td><?= e($r['type']) ?></td>
                <td><span class="badge <?= $r['direction']==='credit'?'text-bg-success':'text-bg-danger' ?>"><?= strtoupper(e($r['direction'])) ?></span></td>
                <td><?= e($r['currency']) ?> <?= number_format((float)$r['amount'],2) ?></td>
                <td><?= e($r['status']) ?></td>
                <td><?= e($r['created_at'] ?? '') ?></td>
              </tr>
              <?php endforeach; ?>
              <?php if (!$tx): ?>
              <tr><td colspan="6" class="text-center text-muted py-4">No transactions found.</td></tr>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

</div>

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