<?php

// Lightweight, env-agnostic test for dynamic images construction
// This test avoids requiring full app bootstrap and Imagick composition by
// leveraging exportAsBase64() fallback when no canvas is initialized.

// Minimal stubs to satisfy dependencies when running without full bootstrap
namespace classes { class db {} class object_property { public function __construct(...$a){} public function value(){return null;} public function set($v){} } }
namespace traits { trait db_object_t {} }

namespace {
    if (!defined('WD')) { define('WD', dirname(__DIR__, 2)); }

    require_once WD . '/modules/dynamicimages/traits/dynamicimages_asset_t.php';
    require_once WD . '/modules/dynamicimages/interfaces/dynamicimages_asset_i.php';
    require_once WD . '/modules/dynamicimages/classes/dynamicimages_asset.php';
    require_once WD . '/modules/dynamicimages/interfaces/dynamicimages_image_i.php';
    require_once WD . '/modules/dynamicimages/traits/dynamicimages_image_t.php';
    require_once WD . '/modules/dynamicimages/classes/dynamicimages_image.php';
    require_once WD . '/modules/dynamicimages/images/machine_1.php';
    require_once WD . '/objects/department_selfserve_tasks_o.php';

    use dynamicimages\images\machine_1;
    use objects\department_selfserve_tasks_o;

    function ok($m){ echo "\n\033[32m✔ $m\033[0m\n"; }
    function fail($m){ echo "\n\033[31m✖ $m\033[0m\n"; }

    echo "\nDepartmentLanesImageTest starting...\n";

    // 1) Buttons normalization examples
    try {
        $arr = department_selfserve_tasks_o::normalizeButtonsInput('0, 2,3 , 5');
        if ($arr === [0,2,3,5]) { ok('CSV buttons normalization works'); } else { fail('CSV normalization mismatch: '.json_encode($arr)); }
    } catch (\Exception $e) { fail('CSV normalization threw: '.$e->getMessage()); }

    try {
        $arr = department_selfserve_tasks_o::normalizeButtonsInput('[1, 1, "2", 4]');
        if ($arr === [1,2,4]) { ok('JSON buttons normalization with de-dup works'); } else { fail('JSON normalization mismatch: '.json_encode($arr)); }
    } catch (\Exception $e) { fail('JSON normalization threw: '.$e->getMessage()); }

    // 2) Vehicle type normalization examples
    try { $v = department_selfserve_tasks_o::normalizeVehicleTypeInput('3'); if ($v===3) ok('Vehicle type numeric string ok'); else fail('Vehicle type mismatch'); } catch (\Exception $e) { fail($e->getMessage()); }
    try { $v = department_selfserve_tasks_o::normalizeVehicleTypeInput(null); if ($v===null) ok('Vehicle type null ok'); else fail('Vehicle type null mismatch'); } catch (\Exception $e) { fail($e->getMessage()); }

    // 3) Sanity: instantiate machine_1 and export base64 from first asset (no canvas)
    try {
        $img = new machine_1();
        // Set some sample parameters (not used until setup(), which we skip to avoid Imagick requirement)
        $img->highlighted_buttons = [0,2,5];
        $img->current_step = 1;
        $dataUri = $img->exportAsBase64();
        if (is_string($dataUri) && str_starts_with($dataUri, 'data:image/')) {
            ok('machine_1 exportAsBase64 fallback returns a data URI');
        } else {
            fail('machine_1 exportAsBase64 returned unexpected value');
        }
    } catch (\Throwable $e) {
        fail('machine_1 instantiation/export threw: '.$e->getMessage());
    }

    echo "\nDepartmentLanesImageTest completed.\n";
}
