<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

user_module_boot();

$pageTitle = 'Users';
$search = trim((string) ($_GET['q'] ?? ''));

$sql = 'SELECT u.id, u.full_name, u.email, u.phone, u.status, u.last_login, r.name AS role_name, c.name AS club_name
        FROM users u
        INNER JOIN roles r ON r.id = u.role_id
        LEFT JOIN clubs c ON c.id = u.club_id';
$params = [];

if ($search !== '') {
    $sql .= ' WHERE u.full_name LIKE :search OR u.email LIKE :search OR r.name LIKE :search';
    $params['search'] = '%' . $search . '%';
}

$sql .= ' ORDER BY u.created_at DESC, u.id DESC';
$users = db_fetch_all($sql, $params);

require dirname(__DIR__, 2) . '/includes/header.php';
require dirname(__DIR__, 2) . '/includes/sidebar.php';
require dirname(__DIR__, 2) . '/includes/topbar.php';
require dirname(__DIR__, 2) . '/includes/flash.php';
?>
<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">User management</h2>
        </div>
        <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/users/create.php')); ?>"><i class="bi bi-person-plus"></i> Add user</a>
    </div>

    <form class="row g-3 mb-4" method="get">
        <div class="col-md-6 col-xl-4">
            <input class="form-control" type="search" name="q" value="<?php echo e($search); ?>" placeholder="Search name, email, or role">
        </div>
        <div class="col-auto">
            <button class="btn btn-outline-jlfc" type="submit">Search</button>
        </div>
        <?php if ($search !== ''): ?>
            <div class="col-auto"><a class="btn btn-light" href="<?php echo e(base_url('app/modules/users/index.php')); ?>">Clear</a></div>
        <?php endif; ?>
    </form>

    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Role</th>
                    <th>Club</th>
                    <th>Status</th>
                    <th>Last login</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($users === []): ?>
                    <tr>
                        <td colspan="6" class="text-center text-muted py-4">No users found.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($users as $managedUser): ?>
                    <tr>
                        <td>
                            <strong><?php echo e($managedUser['full_name']); ?></strong>
                            <div class="small text-muted"><?php echo e($managedUser['email']); ?></div>
                        </td>
                        <td><?php echo e($managedUser['role_name']); ?></td>
                        <td><?php echo e($managedUser['club_name'] ?? 'Unassigned'); ?></td>
                        <td><span class="badge <?php echo e(user_badge_class($managedUser['status'])); ?>"><?php echo e(ucfirst($managedUser['status'])); ?></span></td>
                        <td><?php echo e($managedUser['last_login'] ?? 'Never'); ?></td>
                        <td class="text-end">
                            <div class="btn-group btn-group-sm">
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/users/view.php?id=' . (int) $managedUser['id'])); ?>">View</a>
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/users/edit.php?id=' . (int) $managedUser['id'])); ?>">Edit</a>
                                <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/users/delete.php?id=' . (int) $managedUser['id'])); ?>">Delete</a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>