<?php

namespace routes;

use classes\authentication;
use classes\economic;
use classes\response;
use classes\router;
use objects\logs_o;
use traits\route_t;

class moduleEconomicCustomerRoute
{
    use route_t;

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


        /** Modules > Economic > Customer > Get customer */
        $this->get('/modules/economic/customer', function () {
            global $response;
            self::requirePermission('modules_economic_customer_get');
            $user = (new authentication())->get_user();
            if ($user) {
                self::requireParameters(['customer_number']);
                self::requireType('customer_number', self::type_int());
                self::requireMinLength('customer_number', 1);
                self::requireMaxLength('customer_number', 10);
                (new logs_o())->add('modules_economic', 'global', 1, 0, 'MODULES_ECONOMIC', 'User accessed the customer');
                $result = (new economic())->getCustomer((int)$_GET['customer_number']);
                $response->success((object)$result);
            } else {
                (new logs_o())->add('modules_economic', 'global', 0, 0, 'MODULES_ECONOMIC', 'User tried to access the customer without a valid session');
                $response->error('Invalid session', 400);
            }
        },
            [
                'modules_economic_customer_get' => 'Get customer'
            ]
        );

        /** Modules > Economic > Customer > Create customer */
        $this->post('/modules/economic/customer', function () {
            global $response;
            self::requirePermission('modules_economic_customer_create');
            $user = (new authentication())->get_user();
            if ($user || true) {
                self::requireParameters(['customer_number', 'cvr', 'email', 'phone', 'name']);
                self::requireMinLength('customer_number', 1);
                self::requireMaxLength('customer_number', 10);
                self::requireMinLength('cvr', 1);
                self::requireMaxLength('cvr', 10);
                self::requireMinLength('email', 1);
                self::requireMaxLength('email', 255);
                self::requireMinLength('phone', 1);
                self::requireMaxLength('phone', 255);
                self::requireMinLength('name', 1);
                self::requireMaxLength('name', 255);
                (new logs_o())->add('modules_economic', 'global', 1, 0, 'MODULES_ECONOMIC', 'User accessed the customer');
                $result = (new economic())->createCustomer(
                    (int)self::getParameter('customer_number'),
                    (string)self::getParameter('name'),
                    (int)self::getParameter('cvr'),
                    (string)self::getParameter('email'),
                    (int)self::getParameter('phone'),
                );
                $response->success((object)$result);
            } else {
                (new logs_o())->add('modules_economic', 'global', 0, 0, 'MODULES_ECONOMIC', 'User tried to access the customer without a valid session');
                $response->error('Invalid session', 400);
            }
        });

    }
}