<?php

namespace routes;

use classes\authentication;
use classes\response;
use classes\router;
use objects\logs_o;
use traits\route_t;

class permissionsRoute
{
    use route_t;

    public function run(): void
    {
        global /** @var response $response */
        /** @var router $router */
        $router, $response;


        /** Permissions > List */
        $this->get('/permissions', function () {
            global $response, $router;
            $this->requirePermission('permissions_list');
            $user = (new authentication())->get_user();
            if ($user) {
                (new logs_o())->add('permissions', 'global', 1, 0, 'PERMISSIONS', 'User accessed the permissions list');
                $response->success($router->getPermissions());
            } else {
                $response->error('Invalid session', 400);
            }
        },
            [
                'permissions_list' => 'List all permissions'
            ]
        );

        /** Permissions > User > List */
        self::get('/user/permissions', function () {
            global $response;
            self::requirePermission('permissions_list_own');
            $user = (new authentication())->get_user();
            if ($user) {
                (new logs_o())->add('permissions', 'global', 1, 0, 'PERMISSIONS', 'User accessed the user permissions list');
                $response->success((object)$user->getGroup()->getPermissions());
            } else {
                (new logs_o())->add('permissions', 'global', 0, 0, 'PERMISSIONS', 'User tried to access the user permissions list without a valid session');
                $response->error('Invalid session', 400);
            }
        },
            [
                'permissions_list_own' => 'List all user permissions'
            ]
        );
    }
}