<?php
namespace Misuzu;

use RuntimeException;

if(!$msz->getAuthInfo()->getPerms('global')->check(Perm::G_CL_CHANGES_MANAGE))
    Template::throwError(403);

$changelog = $msz->getChangelog();
$usersCtx = $msz->getUsersContext();

$changelogPagination = new Pagination($changelog->countChanges(), 30);

if(!$changelogPagination->hasValidOffset())
    Template::throwError(404);

$changeInfos = $changelog->getChanges(pagination: $changelogPagination);
$changes = [];

foreach($changeInfos as $changeInfo) {
    $userInfo = $changeInfo->hasUserId() ? $usersCtx->getUserInfo($changeInfo->getUserId()) : null;

    $changes[] = [
        'change' => $changeInfo,
        'tags' => $changelog->getTags(changeInfo: $changeInfo),
        'user' => $userInfo,
        'user_colour' => $usersCtx->getUserColour($userInfo),
    ];
}

Template::render('manage.changelog.changes', [
    'changelog_changes' => $changes,
    'changelog_pagination' => $changelogPagination,
]);
