<?php

namespace routes;

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

class moduleWorkfeedRoute
{
    use route_t;

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

        $this->get('/modules/workfeed/employees', function () {
            global $response;
            self::requirePermission('modules_workfeed_employees_view');
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 400);
            }

            $result = (new workfeed())->listEmployees();
            (new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_EMPLOYEES_LIST', 'Listed Workfeed employees');
            $response->success($result, 200);
        }, [
            'modules_workfeed_employees_view' => 'List Workfeed employees',
        ]);

        $this->get('/modules/workfeed/employees/{id}', function () {
            global $response;
            self::requirePermission('modules_workfeed_employees_view');
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 400);
            }

            $result = (new workfeed())->getEmployee((string)$this->fromRoute('id'));
            (new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_EMPLOYEE_GET', 'Fetched Workfeed employee');
            $response->success($result, 200);
        }, [
            'modules_workfeed_employees_view' => 'Get a specific Workfeed employee',
        ]);

        $this->get('/modules/workfeed/shifts', function () {
            global $response;
            self::requirePermission('modules_workfeed_shifts_view');
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 400);
            }

            $filters = $this->filterRequestParameters($_GET, [
                'startFrom',
                'startTo',
                'from',
                'to',
                'employeeID',
                'employeeId',
                'released',
            ]);

            if (!isset($filters['startFrom']) && isset($filters['from'])) {
                $filters['startFrom'] = $filters['from'];
            }
            if (!isset($filters['startTo']) && isset($filters['to'])) {
                $filters['startTo'] = $filters['to'];
            }
            if (!isset($filters['employeeID']) && isset($filters['employeeId'])) {
                $filters['employeeID'] = $filters['employeeId'];
            }

            unset($filters['from'], $filters['to'], $filters['employeeId']);

            if (!isset($filters['startFrom']) || trim((string)$filters['startFrom']) === '') {
                $response->error('Missing required query parameter: startFrom', 400);
            }
            if (!isset($filters['startTo']) || trim((string)$filters['startTo']) === '') {
                $response->error('Missing required query parameter: startTo', 400);
            }

            $result = (new workfeed())->listShifts($filters);
            (new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_SHIFTS_LIST', 'Listed Workfeed shifts');
            $response->success($result, 200);
        }, [
            'modules_workfeed_shifts_view' => 'List Workfeed shifts',
        ]);

        $this->get('/modules/workfeed/shifts/{id}', function () {
            global $response;
            self::requirePermission('modules_workfeed_shifts_view');
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 400);
            }

            $result = (new workfeed())->getShift((string)$this->fromRoute('id'));
            (new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_SHIFT_GET', 'Fetched Workfeed shift');
            $response->success($result, 200);
        }, [
            'modules_workfeed_shifts_view' => 'Get a specific Workfeed shift',
        ]);

        $this->get('/modules/workfeed/departments', function () {
            global $response;
            self::requirePermission('modules_workfeed_departments_view');
            $user = (new authentication())->get_user();
            if (!$user) {
                $response->error('Invalid session', 400);
            }

            $result = (new workfeed())->listDepartments();
            (new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_DEPARTMENTS_LIST', 'Listed Workfeed departments');
            $response->success($result, 200);
        }, [
            'modules_workfeed_departments_view' => 'List Workfeed departments',
        ]);
    }

    private function filterRequestParameters(array $parameters, array $allowedKeys): array
    {
        $allowed = array_flip($allowedKeys);
        $filtered = [];

        foreach ($parameters as $key => $value) {
            if (isset($allowed[$key]) && $value !== '' && $value !== null) {
                $filtered[$key] = $value;
            }
        }

        return $filtered;
    }
}
