<?php

namespace routes;

use classes\authentication;
use objects\categories_o;
use objects\department_lanes_o;
use objects\logs_o;
use traits\route_t;

class departmentLanesRoute
{
    use route_t;

    public function run(): void
    {
        $this->get('/department/lanes', function () {

            // Require the user to be logged in
            global $response;
            $this->requirePermission('list_department_lanes');
            // Get the user object
            $user = (new authentication())->get_user();
            // Check if the request was successful
            if ($user) {
                // Check if the id parameter is set
                if (self::isParametersSet(['id'])) {
                    // If it is, return the department lane with that id
                    $department_lane = (new department_lanes_o())->select((int)$response->getRequestParameter('id'));
                    // Check if the department lane exists
                    if (!$department_lane->exists()) {
                        // Return an error
                        $response->error('Department lane not found', 404);
                    }
                    // Log the incident
                    (new logs_o())->add('department_lanes', 'global', 1, $user->id, 'VIEW_DEPARTMENT_LANE', 'User viewed department lane with id ' . $department_lane->id);
                    // Return the department lane
                    $response->success(
                        $department_lane->asArray()
                    );
                }
                // Log the incident
                (new logs_o())->add('department_lanes', 'global', 1, $user->id, 'LIST_DEPARTMENT_LANES', 'User listed department lanes');
                // Return the list of departments
                $response->success(
                    (new department_lanes_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',
                            'name',
                            'department',
                            'relay_in_id',
                            'relay_out_id',
                            'relay_machine_id',
                        ])
                        ->listObjectsWithPaginationIfSet(
                            function ($department_lane) use ($user) {
                                // Create the department lane object
                                $department_lane_o = (new department_lanes_o())->select((int)$department_lane['id']);
                                // Return the object as an array
                                return $department_lane_o->asArray();
                            }
                        )
                );
            } else {
                // Log the incident
                (new logs_o())->add('department_lanes', 'global', 1, 0, 'LIST_DEPARTMENT_LANES', 'User tried to list department lanes without being logged in');
                // Return an error
                $response->error('Invalid session', 400);
            }
        },
            [
                'list_department_lanes' => 'List all department lanes'
            ]
        );

        $this->post('/department/lanes', function () {

            // Require the user to be logged in
            global $response;
            $this->requirePermission('add_department_lane');
            // Get the user object
            $user = (new authentication())->get_user();
            // Check if the request was successful
            if ($user) {
                // Log the incident
                (new logs_o())->add('department_lanes', 'global', 1, $user->id, 'ADD_DEPARTMENT_LANE', 'User added a department lane');
                // Get the request data
                $name = $response->getRequestParameter('name') ?? null;
                $department = $response->getRequestParameter('department') ?? null;
                $relay_in_id = $response->getRequestParameter('relay_in_id') ?? null;
                $relay_out_id = $response->getRequestParameter('relay_out_id') ?? null;
                $relay_machine_id = $response->getRequestParameter('relay_machine_id') ?? null;
                // Remove spaces from the relay_in_id and relay_out_id
                // Check if the required fields are set
                if ($name && $department) {
                    // Add the department lane
                    (new department_lanes_o())->add((int)$department, (string)$name, $relay_in_id, $relay_out_id, $relay_machine_id);
                    // Return a success message
                    $response->success('Department lane added');
                } else {
                    // Return an error
                    $response->error('Missing required fields', 400);
                }

            } else {
                // Log the incident
                (new logs_o())->add('department_lanes', 'global', 1, 0, 'ADD_DEPARTMENT_LANE', 'User tried to add a department lane without being logged in');
                // Return an error
                $response->error('Invalid session', 400);
            }
        },
            [
                'add_department_lane' => 'Add a department lane'
            ]
        );

        $this->put('/department/lanes', function () {

            // Require the user to be logged in
            global $response;
            $this->requirePermission('edit_department_lane');
            // Get the user object
            $user = (new authentication())->get_user();
            // Check if the request was successful
            if ($user) {
                // Log the incident
                (new logs_o())->add('department_lanes', 'global', 1, $user->id, 'EDIT_DEPARTMENT_LANE', 'User edited a department lane');
                // Get the request data
                $id = $response->getRequestParameter('id') ?? null;
                $name = $response->getRequestParameter('name') ?? null;
                $department = $response->getRequestParameter('department') ?? null;
                $relay_in_id = $response->getRequestParameter('relay_in_id') ?? null;
                $relay_out_id = $response->getRequestParameter('relay_out_id') ?? null;
                $relay_machine_id = $response->getRequestParameter('relay_machine_id') ?? null;

                // Check what fields are set
                self::requireParameters(['id']);
                // Get the department lane object
                $department_lane = (new department_lanes_o())->select((int)$id);
                // Check if the department lane exists
                if (!$department_lane->exists()) {
                    // Return an error
                    $response->error('Department lane not found', 404);
                }
                // Update the department lane fields that are set
                if (self::isParametersSet(['name'])) {
                    $department_lane->name->set($name);
                }
                if (self::isParametersSet(['department'])) {
                    $department_lane->department->set((int)$department);
                }
                if (self::isParametersSet(['relay_in_id'])) {
                    $department_lane->relay_in_id->set((string)$relay_in_id);
                }
                if (self::isParametersSet(['relay_out_id'])) {
                    $department_lane->relay_out_id->set((string)$relay_out_id);
                }
                if (self::isParametersSet(['relay_machine_id'])) {
                    $department_lane->relay_machine_id->set((string)$relay_machine_id);
                }
                // Return a success message
                $response->success('Department lane updated');
            } else {
                // Log the incident
                (new logs_o())->add('department_lanes', 'global', 1, 0, 'EDIT_DEPARTMENT_LANE', 'User tried to edit a department lane without being logged in');
                // Return an error
                $response->error('Invalid session', 400);
            }
        },
            [
                'edit_department_lane' => 'Edit a department lane'
            ]
        );
    }
}