<?php

namespace routes;

use classes\authentication;
use classes\response;
use classes\router;
use classes\xlvask;
use objects\orders_o;
use objects\users_o;
use objects\xlvask_customers_o;
use traits\route_t;

class moduleXLVaskRoute
{
    use route_t;

    public function run(): void
    {
        global /** @var response $response */
        /** @var router $router */
        $router, $response;
        $this->get('/modules/xlvask/usageLog', function () {
            global $response;
            self::requirePermission('modules_xlvask_usageLog');
            $params = [
                'dateFrom' => null,
                'regNr' => null,
                'vehicleId' => null,
                'customerId' => null,
            ];
            // Get the parameters from the request
            foreach ( $params as $key => $value ) {
                if ($this->isParametersSet([$key])) {
                    $params[$key] = $this->getParameter($key);
                }
            }
            // Set the memory limit to 5120MB
            ini_set('memory_limit', '5120M');
            // Create the xlvask object
            $xlvask = new xlvask();
            $result = $xlvask->getUsageLog(...$params);
            // Response
            $response->success($result, 200);
        },
            [
                'modules_xlvask_usageLog' => 'Get the usage log of the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/vehicles', function () {
            global $response;
            self::requirePermission('modules_xlvask_vehicles');
            $params = [
                'customerId' => null,
                'vehicleId' => null,
                'registrationNumber' => null,
                'changeDate' => null,
            ];
            // Get the parameters from the request
            foreach ( $params as $key => $value ) {
                if ($this->isParametersSet([$key])) {
                    $params[$key] = $this->getParameter($key);
                }
            }
            // Create the xlvask object
            $xlvask = new xlvask();
            $result = $xlvask->getVehicles(...$params);
            // Response
            $response->success($result, 200);
        },
            [
                'modules_xlvask_vehicles' => 'Get the vehicles of the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/customers', function () {
            global $response;
            self::requirePermission('modules_xlvask_customers');
            $params = [
                'customerId' => null,
                'customerName' => null,
                'customerExternalId' => null,
                'vatNumber' => null,
                'changeDate' => null,
                'users' => null,
            ];
            // Get the parameters from the request
            foreach ( $params as $key => $value ) {
                if ($this->isParametersSet([$key])) {
                    $params[$key] = $this->getParameter($key);
                }
            }
            // Create the xlvask object
            $xlvask = new xlvask();
            $result = $xlvask->getCustomers(...$params);
            // Response
            $response->success($result, 200);
        },
            [
                'modules_xlvask_customers' => 'Get the customers of the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/internal/vehicle-types', function () {
            global $response;
            self::requirePermission('modules_xlvask_internal_vehicle_types');
            $user = (new authentication())->get_user();
            $xlvask_vehicle_types = new \objects\xlvask_vehicle_types_o();
            $result = $xlvask_vehicle_types->listObjectsWithPaginationIfSet(
                function ($tmp_object_array) {
                    return [
                        ...$tmp_object_array
                    ];
                }
            );
            // Response
            $response->success($result, 200);
        },
            [
                'modules_xlvask_internal_vehicle_types' => 'Get the vehicle types of the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/related-orders', function () {
            global $response;
            self::requirePermission('modules_xlvask_related_orders');
            self::requireParameters(['washIds']);
            $washIds = self::getParameter('washIds');
            // Check if the washIds is an array
            self::requireType($washIds, self::TYPE_ARRAY());
            // Require only strings in the array
            foreach ( $washIds as $key => $value ) {
                if (!is_string($value)) {
                    self::requireType($value, self::TYPE_STRING());
                }
            }
            // Get all the related orders
            $orders_o = new orders_o();
            $mathing_orders = $orders_o->getFieldsWhere([
                'wash_id' => $washIds,
                'deleted_at' => null,
            ], [
                'id',
                'wash_id',
            ]);

            $formatted_matches = [];
            foreach ( $mathing_orders as $order ) {
                $formatted_matches[(string)$order['wash_id']][] = (int)$order['id'];
            }
            $response->success($formatted_matches, 200);
        },
            [
                'modules_xlvask_related_orders' => 'Get the related orders of the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/tasks/sync-users', function () {
            global $response;
            self::requirePermission('modules_xlvask_sync_users');
            // Create the xlvask tasks object
            $xlvask = new xlvask();
            // Run the sync users task
            $result = $xlvask->getTasks()->runSyncUsers();
            // Response
            $response->success(
                $result,
                200
            );
        },
            [
                'modules_xlvask_sync_users' => 'Synchronize users with the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/tasks/sync-usage', function () {
            global $response;
            //self::requirePermission('modules_xlvask_sync_usage');
            // Remove the memory limit
            // ini_set('memory_limit', '-1');
            // Remove the execution time limit
            // set_time_limit(300);
            // Create the xlvask tasks object
            $xlvask = new xlvask();
            // Run the sync usage task
            $result = $xlvask->getTasks()->runSyncUsage();
            // Response
            $response->success(
                $result,
                200
            );
        },
            [
                'modules_xlvask_sync_usage' => 'Synchronize usage with the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/tasks/debug', function () {
            global $response;
            self::requirePermission('modules_xlvask_sync_usage');
            // Create the xlvask tasks object
            $xlvask = new xlvask();
            $user = new users_o();
            $user->getUserByCustomerNumber(12345679);
            //$result = $xlvask->getTasks()->runSyncVehicles(false);
            $vehicles = $xlvask->new($xlvask->helpers->xlvask_vehicles);
            //print_r($vehicles::getVehicleByRegistrationNumber('BW93159'));
            // Response
            $response->success(
                'Debugging xlvask tasks',
                200
            );
        },
            [
                'modules_xlvask_sync_usage' => 'Synchronize usage with the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/tasks/import-customers', function () {
            global $response;
            self::requirePermission('modules_xlvask_import_customers');
            // Create the xlvask_customers_o object
            $xlvask_customers_o = new xlvask_customers_o();
            $xlvask_customers_o->importCustomers();
            $response->success(
                'Customers imported',
                200
            );
        },
            [
                'modules_xlvask_import_customers' => 'Import customers from the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/tasks/import-vehicles', function () {
            global $response;
            self::requirePermission('modules_xlvask_import_vehicles');
            // Create the xlvask_vehicles_o object
            $xlvask_vehicles_o = new \objects\xlvask_vehicles_o();
            $xlvask_vehicles_o->importVehicles();
            $response->success(
                'Vehicles imported',
                200
            );
        },
            [
                'modules_xlvask_import_vehicles' => 'Import vehicles from the xlvask module'
            ]
        );

        $this->get('/modules/xlvask/tasks/import-usage', function () {
            global $response;
            self::requirePermission('modules_xlvask_import_usage');
            // Create the xlvask_usage_logs_o object
            $xlvask_usage_logs_o = new \objects\xlvask_usage_logs_o();
            // Import usage logs
            $xlvask_usage_logs_o->importUsageLogs();
            // Response
            $response->success(
                'Usage logs imported',
                200
            );
        },
            [
                'modules_xlvask_import_usage' => 'Import usage from the xlvask module'
            ]
        );
    }
}