<?php

namespace routes;

use classes\authentication;
use objects\logs_o;
use objects\module_action_logs_o;
use traits\route_t;

class moduleActionLogsRoute
{
    use route_t;

    public function run(): void
    {
        /** Modules > Action Logs > List */
        $this->get('/modules/action-logs', function () {
            global $response;
            self::requirePermission('modules_action_logs_view');

            // Get the user object
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 401);
            }

            // Log the incident
            (new logs_o())->add('modules_action_logs', 'global', 1, $user->id, 'LIST_MODULES_ACTION_LOGS', 'User listed module action logs');

            $module_action_logs_o = new module_action_logs_o();
            
            $response->success(
                $module_action_logs_o->listObjectsWithPaginationIfSet(
                    function ($log) {
                        return (new module_action_logs_o())->select((int)$log['id'])->asArray();
                    }
                )
            );
        },
            [
                'modules_action_logs_view' => 'View module action logs',
            ]
        );
    }
}
