{% extends "base.html" %}
{% import "partials/nav.html" as nav %}
{% import "partials/icons.html" as icons %}
{% block title %}Client {{ client.index }} — Quartermaster{% endblock %}
{% block nav %}{% call nav::nav("clients", user, csrf_token, fika_installed) %}{% endcall %}{% endblock %}
{% block content %}
<div class="flex-between">
    <h1>Client {{ client.index }}</h1>
    {% if user.role.can_control_server() %}
    <div class="flex gap-1">
        <form method="post" action="/clients/{{ client.index }}/start" style="display:inline">
            <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
            <button type="submit" class="btn btn-sm btn-success">{% call icons::play() %}{% endcall %} Start</button>
        </form>
        <form method="post" action="/clients/{{ client.index }}/restart" style="display:inline"
              onsubmit="return confirm('Restart client {{ client.index }}?')">
            <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
            <button type="submit" class="btn btn-sm btn-warning">{% call icons::refresh() %}{% endcall %} Restart</button>
        </form>
        <form method="post" action="/clients/{{ client.index }}/stop" style="display:inline"
              onsubmit="return confirm('Stop client {{ client.index }}?')">
            <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
            <button type="submit" class="btn btn-sm btn-danger">{% call icons::stop() %}{% endcall %} Stop</button>
        </form>
    </div>
    {% endif %}
</div>

<div class="card">
    <h2>Status</h2>
    <table>
        <tbody>
            <tr>
                <th>Container Name</th>
                <td><code>{{ client.container_name }}</code></td>
            </tr>
            <tr>
                <th>Container Status</th>
                <td>
                    {% match client.container_status %}
                    {% when crate::client::ContainerStatus::Running %}
                    <span class="badge badge-success">Running</span>
                    {% when crate::client::ContainerStatus::Stopped %}
                    <span class="badge badge-error">Stopped</span>
                    {% when crate::client::ContainerStatus::Unknown %}
                    <span class="badge">Unknown</span>
                    {% endmatch %}
                </td>
            </tr>
            <tr>
                <th>Health</th>
                <td>
                    {% match client.health %}
                    {% when crate::client::ClientHealth::Healthy %}
                    <span class="status-dot up glow"></span> Healthy
                    {% when crate::client::ClientHealth::Degraded %}
                    <span class="status-dot degraded"></span> Degraded
                    {% when crate::client::ClientHealth::Down %}
                    <span class="status-dot down"></span> Down
                    {% when crate::client::ClientHealth::GivenUp %}
                    <span class="status-dot down"></span> Given Up
                    {% endmatch %}
                </td>
            </tr>
            <tr>
                <th>Fika Status</th>
                <td>
                    {% if let Some(status) = client.fika_status %}
                    {% match status %}
                    {% when crate::spt::headless::EHeadlessStatus::Ready %}
                    <span class="badge badge-success">Ready</span>
                    {% when crate::spt::headless::EHeadlessStatus::InRaid %}
                    <span class="badge badge-warning">In Raid</span>
                    {% when crate::spt::headless::EHeadlessStatus::Unknown(_) %}
                    <span class="badge">Unknown</span>
                    {% endmatch %}
                    {% else %}
                    <span class="text-muted">Unknown</span>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th>Connected Players</th>
                <td>
                    {% if client.players.is_empty() %}
                    <span class="text-muted">None</span>
                    {% else %}
                    {{ client.players.join(", ") }}
                    {% endif %}
                </td>
            </tr>
        </tbody>
    </table>
</div>

<div class="card">
    <h2>Resources</h2>
    <table>
        <tbody>
            <tr>
                <th>CPU Usage</th>
                <td>
                    {% if let Some(cpu) = client.cpu_percent %}
                    {{ cpu }}%
                    {% else %}
                    <span class="text-muted">Unknown</span>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th>Memory Usage</th>
                <td>
                    {% if let Some(mem) = client.memory_mb %}
                    {{ mem }} MB
                    {% else %}
                    <span class="text-muted">Unknown</span>
                    {% endif %}
                </td>
            </tr>
        </tbody>
    </table>
</div>

<div class="card">
    <h2>Reliability</h2>
    <table>
        <tbody>
            <tr>
                <th>Total Restarts</th>
                <td>{{ client.restart_count }}</td>
            </tr>
            <tr>
                <th>Last Restart</th>
                <td>
                    {% if let Some(ts) = client.last_restart %}
                    {{ ts.format("%Y-%m-%d %H:%M:%S UTC") }}
                    {% else %}
                    <span class="text-muted">Never</span>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th>Consecutive Failures</th>
                <td>
                    {% if client.consecutive_failures > 0 %}
                    <span style="color: var(--error)">{{ client.consecutive_failures }}</span>
                    {% else %}
                    0
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th>Restarting</th>
                <td>
                    {% if client.restarting %}
                    <span class="badge badge-warning">Yes</span>
                    {% else %}
                    <span class="text-muted">No</span>
                    {% endif %}
                </td>
            </tr>
        </tbody>
    </table>
</div>

<p class="text-muted text-sm mt-2"><a href="/clients">← Back to clients list</a></p>
{% endblock %}
