@page
@model RR3CommunityServer.Pages.UsersModel
@{
    ViewData["Title"] = "User Management";
}

<div class="container-fluid mt-4">
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h1>👥 User Management</h1>
                    <p class="text-muted">Manage all registered users</p>
                </div>
                <a href="/admin" class="btn btn-outline-secondary">← Back to Dashboard</a>
            </div>
        </div>
    </div>

    @if (Model.Users.Count == 0)
    {
        <div class="alert alert-info">
            <i class="bi bi-info-circle"></i> No users registered yet. Users will appear here when players connect to your server.
        </div>
    }
    else
    {
        <div class="card">
            <div class="card-header bg-primary text-white">
                <div class="d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">All Users (@Model.Users.Count)</h5>
                    <form method="get" class="d-flex gap-2">
                        <input type="text" name="search" value="@Model.SearchQuery" class="form-control form-control-sm" placeholder="Search by Synergy ID...">
                        <button type="submit" class="btn btn-sm btn-light">Search</button>
                        @if (!string.IsNullOrEmpty(Model.SearchQuery))
                        {
                            <a href="/admin/users" class="btn btn-sm btn-outline-light">Clear</a>
                        }
                    </form>
                </div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Synergy ID</th>
                                <th>Device ID</th>
                                <th>Created</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var user in Model.Users)
                            {
                                <tr>
                                    <td><strong>@user.Id</strong></td>
                                    <td><code>@user.SynergyId</code></td>
                                    <td><code>@user.DeviceId</code></td>
                                    <td>@user.CreatedAt.ToString("g")</td>
                                    <td>
                                        <button class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#userModal@(user.Id)">
                                            <i class="bi bi-eye"></i> View
                                        </button>
                                        <form method="post" asp-page-handler="Delete" class="d-inline">
                                            <input type="hidden" name="userId" value="@user.Id" />
                                            <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Delete this user?')">
                                                <i class="bi bi-trash"></i> Delete
                                            </button>
                                        </form>
                                    </td>
                                </tr>

                                <!-- User Details Modal -->
                                <div class="modal fade" id="userModal@(user.Id)" tabindex="-1">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            <div class="modal-header bg-primary text-white">
                                                <h5 class="modal-title">User Details</h5>
                                                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                                            </div>
                                            <div class="modal-body">
                                                <dl class="row">
                                                    <dt class="col-sm-3">User ID:</dt>
                                                    <dd class="col-sm-9">@user.Id</dd>
                                                    
                                                    <dt class="col-sm-3">Synergy ID:</dt>
                                                    <dd class="col-sm-9"><code>@user.SynergyId</code></dd>
                                                    
                                                    <dt class="col-sm-3">Device ID:</dt>
                                                    <dd class="col-sm-9"><code>@user.DeviceId</code></dd>
                                                    
                                                    <dt class="col-sm-3">Created:</dt>
                                                    <dd class="col-sm-9">@user.CreatedAt.ToString("F")</dd>
                                                </dl>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    }
</div>
