<?php
/**
 * User Activity
 * View user activity logs
 * Kenyan Pension & Life Insurance Management System
 */

require_once 'layouts/main.php';
require_once 'includes/auth.php';

requireLogin();
requirePermission('view_user_activity');

$user_id = $_GET['user_id'] ?? '';
$date_from = $_GET['date_from'] ?? date('Y-m-01');
$date_to = $_GET['date_to'] ?? date('Y-m-t');

$where = ["DATE(al.created_at) BETWEEN ? AND ?"];
$params = [$date_from, $date_to];
$types = 'ss';

if ($user_id) {
    $where[] = "al.performed_by_user_id = ?";
    $params[] = $user_id;
    $types .= 'i';
}

$whereClause = implode(' AND ', $where);

$sql = "SELECT al.*, u.username, u.email
        FROM audit_logs al
        LEFT JOIN users u ON al.performed_by_user_id = u.user_id
        WHERE $whereClause
        ORDER BY al.created_at DESC
        LIMIT 200";

$stmt = $conn->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$activities = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

$users = $conn->query("SELECT user_id, username, email FROM users WHERE deleted_at IS NULL ORDER BY username")->fetch_all(MYSQLI_ASSOC);

$pageTitle = 'User Activity';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'User Activity')); ?>
    <link rel="stylesheet" href="assets/css/pension-dashboard.css">
    <?php include 'layouts/head-css.php'; ?>
</head>

<body data-sidebar="dark">
    <div id="layout-wrapper">
        <?php include 'layouts/sidebar.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="welcome-header" style="background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>User Activity</h2>
                                <p>View user activity logs and system interactions</p>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <form method="GET" class="d-inline-flex gap-2">
                                <select name="user_id" class="form-select" style="width: auto;">
                                    <option value="">All Users</option>
                                    <?php foreach ($users as $user): ?>
                                        <option value="<?php echo $user['user_id']; ?>" <?php echo $user_id == $user['user_id'] ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($user['username']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                                <input type="date" name="date_from" class="form-control" style="width: auto;" value="<?php echo $date_from; ?>">
                                <input type="date" name="date_to" class="form-control" style="width: auto;" value="<?php echo $date_to; ?>">
                                <button type="submit" class="btn btn-primary">Filter</button>
                            </form>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Activity Log</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Date & Time</th>
                                                    <th>User</th>
                                                    <th>Event Type</th>
                                                    <th>Category</th>
                                                    <th>Description</th>
                                                    <th>IP Address</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($activities)): ?>
                                                    <tr><td colspan="6" class="text-center">No activity found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($activities as $activity): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y, h:i A', strtotime($activity['created_at'])); ?></td>
                                                            <td><?php echo htmlspecialchars($activity['username'] ?? $activity['performed_by'] ?? 'System'); ?></td>
                                                            <td><span class="status-badge status-pending"><?php echo htmlspecialchars($activity['event_type']); ?></span></td>
                                                            <td><?php echo htmlspecialchars($activity['event_category']); ?></td>
                                                            <td><?php echo htmlspecialchars($activity['event_description']); ?></td>
                                                            <td><?php echo htmlspecialchars($activity['ip_address'] ?? 'N/A'); ?></td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
