<?php
/**
 * Admin Loyalty Points Page
 * View and manage loyalty points
 */

$pageTitle = 'Puncte Fidelitate';

require_once __DIR__ . '/includes/init.php';
require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/../../includes/Loyalty.php';

$db = Database::getInstance();
$loyalty = new Loyalty();

// Get all users with loyalty points
$usersWithPoints = $db->fetchAll(
    "SELECT u.id, u.first_name, u.last_name, u.email, u.loyalty_points,
            (SELECT COUNT(*) FROM loyalty_points lp WHERE lp.user_id = u.id AND lp.type = 'earned') as transactions_earned,
            (SELECT COUNT(*) FROM loyalty_points lp WHERE lp.user_id = u.id AND lp.type = 'used') as transactions_used
     FROM users u
     WHERE u.loyalty_points > 0
     ORDER BY u.loyalty_points DESC
     LIMIT 100"
);

// Get recent transactions
$recentTransactions = $db->fetchAll(
    "SELECT lp.*, u.first_name, u.last_name, u.email
     FROM loyalty_points lp
     LEFT JOIN users u ON lp.user_id = u.id
     ORDER BY lp.created_at DESC
     LIMIT 50"
);
?>

<div class="content">
  <div class="px-3 px-xxl-4 py-3 py-lg-4">
    <div class="card mb-3">
      <div class="card-header">
        <h5 class="mb-0">Puncte Fidelitate</h5>
      </div>
      <div class="card-body">
        <div class="row g-3 mb-4">
          <div class="col-md-4">
            <div class="border rounded p-3 text-center">
              <h6 class="text-600 mb-1">Total utilizatori cu puncte</h6>
              <h3 class="mb-0"><?php echo count($usersWithPoints); ?></h3>
            </div>
          </div>
          <div class="col-md-4">
            <div class="border rounded p-3 text-center">
              <h6 class="text-600 mb-1">Total puncte distribuite</h6>
              <h3 class="mb-0">
                <?php 
                $totalPoints = $db->fetchOne("SELECT SUM(points) as total FROM loyalty_points WHERE type = 'earned'")['total'] ?? 0;
                echo number_format($totalPoints);
                ?>
              </h3>
            </div>
          </div>
          <div class="col-md-4">
            <div class="border rounded p-3 text-center">
              <h6 class="text-600 mb-1">Total puncte folosite</h6>
              <h3 class="mb-0">
                <?php 
                $totalUsed = $db->fetchOne("SELECT SUM(points) as total FROM loyalty_points WHERE type = 'used'")['total'] ?? 0;
                echo number_format($totalUsed);
                ?>
              </h3>
            </div>
          </div>
        </div>

        <h6 class="mb-3">Top utilizatori</h6>
        <div class="table-responsive">
          <table class="table table-sm mb-0">
            <thead class="bg-body-tertiary">
              <tr>
                <th>Client</th>
                <th>Puncte</th>
                <th>Tranzacții câștigate</th>
                <th>Tranzacții folosite</th>
              </tr>
            </thead>
            <tbody>
              <?php if (empty($usersWithPoints)): ?>
              <tr>
                <td colspan="4" class="text-center py-4">Nu există utilizatori cu puncte fidelitate.</td>
              </tr>
              <?php else: ?>
              <?php foreach ($usersWithPoints as $user): ?>
              <tr>
                <td>
                  <div><?php echo htmlspecialchars($user['first_name'] . ' ' . $user['last_name']); ?></div>
                  <small class="text-600"><?php echo htmlspecialchars($user['email']); ?></small>
                </td>
                <td><span class="badge badge-subtle-warning"><?php echo number_format($user['loyalty_points']); ?></span></td>
                <td><?php echo number_format($user['transactions_earned'] ?? 0); ?></td>
                <td><?php echo number_format($user['transactions_used'] ?? 0); ?></td>
              </tr>
              <?php endforeach; ?>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <div class="card">
      <div class="card-header">
        <h5 class="mb-0">Tranzacții recente</h5>
      </div>
      <div class="card-body p-0">
        <div class="table-responsive">
          <table class="table table-sm mb-0">
            <thead class="bg-body-tertiary">
              <tr>
                <th>Client</th>
                <th>Tip</th>
                <th>Puncte</th>
                <th>Descriere</th>
                <th>Data</th>
              </tr>
            </thead>
            <tbody>
              <?php if (empty($recentTransactions)): ?>
              <tr>
                <td colspan="5" class="text-center py-4">Nu există tranzacții.</td>
              </tr>
              <?php else: ?>
              <?php foreach ($recentTransactions as $trans): ?>
              <tr>
                <td>
                  <?php if ($trans['first_name']): ?>
                  <div><?php echo htmlspecialchars($trans['first_name'] . ' ' . $trans['last_name']); ?></div>
                  <small class="text-600"><?php echo htmlspecialchars($trans['email']); ?></small>
                  <?php else: ?>
                  <span class="text-600">N/A</span>
                  <?php endif; ?>
                </td>
                <td>
                  <?php if ($trans['type'] === 'earned'): ?>
                  <span class="badge badge-subtle-success">Câștigat</span>
                  <?php else: ?>
                  <span class="badge badge-subtle-danger">Folosit</span>
                  <?php endif; ?>
                </td>
                <td><?php echo number_format($trans['points']); ?></td>
                <td><?php echo htmlspecialchars($trans['description'] ?? 'N/A'); ?></td>
                <td><?php echo date('d.m.Y H:i', strtotime($trans['created_at'])); ?></td>
              </tr>
              <?php endforeach; ?>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>

