<?php
$totals = $yearly['totals'];
$bestMonth = null;
foreach ($yearly['months'] as $row) {
    $hasActivity = (float) $row['fresh_income'] !== 0.0
        || (float) $row['rollover_in'] !== 0.0
        || (float) $row['expenses'] !== 0.0
        || (float) $row['planned'] !== 0.0
        || (float) $row['dead_movement'] !== 0.0;
    if (!$hasActivity) {
        continue;
    }
    if ($bestMonth === null || $row['remaining'] > $bestMonth['remaining']) {
        $bestMonth = $row;
    }
}
?>
<section class="hero-grid comparison-metrics">
    <article class="metric-card card"><div class="metric-icon blue"><?= icon('wallet') ?></div><p><?= (int) $yearly['year'] ?> fresh income</p><strong><?= h(Support::money($totals['fresh_income'], $symbol)) ?></strong><small>Rollover excluded to avoid double-counting</small></article>
    <article class="metric-card card"><div class="metric-icon pink"><?= icon('expense') ?></div><p><?= (int) $yearly['year'] ?> expenses</p><strong><?= h(Support::money($totals['expenses'], $symbol)) ?></strong><small><?= $totals['fresh_income'] > 0 ? number_format($totals['expenses'] / $totals['fresh_income'] * 100, 1) : '0.0' ?>% of fresh income</small></article>
    <article class="metric-card card"><div class="metric-icon orange"><?= icon('vault') ?></div><p>Moved dormant</p><strong><?= h(Support::money($totals['dead_deposits'], $symbol)) ?></strong><small><?= h(Support::money($totals['dead_restorations'], $symbol)) ?> restored</small></article>
    <article class="metric-card card"><div class="metric-icon green"><?= icon('chart') ?></div><p>Strongest remainder</p><strong><?= h(Support::money($bestMonth['remaining'] ?? 0, $symbol)) ?></strong><small><?= $bestMonth ? h(date('F', strtotime($bestMonth['month'] . '-01'))) : 'No data' ?></small></article>
</section>

<section class="content-grid comparison-grid">
    <article class="card span-2">
        <div class="card-heading"><div><p class="eyebrow">Month-to-month</p><h2>Resources vs. expenses</h2></div></div>
        <canvas class="trend-chart large" data-trend-chart='<?= h(json_encode($comparison)) ?>' data-symbol="<?= h($symbol) ?>" height="280"></canvas>
    </article>
    <article class="card">
        <div class="card-heading"><div><p class="eyebrow"><?= h(date('F', strtotime($month . '-01'))) ?></p><h2>Category share</h2></div></div>
        <?php if ($categoryStats === []): ?>
            <div class="empty-state compact"><strong>No category data</strong><p>Record categorized expenses to build this view.</p></div>
        <?php else: ?>
            <?php $categoryTotal = array_sum(array_column($categoryStats, 'spent')); ?>
            <div class="category-bars">
                <?php foreach ($categoryStats as $category): $width = $categoryTotal > 0 ? (float) $category['spent'] / $categoryTotal * 100 : 0; ?>
                    <div>
                        <p><span><i style="--dot:<?= h($category['color']) ?>"></i><?= h($category['category_name']) ?></span><strong><?= h(Support::money($category['spent'], $symbol)) ?></strong></p>
                        <div class="progress-track"><span style="width:<?= h((string) $width) ?>%;--bar:<?= h($category['color']) ?>"></span></div>
                        <small><?php if ((float) $category['planned'] > 0): ?><?= number_format($category['spent'] / $category['planned'] * 100, 0) ?>% of <?= h(Support::money($category['planned'], $symbol)) ?> planned<?php elseif ((float) $category['monthly_limit'] > 0): ?><?= number_format($category['spent'] / $category['monthly_limit'] * 100, 0) ?>% of <?= h(Support::money($category['monthly_limit'], $symbol)) ?> category limit<?php else: ?>No target defined<?php endif; ?></small>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </article>
</section>

<article class="card">
    <div class="card-heading">
        <div><p class="eyebrow">Statistical table</p><h2><?= (int) $yearly['year'] ?> monthly breakdown</h2></div>
        <label class="search-box"><?= icon('search') ?><input type="search" placeholder="Find a month…" data-table-search="year-table"></label>
    </div>
    <div class="table-wrap">
        <table id="year-table">
            <thead><tr><th>Month</th><th>Fresh income</th><th>Rollover</th><th>Planned</th><th>Expenses</th><th>Saved</th><th>Remainder</th></tr></thead>
            <tbody>
            <?php foreach ($yearly['months'] as $row): ?>
                <tr>
                    <td><a class="text-link" href="<?= h(url('comparisons', ['month' => $row['month']])) ?>"><?= h(date('F Y', strtotime($row['month'] . '-01'))) ?></a></td>
                    <td><?= h(Support::money($row['fresh_income'], $symbol)) ?></td>
                    <td><?= h(Support::money($row['rollover_in'], $symbol)) ?></td>
                    <td><?= h(Support::money($row['planned'], $symbol)) ?></td>
                    <td><?= h(Support::money($row['expenses'], $symbol)) ?></td>
                    <td><?= h(Support::money($row['dead_deposits'], $symbol)) ?></td>
                    <td class="<?= $row['remaining'] >= 0 ? 'green' : 'red' ?>"><strong><?= h(Support::money($row['remaining'], $symbol)) ?></strong></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</article>
