{% extends "base.html" %}

{% block css %}
    #packet_details {
        height: 95vh;
        overflow: auto;
    }

    .main-container, .container {
        max-width: 900px;
        margin: 0 auto;
        text-align: center;
    }

    .card-section {
        background-color: #272b2f;
        border: 1px solid #474b4e;
        padding: 15px 20px;
        margin-bottom: 20px;
        border-radius: 10px;
        transition: background-color 0.2s ease;
    }

    .card-section:hover {
        background-color: #2f3338;
    }

    .section-header {
        font-size: 16px;
        margin: 0 0 6px 0;
        font-weight: 500;
        color: #fff;
    }

    .main-header {
        font-size: 22px;
        margin-bottom: 20px;
        font-weight: 600;
    }

    .chart {
        height: 400px;
        margin-top: 10px;
    }

    .total-count {
        color: #ccc;
        font-size: 14px;
        margin-bottom: 8px;
    }
{% endblock %}

{% block head %}
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.5.0/dist/echarts.min.js"></script>
{% endblock %}

{% block body %}
<div class="main-container">
    <h2 class="main-header">Mesh Statistics - Hourly Packet Counts (Last 24 Hours)</h2>

    <!-- Daily packets chart moved to top -->
    <div class="card-section">
        <p class="section-header">Packets per Day - All Ports (Last 14 Days)</p>
        <div id="total_daily_all" class="total-count">Total: 0</div>
        <div id="chart_daily_all" class="chart"></div>
    </div>

    <!-- Overall hourly packets -->
    <div class="card-section">
        <p class="section-header">Packets per Hour - All Ports</p>
        <div id="total_hourly_all" class="total-count">Total: 0</div>
        <div id="chart_hourly_all" class="chart"></div>
    </div>

    <!-- Hourly charts by portnum -->
    <div class="card-section">
        <p class="section-header">Packets per Hour - Text Messages (Port 1)</p>
        <div id="total_portnum_1" class="total-count">Total: 0</div>
        <div id="chart_portnum_1" class="chart"></div>
    </div>

    <div class="card-section">
        <p class="section-header">Packets per Hour - Position (Port 3)</p>
        <div id="total_portnum_3" class="total-count">Total: 0</div>
        <div id="chart_portnum_3" class="chart"></div>
    </div>

    <div class="card-section">
        <p class="section-header">Packets per Hour - Node Info (Port 4)</p>
        <div id="total_portnum_4" class="total-count">Total: 0</div>
        <div id="chart_portnum_4" class="chart"></div>
    </div>

    <div class="card-section">
        <p class="section-header">Packets per Hour - Telemetry (Port 67)</p>
        <div id="total_portnum_67" class="total-count">Total: 0</div>
        <div id="chart_portnum_67" class="chart"></div>
    </div>

    <div class="card-section">
        <p class="section-header">Packets per Hour - Traceroute (Port 70)</p>
        <div id="total_portnum_70" class="total-count">Total: 0</div>
        <div id="chart_portnum_70" class="chart"></div>
    </div>

    <div class="card-section">
        <p class="section-header">Packets per Hour - Neighbor Info (Port 71)</p>
        <div id="total_portnum_71" class="total-count">Total: 0</div>
        <div id="chart_portnum_71" class="chart"></div>
    </div>
</div>

<script>
    async function fetchStats(period_type, length, portnum = null) {
        try {
            let url = `/api/stats?period_type=${period_type}&length=${length}`;
            if (portnum !== null) {
                url += `&portnum=${portnum}`;
            }
            const res = await fetch(url);
            if (!res.ok) {
                console.error(`Failed to fetch ${period_type} stats portnum ${portnum}:`, res.status, res.statusText);
                return [];
            }
            const json = await res.json();
            return json.data || [];
        } catch (err) {
            console.error('Error fetching stats:', err);
            return [];
        }
    }

    function updateTotalCount(domId, data) {
        const el = document.getElementById(domId);
        if (!el || !data.length) return;
        const total = data.reduce((acc, d) => acc + (d.count ?? d.packet_count ?? 0), 0);
        el.textContent = `Total: ${total.toLocaleString()}`;
    }

    let chartHourlyAll = null;
    let chartPortnum1 = null;
    let chartPortnum3 = null;
    let chartPortnum4 = null;
    let chartPortnum67 = null;
    let chartPortnum70 = null;
    let chartPortnum71 = null;
    let chartDailyAll = null;

    function renderChart(domId, data, type, color, isHourly) {
        const el = document.getElementById(domId);
        if (!el) return;
        const chart = echarts.init(el);
        switch(domId) {
            case 'chart_hourly_all': chartHourlyAll = chart; break;
            case 'chart_portnum_1': chartPortnum1 = chart; break;
            case 'chart_portnum_3': chartPortnum3 = chart; break;
            case 'chart_portnum_4': chartPortnum4 = chart; break;
            case 'chart_portnum_67': chartPortnum67 = chart; break;
            case 'chart_portnum_70': chartPortnum70 = chart; break;
            case 'chart_portnum_71': chartPortnum71 = chart; break;
            case 'chart_daily_all': chartDailyAll = chart; break;
        }

        const periods = data.map(d => {
            const p = (d && (d.period || d.period === 0)) ? d.period.toString() : '';
            if (isHourly) {
                if (p.includes(' ')) return p.split(' ')[1];
                return p.slice(-5) || p;
            }
            return p;
        });

        const counts = data.map(d => (d.count ?? d.packet_count ?? 0));

        const option = {
            backgroundColor: '#272b2f',
            tooltip: { trigger: 'axis' },
            grid: { left: '6%', right: '6%', bottom: '18%' },
            xAxis: {
                type: 'category',
                data: periods,
                axisLine: { lineStyle: { color: '#aaa' } },
                axisLabel: { rotate: 45, color: '#ccc' }
            },
            yAxis: {
                type: 'value',
                axisLine: { lineStyle: { color: '#aaa' } },
                axisLabel: { color: '#ccc' }
            },
            series: [{
                data: counts,
                type: type,
                smooth: type === 'line',
                itemStyle: { color: color },
                areaStyle: type === 'line' ? {} : undefined
            }]
        };

        chart.setOption(option);
    }

    async function init() {
        const dailyAllData = await fetchStats('day', 14);
        updateTotalCount('total_daily_all', dailyAllData);
        renderChart('chart_daily_all', dailyAllData, 'line', '#66bb6a', false);

        const hourlyAllData = await fetchStats('hour', 24);
        updateTotalCount('total_hourly_all', hourlyAllData);
        renderChart('chart_hourly_all', hourlyAllData, 'bar', '#03dac6', true);

        const portnums = [1, 3, 4, 67, 70, 71];
        const colors = ['#ff5722', '#2196f3', '#9c27b0', '#ffeb3b', '#795548', '#4caf50'];
        const domIds = [
            'chart_portnum_1',
            'chart_portnum_3',
            'chart_portnum_4',
            'chart_portnum_67',
            'chart_portnum_70',
            'chart_portnum_71'
        ];
        const totalIds = [
            'total_portnum_1',
            'total_portnum_3',
            'total_portnum_4',
            'total_portnum_67',
            'total_portnum_70',
            'total_portnum_71'
        ];

        const allData = await Promise.all(portnums.map(pn => fetchStats('hour', 24, pn)));

        for (let i = 0; i < portnums.length; i++) {
            updateTotalCount(totalIds[i], allData[i]);
            renderChart(domIds[i], allData[i], 'bar', colors[i], true);
        }
    }

    window.addEventListener('resize', () => {
        if (chartHourlyAll) chartHourlyAll.resize();
        if (chartPortnum1) chartPortnum1.resize();
        if (chartPortnum3) chartPortnum3.resize();
        if (chartPortnum4) chartPortnum4.resize();
        if (chartPortnum67) chartPortnum67.resize();
        if (chartPortnum70) chartPortnum70.resize();
        if (chartPortnum71) chartPortnum71.resize();
        if (chartDailyAll) chartDailyAll.resize();
    });

    init();
</script>
{% endblock %}
