<?php
/**
 * Route for reusable self-serve machine types
 */

namespace routes;

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

class departmentSelfserveMachineTypesRoute
{
    use route_t;

    public function run(): void
    {
        $this->get('/department/selfserve/machine-types', function () {
            global $response;
            $this->requirePermission('list_department_selfserve_machine_types');
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 400);
            }

            $machine_types_o = new selfserve_machine_types_o();
            if (self::isParametersSet(['id'])) {
                $machine_types_o->select((int)self::getParameter('id'));
                if (!$machine_types_o->exists()) {
                    $response->error('Machine type not found', 404);
                }
                $response->success($machine_types_o->asArray());
            }

            (new logs_o())->add('selfserve_machine_types', 'global', 1, $user->id, 'LIST_MACHINE_TYPES', 'User listed self-serve machine types');
            $response->success(
                $machine_types_o->setSearchableFields(['id', 'name', 'description', 'created_at', 'updated_at', 'deleted_at'])
                    ->listObjectsWithPaginationIfSet(function (array $machine_type): array {
                        return (new selfserve_machine_types_o())->select((int)$machine_type['id'])->asArray();
                    })
            );
        }, [
            'list_department_selfserve_machine_types' => 'List reusable self-serve machine types'
        ]);

        $this->post('/department/selfserve/machine-types', function () {
            global $response;
            $this->requirePermission('add_department_selfserve_machine_types');
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 400);
            }

            $name = trim((string)$response->getRequestParameter('name'));
            $description = $response->isRequestParameterSet('description') ? (string)$response->getRequestParameter('description') : null;
            if ($name === '') {
                $response->error('Missing required fields: name', 400);
            }

            try {
                $machine_type = (new selfserve_machine_types_o())->add($name, $description);
                (new logs_o())->add('selfserve_machine_types', 'global', 1, $user->id, 'ADD_MACHINE_TYPE', 'User added self-serve machine type ' . $machine_type->id);
                $response->success($machine_type->asArray());
            } catch (\Exception $e) {
                $response->error($e->getMessage(), 500);
            }
        }, [
            'add_department_selfserve_machine_types' => 'Add reusable self-serve machine types'
        ]);

        $this->put('/department/selfserve/machine-types', function () {
            global $response;
            $this->requirePermission('update_department_selfserve_machine_types');
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 400);
            }

            self::requireParameters(['id']);
            $machine_type = (new selfserve_machine_types_o())->select((int)self::getParameter('id'));
            if (!$machine_type->exists()) {
                $response->error('Machine type not found', 404);
            }

            if (self::isParametersSet(['name'])) {
                $name = trim((string)self::getParameter('name'));
                if ($name === '') {
                    $response->error('name cannot be empty', 400);
                }
                $machine_type->name->set($name);
            }
            if (self::isParametersSet(['description'])) {
                $description = self::getParameter('description');
                $machine_type->description->set($description === null || $description === '' ? null : (string)$description);
            }

            (new logs_o())->add('selfserve_machine_types', 'global', 1, $user->id, 'UPDATE_MACHINE_TYPE', 'User updated self-serve machine type ' . $machine_type->id);
            $response->success($machine_type->asArray());
        }, [
            'update_department_selfserve_machine_types' => 'Update reusable self-serve machine types'
        ]);

        $this->delete('/department/selfserve/machine-types', function () {
            global $response;
            $this->requirePermission('delete_department_selfserve_machine_types');
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 400);
            }

            self::requireParameters(['id']);
            $machine_type = (new selfserve_machine_types_o())->select((int)self::getParameter('id'));
            if (!$machine_type->exists()) {
                $response->error('Machine type not found', 404);
            }

            $machine_type->delete();
            (new logs_o())->add('selfserve_machine_types', 'global', 1, $user->id, 'DELETE_MACHINE_TYPE', 'User deleted self-serve machine type ' . $machine_type->id);
            $response->success('Machine type deleted');
        }, [
            'delete_department_selfserve_machine_types' => 'Delete reusable self-serve machine types'
        ]);
    }
}
