{% extends "admin_base.html" %}
{% block extra_head %}
{{ super() }}
<link rel="stylesheet" href="{{ static_url('/static/css/workspace.css') }}">
{% endblock %}
{% block admin_content %}
<div class="admin-toolbar">
    <h2>Workspaces</h2>
    <span class="admin-count">{{ workspaces|length }} workspaces</span>
    <span class="admin-count">{{ flags|length }} open flags</span>
</div>

<div class="admin-table-wrap">
    <table class="admin-table" id="ws-admin-list" data-endpoint="/admin/workspaces/data">
        <caption class="sr-only">Dev workspaces</caption>
        <thead>
            <tr>
                <th>Name</th>
                <th>Owner</th>
                <th>Project</th>
                <th>Status</th>
                <th>Disk</th>
                <th>Egress</th>
                <th>Size</th>
                <th>Editor</th>
                <th>Tunnels</th>
                <th>Flags</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for ws in workspaces %}
            <tr data-workspace-uid="{{ ws.uid }}">
                <td>{{ ws.name }}</td>
                <td>{{ ws.owner_username }}</td>
                <td>{{ ws.project_title }}</td>
                <td>
                    {% if ws.suspended %}
                    <span class="workspace-badge workspace-badge-failed">suspended</span>
                    {% else %}
                    <span class="workspace-badge">{{ ws.status }}</span>
                    {% endif %}
                </td>
                <td>{{ ws.disk_percent }}% of {{ ws.disk_quota_mb }} MB</td>
                <td>{{ ws.egress_percent }}% of {{ ws.egress_quota_mb }} MB</td>
                <td>{{ ws.editor.cpu_cores }} CPU / {{ ws.editor.memory_mb }} MB</td>
                <td>{{ ws.editor.theme }} / {{ ws.editor.layout }}</td>
                <td>{{ ws.tunnels|length }}</td>
                <td>{{ ws.flags|length }}</td>
                <td class="admin-actions">
                    {% if ws.status == "running" %}
                    <form method="post" action="/admin/workspaces/{{ ws.uid }}/stop">
                        <button type="submit" class="admin-btn">Stop</button>
                    </form>
                    {% else %}
                    <form method="post" action="/admin/workspaces/{{ ws.uid }}/start">
                        <button type="submit" class="admin-btn">Start</button>
                    </form>
                    {% endif %}
                    {% if ws.suspended %}
                    <form method="post" action="/admin/workspaces/{{ ws.uid }}/unsuspend">
                        <button type="submit" class="admin-btn">Unsuspend</button>
                    </form>
                    {% else %}
                    <form method="post" action="/admin/workspaces/{{ ws.uid }}/suspend">
                        <input type="text" name="reason" placeholder="Reason" maxlength="500" required>
                        <button type="submit" class="admin-btn">Suspend</button>
                    </form>
                    {% endif %}
                    <form method="post" action="/admin/workspaces/{{ ws.uid }}/flag">
                        <input type="text" name="detail" placeholder="Flag detail" maxlength="500">
                        <button type="submit" class="admin-btn">Flag</button>
                    </form>
                    <form method="post" action="/admin/workspaces/{{ ws.uid }}/delete">
                        <button type="submit" data-confirm="Delete this workspace?" data-confirm-danger class="admin-btn admin-btn-danger">Delete</button>
                    </form>
                </td>
            </tr>
            {% else %}
            <tr><td colspan="11">No workspaces yet.</td></tr>
            {% endfor %}
        </tbody>
    </table>
</div>

<div class="admin-toolbar">
    <h2>Open flags</h2>
</div>
<div class="admin-table-wrap">
    <table class="admin-table">
        <caption class="sr-only">Open workspace flags</caption>
        <thead>
            <tr><th>Kind</th><th>Severity</th><th>Detail</th><th>Value</th><th>Threshold</th><th>Actions</th></tr>
        </thead>
        <tbody>
            {% for flag in flags %}
            <tr>
                <td>{{ flag.kind }}</td>
                <td>{{ flag.severity }}</td>
                <td>{{ flag.detail }}</td>
                <td>{{ flag.metric_value }}</td>
                <td>{{ flag.threshold }}</td>
                <td class="admin-actions">
                    <form method="post" action="/admin/workspaces/flags/{{ flag.uid }}/resolve?status=resolved">
                        <button type="submit" class="admin-btn">Resolve</button>
                    </form>
                    <form method="post" action="/admin/workspaces/flags/{{ flag.uid }}/resolve?status=dismissed">
                        <button type="submit" class="admin-btn">Dismiss</button>
                    </form>
                </td>
            </tr>
            {% else %}
            <tr><td colspan="6">No open flags.</td></tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endblock %}
