<?php

namespace routes;

use classes\authentication;
use objects\departments_o;
use objects\logs_o;
use objects\plate_scanners_o;
use traits\route_t;

class plateScannersRoute
{
    use route_t;

    public function run(): void
    {
        $this->get('/numberplatescanners', function () {
            // Require the user to be logged in
            global $response;
            $this->requirePermission('list_number_plate_scanners');
            // Get the user object
            $user = (new authentication())->get_user();
            // Check if the request was successful
            if ($user) {
                // Log the incident
                (new logs_o())->add('numberplatescanners', 'global', 1, $user->id, 'LIST_NUMBER_PLATE_SCANNERS', 'Successfully listed number plate scanners');
                // Return the list of plate scanners
                $response->success(
                    (new plate_scanners_o())
                        ->setSearchableFields([
                            // The fields that can be searched. This would otherwise make it possible to get secret information from the database, simply by searching for it and getting the result count back
                            'id',
                            'department_id',
                            'name',
                            'notes'
                        ])
                        ->listObjectsWithPaginationIfSet()
                );
            } else {
                // Log the incident
                (new logs_o())->add('numberplatescanners', 'global', 1, 0, 'LIST_NUMBER_PLATE_SCANNERS', 'No user found, or invalid session');
                // Return an error
                $response->error('Invalid session', 400);
            }
        },
            [
                'list_number_plate_scanners' => 'List all number plate scanners'
            ]
        );

        $this->post('/numberplatescanners', function () {
            // Require the user to be logged in
            global $response;
            $this->requirePermission('add_number_plate_scanner');
            // Get the user object
            $user = (new authentication())->get_user();
            // Check if the request was successful
            if ($user) {
                // Get the post data
                $data = json_decode(file_get_contents('php://input'), true);
                // Check if the required fields are set
                if (!isset($data['department_id'])) {
                    $response->error('Department ID is required', 400);
                }
                if (!isset($data['name'])) {
                    $response->error('Name is required', 400);
                }
                if (!isset($data['notes'])) {
                    $response->error('Notes is required', 400);
                }
                // Add the number plate scanner
                (new plate_scanners_o())->add($data['department_id'], $data['name'], $data['notes']);
                // Log the incident
                (new logs_o())->add('numberplatescanners', 'global', 1, $user->id, 'ADD_NUMBER_PLATE_SCANNER', 'Successfully added a number plate scanner');
                // Return a success message
                $response->success(['message' => 'Number plate scanner added']);
            } else {
                // Log the incident
                (new logs_o())->add('numberplatescanners', 'global', 1, 0, 'ADD_NUMBER_PLATE_SCANNER', 'No user found, or invalid session');
                // Return an error
                $response->error('Invalid session', 400);
            }
        },
            [
                'add_number_plate_scanner' => 'Add a number plate scanner'
            ]
        );

        $this->put('/numberplatescanners', function () {
            // Require the user to be logged in
            global $response;
            $this->requirePermission('edit_number_plate_scanner');
            // Get the user object
            $user = (new authentication())->get_user();
            // Check if the request was successful
            if ($user) {
                // Get the post data
                $data = json_decode(file_get_contents('php://input'), true);
                // Check if the required fields are set
                if (!isset($data['id'])) {
                    $response->error('ID is required', 400);
                }
                if (!isset($data['department_id'])) {
                    $response->error('Department ID is required', 400);
                }
                if (!isset($data['name'])) {
                    $response->error('Name is required', 400);
                }
                if (!isset($data['notes'])) {
                    $response->error('Notes is required', 400);
                }
                // Edit the number plate scanner
                (new plate_scanners_o())->edit($data['id'], $data['department_id'], $data['name'], $data['notes']);
                // Log the incident
                (new logs_o())->add('numberplatescanners', 'global', 1, $user->id, 'EDIT_NUMBER_PLATE_SCANNER', 'Successfully edited a number plate scanner');
                // Return a success message
                $response->success(['message' => 'Number plate scanner edited']);
            } else {
                // Log the incident
                (new logs_o())->add('numberplatescanners', 'global', 1, 0, 'EDIT_NUMBER_PLATE_SCANNER', 'No user found, or invalid session');
                // Return an error
                $response->error('Invalid session', 400);
            }
        },
            [
                'edit_number_plate_scanner' => 'Edit a number plate scanner'
            ]
        );

        self::get('/department/numberplatescanners', function () {
            // Require the user to be logged in
            global $response;
            self::requirePermission('list_department_number_plate_scanners');
            // Get the user object
            $user = (new authentication())->get_user();
            // Check if the request was successful
            if ($user) {
                // Get the department ID
                self::requireParameters(['id']);
                self::requireType((int)self::getParameter('id'), self::type_int());
                self::requireParameterIntPositive((int)self::getParameter('id'), 'id');
                // Check if the user has access to the department
                self::requireDepartmentAccess((int)self::getParameter('id'));
                // Check if the department exists
                $department = (new departments_o())->select((int)self::getParameter('id'));
                $department->requireSelected();
                // Log the incident
                (new logs_o())->add('numberplatescanners', 'global', 1, $user->id, 'LIST_DEPARTMENT_NUMBER_PLATE_SCANNERS', 'Successfully listed department number plate scanners');
                // Get the department scanners.
                $result = (new plate_scanners_o())
                    ->getFieldsWhere([
                        'department_id' => (int)self::getParameter('id')
                    ], [
                        'id',
                        'name',
                        'notes'
                    ]);
                // Parse the result
                foreach ( $result as $key => $value ) {
                    $result[$key]['id'] = (int)$value['id'];
                }
                // Return the list of plate scanners
                $response->success(
                    $result
                );
            } else {
                // Log the incident
                (new logs_o())->add('numberplatescanners', 'global', 1, 0, 'LIST_DEPARTMENT_NUMBER_PLATE_SCANNERS', 'No user found, or invalid session');
                // Return an error
                $response->error('Invalid session', 400);
            }
        },
            [
                'list_department_number_plate_scanners' => 'List all number plate scanners in a department',
                'department_access_:id' => 'Access department :id'
            ]
        );
    }
}