<?php
require 'auth.php';
require 'db.php';

$stmt = $pdo->query("
  SELECT u.email, f.title, f.type, fs.opened_at, fs.closed_at,
         fs.pages_read, fs.watched_seconds
  FROM file_sessions fs
  JOIN userdb u ON u.id = fs.user_id
  JOIN files f ON f.id = fs.file_id
  ORDER BY fs.opened_at DESC
");
$rows = $stmt->fetchAll();
?>
<h2>Usage Logs</h2>
<table border="1" cellpadding="6">
<tr>
  <th>User</th><th>File</th><th>Type</th>
  <th>Opened</th><th>Closed</th>
  <th>Pages Read</th><th>Watched (sec)</th>
</tr>
<?php foreach ($rows as $r): ?>
<tr>
  <td><?= htmlspecialchars($r['email']) ?></td>
  <td><?= htmlspecialchars($r['title']) ?></td>
  <td><?= htmlspecialchars($r['type']) ?></td>
  <td><?= $r['opened_at'] ?></td>
  <td><?= $r['closed_at'] ?></td>
  <td><?= $r['pages_read'] ?></td>
  <td><?= $r['watched_seconds'] ?></td>
</tr>
<?php endforeach; ?>
</table>
