<?php
// wallet/tenant/history.php
session_start();
require_once __DIR__ . "/../../db_connect.php";

if (!isset($_SESSION['user_id'])) {
    exit("Access denied: not logged in.");
}

$current_user_id = (int)$_SESSION['user_id'];
$tenant_id = (int)($_GET['tenant_id'] ?? $current_user_id);

// ---------------------------
// Access Control
// ---------------------------
$stmt = $conn_rbe->prepare("SELECT role FROM rbe_roles WHERE user_id = ? LIMIT 1");
$stmt->execute([$current_user_id]);
$role = $stmt->fetchColumn();

if ($role === 'tenant' && $tenant_id !== $current_user_id) {
    exit("Access denied: tenants can only view their own history.");
}

// ---------------------------
// Fetch tenant info
// ---------------------------
$stmt = $conn_rbe->prepare("SELECT fullname FROM rbe_users WHERE id = ?");
$stmt->execute([$tenant_id]);
$tenant_name = $stmt->fetchColumn() ?: "Unknown";

// ---------------------------
// Fetch transactions
// ---------------------------
$stmt = $conn_rbe->prepare("
    SELECT transaction_type, amount, description, created_at
    FROM wallet_transactions
    WHERE user_type = 'tenant' AND user_id = ?
    ORDER BY created_at DESC
    LIMIT 100
");
$stmt->execute([$tenant_id]);
$transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Wallet History - <?= htmlspecialchars($tenant_name) ?></title>
  <style>
    body { font-family: Arial, sans-serif; background: #051a25; color: #fff; padding: 20px; }
    .card { background: #122c4c; padding: 20px; border-radius: 12px; max-width: 800px; margin: auto; }
    h1 { color: #7ed957; margin-bottom: 15px; }
    table { width: 100%; border-collapse: collapse; margin-top: 10px; }
    th, td { padding: 10px; border: 1px solid #7ed957; text-align: center; }
    th { background: #1b3a4d; color: #7ed957; }
    tr:nth-child(even) { background: #0b1f33; }
    .btn { background: #7ed957; color: #000; padding: 8px 16px; border-radius: 8px; text-decoration: none; font-weight: bold; }
    .btn:hover { background: #00cc77; }
    .pos { color: #7ed957; font-weight: bold; } /* deposits */
    .neg { color: #ff7777; font-weight: bold; } /* deductions/fees */
  </style>
</head>
<body>
  <div class="card">
    <h1>💳 Wallet History – <?= htmlspecialchars($tenant_name) ?></h1>

    <?php if (empty($transactions)): ?>
      <p>No transactions yet.</p>
    <?php else: ?>
      <table>
        <tr>
          <th>Date</th>
          <th>Type</th>
          <th>Amount (R)</th>
          <th>Description</th>
        </tr>
        <?php foreach ($transactions as $t): 
          $type = ucwords(str_replace('_',' ', $t['transaction_type']));
          $amount = (float)$t['amount'];
          $is_deduction = in_array($t['transaction_type'], ['usage_deduction','fee']);
        ?>
          <tr>
            <td><?= htmlspecialchars($t['created_at']) ?></td>
            <td><?= htmlspecialchars($type) ?></td>
            <td class="<?= $is_deduction ? 'neg' : 'pos' ?>">
              <?= $is_deduction ? '- ' : '+ ' ?><?= number_format($amount, 2) ?>
            </td>
            <td><?= htmlspecialchars($t['description']) ?></td>
          </tr>
        <?php endforeach; ?>
      </table>
    <?php endif; ?>

    <p style="margin-top:15px;">
      <a class="btn" href="../../tenant-dashboard.php">⬅ Back to Tenant Dashboard</a>
    </p>
  </div>
</body>
</html>
