<?php

namespace routes;

use attachments\helpers\attachment;
use attachments\helpers\attachment_content;
use classes\attachment_store;
use classes\attachments;
use classes\response;
use objects\orders_o;
use traits\route_t;

class attachmentsRoute
{
    use route_t;

    public function run(): void
    {
        $this->get('/attachments/example', function () {
            global $response;
            throw new \Exception('EXAMPLE ROUTE, SHOULD BE IMPLEMENTED IN THE INDIVIDUAL OBJECT ROUTES');
            $orders_o = new orders_o();
            $orders_o->select(22636);
            // Debug: Create an attachment
            $orders_o->addAttachment((new attachment_content())->setOther('Hello World!'));
            $response->success($orders_o->listAttachments());
        });
        $this->post('/attachments/upload', function () {
            global $response;
            self::requireParameters(['base64_image']);
            $base64_image = self::getParameter('base64_image');
            //self::requirePermission('modules_scanner_lpr');
            if (empty($base64_image)) {
                $response->error('Base64 image is required.');
            }
            $uploads = new attachment_store();
            $object_name = $uploads->storeTempImageFromBase64(
                $base64_image
            );
            echo $object_name;
            $response->success(['object_name' => $object_name]);
        });
    }
}