{% extends 'admin/base.html.twig' %}

{% block title %}Infrastructure{% endblock %}

{% block body %}
<style>.admin-main { max-width: 100% !important; padding: 1.5rem !important; }</style>
{% if snapshot_missing is defined and snapshot_missing %}
<div class="admin-card mb-4">
    <p class="text-sm font-bold text-yellow-600">Aucun snapshot disponible. Lancez <code class="bg-gray-100 px-1">php bin/console app:infra:snapshot</code></p>
</div>
{% endif %}

{% if generated_at is defined and generated_at %}
<div class="flex items-center justify-between mb-4">
    <p class="text-[10px] font-black uppercase tracking-widest text-gray-400">Derniere mise a jour : {{ generated_at|date('d/m/Y H:i:s') }}</p>
    <a href="{{ path('app_admin_infra') }}" class="text-[10px] font-black uppercase tracking-widest text-gray-400 hover:text-gray-600">Rafraichir</a>
</div>
{% endif %}

<div class="grid grid-cols-1 xl:grid-cols-4 gap-4 min-h-[calc(100vh-80px)]">

    {# ── COL 1: Serveur ────────────────────────────────── #}
    <div class="flex flex-col gap-4">
        <h2 class="text-lg font-black uppercase tracking-tighter italic heading-page">Serveur</h2>

        {# Systeme #}
        <div class="admin-card">
            <h3 class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-3">Systeme</h3>
            {{ _self.r('Hostname', server.hostname) }}
            {{ _self.r('OS', server.os) }}
            {{ _self.r('Uptime', server.uptime) }}
        </div>

        {# CPU #}
        <div class="admin-card">
            <h3 class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-3">CPU</h3>
            {{ _self.r('Modele', server.cpu_model) }}
            {{ _self.r('Coeurs', server.cpu_cores) }}
            {{ _self.r('Load 1/5/15m', server.load_1m ~ ' / ' ~ server.load_5m ~ ' / ' ~ server.load_15m) }}
            {{ _self.c('Charge', server.load_percent != '?' ? server.load_percent ~ '%' : '?', _self.pct_color(server.load_percent)) }}
        </div>

        {# RAM #}
        <div class="admin-card">
            <h3 class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-3">RAM</h3>
            {{ _self.r('Total', server.ram_total) }}
            {{ _self.r('Utilisee', server.ram_used) }}
            {{ _self.r('Disponible', server.ram_free) }}
            {{ _self.c('Utilisation', server.ram_percent != '?' ? server.ram_percent ~ '%' : '?', _self.pct_color(server.ram_percent)) }}
        </div>

        {# Disk #}
        <div class="admin-card">
            <h3 class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-3">Disque</h3>
            {{ _self.r('Total', server.disk_total) }}
            {{ _self.r('Utilise', server.disk_used) }}
            {{ _self.r('Libre', server.disk_free) }}
            {{ _self.c('Utilisation', server.disk_percent != '?' ? server.disk_percent ~ '%' : '?', _self.pct_color(server.disk_percent)) }}
        </div>

        {# Services #}
        <div class="admin-card">
            <h3 class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-3">Services</h3>
            <div class="flex flex-col gap-2">
                {{ _self.svc('Caddy', server.caddy.status, server.caddy.info) }}
                {{ _self.svc('Docker', server.docker.status, server.docker.info) }}
                {{ _self.svc_ssl(server.ssl) }}
            </div>
        </div>
    </div>

    {# ── COL 2: Containers ─────────────────────────────── #}
    <div class="flex flex-col gap-4">
        <h2 class="text-lg font-black uppercase tracking-tighter italic heading-page">Containers</h2>

        {% if containers|length > 0 %}
            {% for c in containers %}
            <div class="admin-card">
                <div class="flex items-center justify-between mb-2">
                    <h3 class="text-xs font-black uppercase tracking-widest">{{ c.name }}</h3>
                    {% if c.state == 'running' %}
                        <span class="admin-badge-green text-[9px] font-black uppercase">{{ c.state }}</span>
                    {% else %}
                        <span class="admin-badge-red text-[9px] font-black uppercase">{{ c.state }}</span>
                    {% endif %}
                </div>
                <p class="text-[10px] text-gray-400 mb-2">{{ c.image }} — {{ c.status }}</p>
                <div class="grid grid-cols-3 gap-2 text-center">
                    <div>
                        <p class="text-[9px] font-black uppercase text-gray-400">CPU</p>
                        <p class="text-sm font-black {{ _self.pct_class(c.cpu_percent|replace({'%': ''})) }}">{{ c.cpu_percent }}</p>
                    </div>
                    <div>
                        <p class="text-[9px] font-black uppercase text-gray-400">RAM</p>
                        <p class="text-sm font-black">{{ c.ram_used }}</p>
                    </div>
                    <div>
                        <p class="text-[9px] font-black uppercase text-gray-400">RAM %</p>
                        <p class="text-sm font-black {{ _self.pct_class(c.ram_percent|replace({'%': '', 'N/A': '0'})) }}">{{ c.ram_percent }}</p>
                    </div>
                </div>
            </div>
            {% endfor %}
        {% else %}
            <div class="admin-card">
                <p class="text-sm text-gray-400">Docker socket inaccessible.</p>
            </div>
        {% endif %}
    </div>

    {# ── COL 3: Redis ──────────────────────────────────── #}
    <div class="flex flex-col gap-4">
        <h2 class="text-lg font-black uppercase tracking-tighter italic heading-page">Redis</h2>

        {# Global #}
        <div class="admin-card">
            <div class="flex items-center justify-between mb-3">
                <h3 class="text-[10px] font-black uppercase tracking-widest text-gray-400">Global</h3>
                {% if redis_global.connected %}
                    <span class="admin-badge-green text-[9px] font-black uppercase">v{{ redis_global.version }}</span>
                {% else %}
                    <span class="admin-badge-red text-[9px] font-black uppercase">Deconnecte</span>
                {% endif %}
            </div>
            {% if redis_global.connected %}
                {{ _self.r('Uptime', redis_global.uptime_days ~ 'j') }}
                {{ _self.r('Role', redis_global.role) }}
                {{ _self.r('Clients', redis_global.connected_clients) }}
                {{ _self.r('Ops/sec', redis_global.ops_per_sec) }}
                <div class="border-t border-gray-200 my-1"></div>
                {{ _self.r('Memoire', redis_global.used_memory_human ~ ' / pic ' ~ redis_global.used_memory_peak_human) }}
                <div class="border-t border-gray-200 my-1"></div>
                {{ _self.r('Commandes', redis_global.total_commands_processed|number_format(0, '.', ' ')) }}
                {{ _self.r('Hits / Misses', redis_global.keyspace_hits|number_format(0, '.', ' ') ~ ' / ' ~ redis_global.keyspace_misses|number_format(0, '.', ' ')) }}
                {{ _self.c('Hit Rate', redis_global.hit_rate, redis_global.hit_rate != 'N/A' and redis_global.hit_rate|replace({'%': ''})|number_format > 80 ? 'green' : (redis_global.hit_rate == 'N/A' ? 'gray' : 'red')) }}
                {{ _self.c('Evictees', redis_global.evicted_keys, redis_global.evicted_keys == '0' ? 'green' : 'red') }}
            {% else %}
                <p class="text-sm text-red-500 font-bold">{{ redis_global.error }}</p>
            {% endif %}
        </div>

        {# Per-DB #}
        {% for db in redis_dbs %}
        <div class="admin-card">
            <div class="flex items-center justify-between mb-2">
                <h3 class="text-[10px] font-black uppercase tracking-widest text-gray-400">{{ db.label }}</h3>
                {% if db.connected %}
                    <span class="admin-badge-green text-[9px] font-black uppercase">{{ db.db }}</span>
                {% else %}
                    <span class="admin-badge-red text-[9px] font-black uppercase">Erreur</span>
                {% endif %}
            </div>
            {% if db.connected %}
                {{ _self.r('Cles', db.keys|number_format(0, '.', ' ')) }}
                {{ _self.r('TTL moyen', db.avg_ttl) }}
            {% else %}
                <p class="text-xs text-red-500 font-bold">{{ db.error }}</p>
            {% endif %}
        </div>
        {% endfor %}
    </div>

    {# ── COL 4: PostgreSQL & PgBouncer ─────────────────── #}
    <div class="flex flex-col gap-4">
        <h2 class="text-lg font-black uppercase tracking-tighter italic heading-page">PostgreSQL</h2>

        {# PostgreSQL #}
        <div class="admin-card">
            <div class="flex items-center justify-between mb-3">
                <h3 class="text-[10px] font-black uppercase tracking-widest text-gray-400">Instance</h3>
                {% if postgres.connected %}
                    <span class="admin-badge-green text-[9px] font-black uppercase">v{{ postgres.version }}</span>
                {% else %}
                    <span class="admin-badge-red text-[9px] font-black uppercase">Deconnecte</span>
                {% endif %}
            </div>
            {% if postgres.connected %}
                {{ _self.r('Uptime', postgres.uptime) }}
                {{ _self.r('Taille BDD', postgres.db_size) }}
                {{ _self.r('Tables', postgres.table_count) }}
                <div class="border-t border-gray-200 my-1"></div>
                {{ _self.r('Connexions actives', postgres.active_connections) }}
                {{ _self.r('Connexions total', postgres.total_connections ~ ' / ' ~ postgres.max_connections) }}
                <div class="border-t border-gray-200 my-1"></div>
                {{ _self.c('Cache Hit', postgres.cache_hit_ratio, postgres.cache_hit_ratio != 'N/A' and postgres.cache_hit_ratio|replace({'%': ''})|number_format > 95 ? 'green' : (postgres.cache_hit_ratio == 'N/A' ? 'gray' : 'yellow')) }}
                {{ _self.c('Dead Tuples', postgres.dead_tuples|number_format(0, '.', ' '), postgres.dead_tuples < 10000 ? 'green' : (postgres.dead_tuples < 100000 ? 'yellow' : 'red')) }}
            {% else %}
                <p class="text-sm text-red-500 font-bold">{{ postgres.error }}</p>
            {% endif %}
        </div>

        {# PgBouncer #}
        <div class="admin-card">
            <div class="flex items-center justify-between mb-3">
                <h3 class="text-[10px] font-black uppercase tracking-widest text-gray-400">PgBouncer</h3>
                {% if pgbouncer.connected %}
                    <span class="admin-badge-green text-[9px] font-black uppercase">Connecte</span>
                {% else %}
                    <span class="admin-badge-red text-[9px] font-black uppercase">Deconnecte</span>
                {% endif %}
            </div>
            {% if pgbouncer.connected %}
                {% if pgbouncer.pools|length > 0 %}
                    <p class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-2">Pools</p>
                    {% for pool in pgbouncer.pools %}
                    <div class="bg-gray-50 p-2 mb-2 border border-gray-200">
                        <div class="flex items-center justify-between mb-1">
                            <span class="text-xs font-black">{{ pool.database }}</span>
                            <span class="text-[9px] font-bold text-gray-400 uppercase">{{ pool.pool_mode }}</span>
                        </div>
                        <div class="grid grid-cols-2 gap-1 text-[11px]">
                            {{ _self.r('Cl. actifs', pool.cl_active) }}
                            <div class="flex justify-between"><span class="text-gray-500 font-bold">Cl. attente</span><span class="font-black {% if pool.cl_waiting > 0 %}text-yellow-600{% endif %}">{{ pool.cl_waiting }}</span></div>
                            {{ _self.r('Sv. actifs', pool.sv_active) }}
                            {{ _self.r('Sv. idle', pool.sv_idle) }}
                        </div>
                    </div>
                    {% endfor %}
                {% else %}
                    <p class="text-xs text-gray-400 mb-2">Aucun pool actif.</p>
                {% endif %}

                {% if pgbouncer.stats|length > 0 %}
                    <p class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-2 mt-3">Stats</p>
                    {% for stat in pgbouncer.stats %}
                    <div class="bg-gray-50 p-2 mb-2 border border-gray-200">
                        <p class="text-xs font-black mb-1">{{ stat.database }}</p>
                        <div class="text-[11px]">
                            {{ _self.r('Transactions', stat.total_xact_count|number_format(0, '.', ' ')) }}
                            {{ _self.r('Requetes', stat.total_query_count|number_format(0, '.', ' ')) }}
                            {{ _self.c('Avg xact', stat.avg_xact_time|number_format(0, '.', ' ') ~ 'us', stat.avg_xact_time > 100000 ? 'red' : (stat.avg_xact_time > 10000 ? 'yellow' : 'green')) }}
                            {{ _self.c('Avg query', stat.avg_query_time|number_format(0, '.', ' ') ~ 'us', stat.avg_query_time > 50000 ? 'red' : (stat.avg_query_time > 5000 ? 'yellow' : 'green')) }}
                        </div>
                    </div>
                    {% endfor %}
                {% endif %}
            {% else %}
                <p class="text-sm text-red-500 font-bold">{{ pgbouncer.error }}</p>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{# ── Macros ──────────────────────────────────────────── #}

{% macro r(label, value) %}
<div class="flex justify-between py-0.5 text-[12px]">
    <span class="text-gray-500 font-bold">{{ label }}</span>
    <span class="font-black">{{ value }}</span>
</div>
{% endmacro %}

{% macro c(label, value, color) %}
<div class="flex justify-between py-0.5 text-[12px]">
    <span class="text-gray-500 font-bold">{{ label }}</span>
    <span class="font-black {% if color == 'green' %}text-green-600{% elseif color == 'red' %}text-red-600{% elseif color == 'yellow' %}text-yellow-600{% else %}text-gray-400{% endif %}">{{ value }}</span>
</div>
{% endmacro %}

{% macro svc(name, status, info) %}
<div class="flex items-center justify-between py-0.5 text-[12px]">
    <span class="font-bold text-gray-500">{{ name }}</span>
    <div class="flex items-center gap-2">
        <span class="text-[11px] text-gray-400">{{ info }}</span>
        {% if status == 'ok' %}
            <span class="w-2 h-2 rounded-full bg-green-500 inline-block"></span>
        {% else %}
            <span class="w-2 h-2 rounded-full bg-red-500 inline-block"></span>
        {% endif %}
    </div>
</div>
{% endmacro %}

{% macro svc_ssl(ssl) %}
<div class="flex items-center justify-between py-0.5 text-[12px]">
    <span class="font-bold text-gray-500">SSL {{ ssl.domain }}</span>
    <div class="flex items-center gap-2">
        <span class="text-[11px] text-gray-400">{{ ssl.issuer }} — {{ ssl.valid_until }} ({{ ssl.days_left }}j)</span>
        {% if ssl.status == 'ok' %}
            <span class="w-2 h-2 rounded-full bg-green-500 inline-block"></span>
        {% elseif ssl.status == 'warning' %}
            <span class="w-2 h-2 rounded-full bg-yellow-500 inline-block"></span>
        {% else %}
            <span class="w-2 h-2 rounded-full bg-red-500 inline-block"></span>
        {% endif %}
    </div>
</div>
{% endmacro %}

{% macro pct_color(val) %}{% if val != '?' and val < 70 %}green{% elseif val == '?' %}gray{% elseif val < 90 %}yellow{% else %}red{% endif %}{% endmacro %}

{% macro pct_class(val) %}{% if val != '?' and val < 70 %}text-green-600{% elseif val == '?' %}text-gray-400{% elseif val < 90 %}text-yellow-600{% else %}text-red-600{% endif %}{% endmacro %}
