<?php

namespace classes {
    // Minimal stubs to satisfy class loading without full app bootstrap
    class db {}
    class object_property {
        public function __construct(...$args) {}
        public function value() { return null; }
        public function set($v) {}
    }
}

namespace traits {
    trait db_object_t {}
}

namespace {
    // Lightweight bootstrap for CLI execution without full index.php
    if (!defined('WD')) {
        define('WD', __DIR__ . '/../../');
    }

    require_once WD . 'objects/department_selfserve_tasks_o.php';

    use objects\department_selfserve_tasks_o;

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

    echo "\nButtonsNormalizationTest starting...\n";

    // 1) Array input with ints and numeric strings
    try {
        $result = department_selfserve_tasks_o::normalizeButtonsInput([1, '2', 3]);
        if ($result === [1, 2, 3]) {
            ok('Array input normalized correctly');
        } else {
            fail('Array input normalization mismatch: ' . json_encode($result));
        }
    } catch (\Exception $e) {
        fail('Array input threw unexpectedly: ' . $e->getMessage());
    }

    // 2) JSON string input with duplicates and zero
    try {
        $result = department_selfserve_tasks_o::normalizeButtonsInput('[4,5,5,0]');
        if ($result === [4,5,0]) {
            ok('JSON input normalized with duplicates removed');
        } else {
            fail('JSON input normalization mismatch: ' . json_encode($result));
        }
    } catch (\Exception $e) {
        fail('JSON input threw unexpectedly: ' . $e->getMessage());
    }

    // 3) CSV string input
    try {
        $result = department_selfserve_tasks_o::normalizeButtonsInput('6, 7 ,8');
        if ($result === [6,7,8]) {
            ok('CSV input normalized correctly');
        } else {
            fail('CSV input normalization mismatch: ' . json_encode($result));
        }
    } catch (\Exception $e) {
        fail('CSV input threw unexpectedly: ' . $e->getMessage());
    }

    // 4) Invalid input should throw
    $thrown = false;
    try {
        department_selfserve_tasks_o::normalizeButtonsInput('["a", 2]');
    } catch (\Exception $e) {
        $thrown = true;
        ok('Invalid input correctly threw exception: ' . $e->getMessage());
    }
    if (!$thrown) {
        fail('Expected exception for invalid input, but none was thrown');
    }

    echo "\nButtonsNormalizationTest completed.\n";
}
