<?php

namespace routes;

use classes\limble;
use classes\response;
use classes\router;
use classes\slack;
use limble\helpers\limble_tasks_pagination;
use limble\helpers\limble_webhook_payload_task;
use traits\route_t;

class moduleLimbleRoute
{
    use route_t;

    public function run(): void
    {
        global /** @var response $response */
        /** @var router $router */
        $router, $response;
        $this->post('/modules/limble/webhook/task', function () {
            global $response;
            $slack = new slack();
            //TODO:  Add authentication of some sort here
            //self::requirePermission('modules_limble_webhooks_task');
            // Check if the module is enabled
            $limble = new limble();
            $limble->requireModuleEnabled();
            $limble->requireWebhooksEnabled();
            $payload = json_decode(file_get_contents('php://input'), true);
            if (json_last_error() !== JSON_ERROR_NONE) {
                $response->error('Invalid JSON payload', 400);
            }
            $task = new limble_webhook_payload_task($payload);
            // Response
            $response->success('Debug', 200);
        },
            [
                'modules_limble_webhooks_task' => 'Send Limble Webhook Task (POST)',
            ]
        );

        $this->get('/modules/limble/tasks', function () {
            global $response;
            $slack = new slack();
            $slack->send_message('Limble Tasks Endpoint Triggered', 'Limble Tasks');
            //self::requirePermission('modules_limble_tasks');
            // Check if the module is enabled
            $limble = new limble();
            $limble->requireModuleEnabled();
            $pagination = new limble_tasks_pagination();
            //$pagination->tasks = 2;
            // Get the tasks
            $tasks = $limble->getTasks()->formatTasks($limble->listTasks($pagination));
            // Response
            $response->success($tasks, 200);
        },
            [
                'modules_limble_tasks' => 'Get Limble Tasks (GET)',
            ]
        );
    }
}