<?php

namespace classes {
    if (!class_exists('classes\\db')) {
        class db {}
    }

    if (!class_exists('classes\\object_property')) {
        class object_property
        {
            public function __construct(...$args) {}
            public function value() { return null; }
            public function set($v) {}
        }
    }
}

namespace traits {
    if (!trait_exists('traits\\db_object_t')) {
        trait db_object_t {}
    }
}

namespace {
    app_require('objects/department_selfserve_tasks_o.php');
}

namespace {
    use objects\department_selfserve_tasks_o;

    it('allows studio lane dynamic image previews to override the saved image id', function (): void {
        $content = file_get_contents(app_path('routes/departmentLanesRoute.php'));

        expect($content)->not->toBeFalse();
        expect($content)->toContain("isRequestParameterSet('dynamic_image_id')");
        expect($content)->toContain("\$dynamic_image_override = \$response->getRequestParameter('dynamic_image_id')");
        expect($content)->toContain("self::requireMinValue(\$dynamic_image_id, 1)");
        expect($content)->toContain("switch (\$dynamic_image_id)");
    });

    it('uses the selfserve dynamic image size config for route output and cache keys', function (): void {
        $route = file_get_contents(app_path('routes/departmentLanesRoute.php'));
        $config = file_get_contents(app_path('modules/selfserve/config/selfserve_dynamic_image_size_c.php'));
        $moduleConfig = file_get_contents(app_path('modules/selfserve/selfserve_c.php'));
        $imageTrait = file_get_contents(app_path('modules/dynamicimages/traits/dynamicimages_image_t.php'));

        expect($route)->not->toBeFalse();
        expect($config)->not->toBeFalse();
        expect($moduleConfig)->not->toBeFalse();
        expect($imageTrait)->not->toBeFalse();
        expect($config)->toContain("self::SIZE_ORIGINAL");
        expect($config)->toContain("self::SIZE_RELEVANT");
        expect($config)->toContain("[self::SIZE_ORIGINAL, self::SIZE_RELEVANT]");
        expect($config)->toContain("'dynamic_image_size'");
        expect($moduleConfig)->toContain("selfserve_dynamic_image_size_c::class");
        expect($moduleConfig)->toContain('public selfserve_dynamic_image_size_c $dynamic_image_size;');
        expect($route)->toContain('$dynamic_image_size = self::getSelfServeDynamicImageSizeMode();');
        expect($route)->toContain("'dynamic_image_size' => \$dynamic_image_size");
        expect($route)->toContain('self::buildDynamicImageCacheKey');
        expect($route)->toContain("'thumb_position' => \$thumb_position");
        expect($route)->toContain("'buttons' => \$buttons");
        expect($route)->toContain("'current_step' => \$current_step");
        expect($route)->toContain("'only_current_step' => \$only_current_step");
        expect($route)->toContain("'vehicle_type' => \$vehicle_type");
        expect($route)->toContain("resizeToMaxWidth(self::RELEVANT_DYNAMIC_IMAGE_MAX_WIDTH)");
        expect($imageTrait)->toContain('function resizeToMaxWidth(int $maxWidth)');
        expect($imageTrait)->toContain('function exportBinary(?string $format = null, int $quality = 90): string');
        expect($route)->toContain("\$imageData = \$image->exportBinary('png');");
    });

    it('accepts ordered dynamic image button tokens including program picker reset start and zero', function (): void {
        expect(department_selfserve_tasks_o::normalizeButtonsInput('["program_picker","reset",0,2,"start",5]'))->toBe([
            'program_picker',
            'reset',
            0,
            2,
            'start',
            5,
        ]);

        $route = file_get_contents(app_path('routes/departmentLanesRoute.php'));
        expect($route)->not->toBeFalse();
        expect($route)->toContain('ordered highlighted button IDs');
        expect($route)->toContain('"reset", "start", or "program_picker"');
    });

    it('renders machine one dynamic image steps from the ordered button payload', function (): void {
        $content = file_get_contents(app_path('modules/dynamicimages/images/machine_1.php'));

        expect($content)->not->toBeFalse();
        expect($content)->toContain('$deferredStartButtons = $this->drawHighlightedButtonSequence();');
        expect($content)->toContain('getOrderedHighlightedButtonTokens');
        expect($content)->toContain('normalizeHighlightedButtonToken');
        expect($content)->toContain("const BUTTON_PROGRAM_PICKER = 'program_picker'");
        expect($content)->toContain('getProgramPickerStepCoordinates');
        expect($content)->toContain('drawDeferredHighlightedButtons($deferredStartButtons)');
        expect($content)->toContain('self::BUTTON_PROGRAM_PICKER');

        $setupOffset = strpos($content, 'public function setup');
        expect($setupOffset)->not->toBeFalse();
        $setupBody = substr($content, (int)$setupOffset, 1000);
        expect($setupBody)->not->toContain('drawStepThumb();');
    });
}
