<?php

use Models\PostModel;

// We can instantiate the PostModel here
$postModel = new PostModel();
$archives = $postModel->getMonthlyArchives();

// Basic styles or structure:
echo '<div class="widget-archives">';
echo '<h3 class="widget-title">Archives</h3>';

if (!empty($archives)) {
    echo '<ul>';
    foreach ($archives as $row) {
        $yearMonth = $row['year_month']; // e.g. "2024-11"
        $year = $row['year'];
        // Convert numeric month to a textual representation, e.g., November
        // Or you can keep it numeric if you prefer
        $monthNum = (int)$row['month']; // "11" => 11
        $monthLabel = date('F', mktime(0, 0, 0, $monthNum, 1, $year));
        $total = $row['total'];

        // Build a link, e.g. /HOME_DIRECTORY/index.php?url=post/archive/2024-11
        // or /post/archive/2024-11 if you have a custom .htaccess
        $archiveUrl = HOME_DIRECTORY . 'index.php?url=post/archive/' . htmlspecialchars($yearMonth);

        echo '<li>';
        echo '<a href="' . $archiveUrl . '">';
        // "November 2024 (2)"
        echo $monthLabel . ' ' . $year . ' </a>';
        echo '(' . $total . ')';
        echo '</li>';
    }
    echo '</ul>';
} else {
    echo '<p>No archives yet.</p>';
}

echo '</div>';