<?php

namespace routes;

use classes\authentication;
use classes\email;
use classes\response;
use classes\router;
use classes\selfserve;
use classes\stripe;
use modules\selfserve\helpers\selfserve_lane_command;
use objects\departments_o;
use objects\logs_o;
use objects\orders_o;
use objects\stripe_module_customers_o;
use traits\route_t;

class moduleSelfServeRoute
{
    use route_t;

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


        /** Modules > Self Serve > Lane > Status */
        $this->get('/modules/self-serve/lane/status', function () {
            global $response;
            self::requirePermission('modules_selfserve_lane_status_view');
            $selfserve = new selfserve();
            $lane_id = self::isParametersSet(['lane_id']) ? (int)self::getParameter('lane_id') : 1;
            $lane = $selfserve->lane($lane_id);
            $response->success([
                'id' => $lane->id,
                'status' => $lane->getLaneStatus()->name,
                'mode' => $lane->getLaneMode()->name,
                'state' => $lane->getLaneState()->name,
                'wash_start_time' => $lane->getWashStartTime(),
                'elapsed_wash_time' => $lane->getElapsedWashTime(),
                'license_plate' => $lane->getLicensePlate(),
                'customer_number' => $lane->getCustomerNumber(),
            ]);
        },
            [
                'modules_selfserve_lane_status_view' => 'View self-serve lane status',
            ]
        );

        /** Modules > Self Serve > Lane > Command */
        $this->post('/modules/self-serve/lane/command', function () {
            global $response;
            self::requirePermission('modules_selfserve_lane_command_execute');
            $selfserve = new selfserve();
            // Get the request user
            $user = (new authentication())->get_user();
            $param_lane_id = 'lane_id';
            $param_command = 'command';
            // Validate parameters
            self::requireParameters([$param_lane_id, $param_command]);
            $lane_id = (int)$this->getParameter($param_lane_id);
            self::requireType($lane_id, self::type_int());
            self::requireMinValue($lane_id, 1);
            $commandParam = (string)$this->getParameter($param_command);
            self::requireType($commandParam, self::type_string());
            // Get the lane and command
            $lane = $selfserve->lane($lane_id);
            // If the user has the bypass permission, set the lane to bypass customer number validation
            if (self::hasPermission('modules_selfserve_lane_command_bypass_customer_number_validation')) {
                $lane->setBypassCustomerNumberValidation(true);
            }
            $command = selfserve_lane_command::tryFrom($commandParam);
            if ($command === null) {
                $response->error("Invalid command: " . $commandParam);
            }
            // Require permissions for specific commands
            switch ($command) {
                case selfserve_lane_command::START:
                    self::requirePermission('modules_selfserve_lane_command_execute_start');
                    break;
                case selfserve_lane_command::STOP:
                    self::requirePermission('modules_selfserve_lane_command_execute_stop');
                    break;
                case selfserve_lane_command::RESERVE:
                    self::requirePermission('modules_selfserve_lane_command_execute_reserve');
                    break;
                case selfserve_lane_command::RELEASE:
                    self::requirePermission('modules_selfserve_lane_command_execute_release');
                    break;
                case selfserve_lane_command::RESET:
                    self::requirePermission('modules_selfserve_lane_command_execute_reset');
                    break;
            }
            // Execute the command
            try {
                $args = new \modules\selfserve\classes\selfserve_lane_command_arguments();
                $args->setParameters([
                    ...$this->getParametersAsArray(), // Pass all parameters
                    'customer_number' => (int)$user->customer_number->value(), // Get customer number from request user
                    ]);
                $lane->execute($command, $args);
                $response->success([
                    'id' => $lane->id,
                    'status' => $lane->getLaneStatus()->name,
                    'mode' => $lane->getLaneMode()->name,
                    'state' => $lane->getLaneState()->name,
                    'wash_start_time' => $lane->getWashStartTime(),
                    'elapsed_wash_time' => $lane->getElapsedWashTime(),
                    'license_plate' => $lane->getLicensePlate(),
                    'customer_number' => $lane->getCustomerNumber(),
                ]);
            } catch (\Exception $e) {
                $response->error("Failed to execute command: " . $e->getMessage());
            }
        },
            [
                'modules_selfserve_lane_command_execute' => 'Execute self-serve lane command. This is required together with specific command permissions below.',
                'modules_selfserve_lane_command_execute_start' => 'Execute self-serve lane START command',
                'modules_selfserve_lane_command_execute_stop' => 'Execute self-serve lane STOP command',
                'modules_selfserve_lane_command_execute_reserve' => 'Execute self-serve lane RESERVE command',
                'modules_selfserve_lane_command_execute_release' => 'Execute self-serve lane RELEASE command (Release the lane reservation and reset its state)',
                'modules_selfserve_lane_command_execute_reset' => 'Execute self-serve lane RESET command',
                'modules_selfserve_lane_command_bypass_customer_number_validation' => 'Bypass customer number validation when executing commands',
            ]
        );
    }
}