<?php
// Task 21: Site disable/enable audit trail view.
// Root-only. Shows up to 200 audit rows for a given site_id, newest first.

require_once('/docker/whp/web/libs/mysqlmgmt.php');
require_once('/docker/whp/web/libs/permission_checker.php');

if (!is_root_user()) {
    http_response_code(403);
    echo '<div class="alert alert-danger"><i class="fa fa-lock me-2"></i>Access denied: root only.</div>';
    return;
}

$site_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($site_id <= 0) {
    echo '<div class="alert alert-warning"><i class="fa fa-exclamation-triangle me-2"></i>No site ID specified. Use <code>?page=site-audit&amp;id=N</code>.</div>';
    return;
}

$MySQLMgmt = new mysqlmgmt();
$db = $MySQLMgmt->getMySQLConnection();
if (!$db) {
    echo '<div class="alert alert-danger">Database connection failed.</div>';
    return;
}

// Fetch site info for display
$stmt = $db->prepare("SELECT site_name, username FROM whp.sites WHERE id = ? LIMIT 1");
$stmt->execute([$site_id]);
$site = $stmt->fetch(PDO::FETCH_ASSOC);

// Fetch audit rows (newest first, cap at 200)
$stmt2 = $db->prepare(
    "SELECT id, action, reason, actor, note, created_at
       FROM whp.site_disable_audit
      WHERE site_id = :id
      ORDER BY created_at DESC
      LIMIT 200"
);
$stmt2->execute(['id' => $site_id]);
$rows = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="row">
    <div class="col-12">
        <div class="d-flex align-items-center justify-content-between mb-3">
            <div>
                <?php if ($site): ?>
                <h4 class="mb-1">
                    Disable/Enable Audit &mdash;
                    <?php echo htmlspecialchars($site['site_name']); ?>
                    <small class="text-muted">(<?php echo htmlspecialchars($site['username']); ?>)</small>
                </h4>
                <?php else: ?>
                <h4 class="mb-1">Disable/Enable Audit &mdash; Site #<?php echo $site_id; ?></h4>
                <?php endif; ?>
                <small class="text-muted">Showing up to 200 most recent entries, newest first.</small>
            </div>
            <a href="index.php?page=sites" class="btn btn-sm btn-outline-secondary">
                <i class="fa fa-arrow-left me-1"></i>Back to Sites
            </a>
        </div>

        <?php if (empty($rows)): ?>
        <div class="alert alert-info">
            <i class="fa fa-info-circle me-2"></i>No audit entries found for this site.
        </div>
        <?php else: ?>
        <div class="card">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-sm table-hover mb-0">
                        <thead>
                            <tr>
                                <th>When</th>
                                <th>Action</th>
                                <th>Reason</th>
                                <th>Actor</th>
                                <th>Note</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($rows as $row): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($row['created_at']); ?></td>
                                <td>
                                    <?php if ($row['action'] === 'disable'): ?>
                                        <span class="badge bg-danger">disable</span>
                                    <?php elseif ($row['action'] === 'enable'): ?>
                                        <span class="badge bg-success">enable</span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary"><?php echo htmlspecialchars($row['action']); ?></span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo htmlspecialchars($row['reason'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row['actor']); ?></td>
                                <td><?php echo htmlspecialchars($row['note'] ?? ''); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>
