<?php

namespace routes;

use classes\authentication;
use classes\recaptcha;
use classes\virkdata;
use objects\department_lanes_o;
use objects\departments_o;
use objects\logs_o;
use objects\tokens_o;
use objects\users_o;
use traits\route_t;

class guestRoute
{
    use route_t;

    public function run(): void
    {
        $this->post('/guest/validation/customer-number', function () {
            // Get the post data
            global $response;
            self::requireParameters(['customer_number']);
            // Validate the customer number
            $customer_number = (int)$this->getParameter('customer_number');
            self::requireType($customer_number, $this->type_int());
            self::requireMinValue($customer_number, 1);
            self::requireMaxValue($customer_number, 999999999);
            $customer_number = (int)$this->getParameter('customer_number');
            // Check if the customer number exists
            $doesCustomerExist = (new users_o())->getUserByCustomerNumber($customer_number)->exists();

            // Return the result
            $response->success(['exists' => $doesCustomerExist, 'customer_number' => $customer_number]);
        });

        $this->get('/cvr/search', function () {
            global $response;
            //self::requirePermission('cvr_search'); // TODO: Remove this
            self::requireParameters(['query']);
            $query = self::getParameter('query');
            if (strlen($query) < 2) {
                $response->error('Query must be at least 2 characters long', 400);
            }
            $virkdata = new virkdata();
            $result = $virkdata->searchCompanies($query);
            $response->success($result, 200);
        }, [
            'cvr_search' => 'Search CVR numbers'
        ]);

        $this->get('/guest/departments', function () {
            global $response;
            $departments = (new departments_o());
            // Check if the department lane status is requested to be included
            $include_lane_status = false;
            if (self::isParametersSet(['include_lanes']) && self::getParameter('include_lanes')) {
               $include_lane_status = true;
            }
            $response->success($departments->listObjectsWithPaginationIfSet(function ($department_array) use ($departments, $include_lane_status) {
                $additional_data = [];
                // If including lane status, fetch it
                if ($include_lane_status) {
                    $department = $departments->select((int)$department_array['id']);
                    $additional_data['lanes'] = array_map(
                     /**
                     * @param department_lanes_o $lane
                     * @return array
                     */
                    function (department_lanes_o $lane) {
                        return [
                            'id' => (int)$lane->id,
                            'name' => (string)$lane->name->value(),
                            'status' => (string)$lane->getLaneStatus()->name,
                            'products' => $lane->getSelfServeLaneProducts(),
                        ];
                    }, $department->getLanes());
                }
                // Only return id and name
                return [
                    'id' => $department_array['id'],
                    'name' => $department_array['name'],
                    'longitude' => $department_array['longitude'],
                    'latitude' => $department_array['latitude'],
                    'address' => $department_array['description'],
                    ...$additional_data
                ];
            },
                $departments->forceRestrictFilters([
                'visible' => 1, // Only show visible departments, this is to prevent showing internal system departments to the end-user.
            ])), 200);
        });
    }
}