<?php

namespace routes;

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

class moduleMotorAPIRoute
{
    use route_t;

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


        /** Modules > MotorAPI > Lookup > GET */
        $this->get('/modules/motorapi/lookup', function () {
            global $response;
            self::requirePermission('modules_motorapi_lookup');
            $user = (new authentication())->get_user();
            if ($user) {
                self::requireParameters(['license_plate']);
                self::requireType('license_plate', 'string');
                self::requireMinLength('license_plate', 1);
                self::requireMaxLength('license_plate', 10);
                (new logs_o())->add('modules_motorapi', 'global', 1, $user->id, 'MODULES_MOTORAPI', 'Successfully looked up license plate information');
                $result = (new motorapi())->getLicensePlateInformation($this->fromRequest('license_plate'), false);
                $response->success((object)$result);
            } else {
                (new logs_o())->add('modules_motorapi', 'global', 1, 0, 'MODULES_MOTORAPI', 'No user found, or invalid session');
                $response->error('Invalid session', 400);
            }
        },
            [
                'modules_motorapi_lookup' => 'Lookup license plate information'
            ]
        );

    }
}