<?php

use App\Libraries\AIManager;

function helper($name) {}

function checkIntent(bool $condition, string $message): void
{
    if (!$condition) {
        throw new RuntimeException($message);
    }
}

$temp = sys_get_temp_dir() . '/image-intent-' . bin2hex(random_bytes(6)) . '/';
mkdir($temp, 0700);
file_put_contents($temp . 'ai_response_track.php', '<?php');
define('MSACOMMON_PATH', $temp);
define('APPPATH', dirname(__DIR__) . '/app/');

try {
    require APPPATH . 'Libraries/AIManager.php';
    $manager = new class extends AIManager {
        public function __construct() {}
    };
    $provider = new class {
        public array $calls = [];
        public string $intent = 'chat';
        public function classifyImageIntent($prompt, $hasImage, $previousPrompt, $isReply)
        {
            $this->calls[] = $prompt;
            return $this->intent;
        }
    };
    (new ReflectionProperty(AIManager::class, 'providers'))->setValue($manager, ['OpenAI' => $provider]);

    foreach (['bat', ' BAT ', "Bat\n", 'cat', 'cricket bat'] as $prompt) {
        checkIntent($manager->classifyImageIntent($prompt, false) === 'chat', 'Bare-topic text decisions must be preserved without image history');
    }
    foreach (['Amazon', 'Maybelline', 'amazon coupon'] as $prompt) {
        checkIntent($manager->classifyImageIntent($prompt, true, [], 'draw a cat') === 'chat', 'Unrelated follow-up chat decisions must be preserved');
    }
    $provider->intent = 'clarify';
    checkIntent($manager->classifyImageIntent('another breed', true, [], 'draw a cat') === 'clarify', 'Related follow-up clarification must not be forced into chat');
    checkIntent($manager->classifyImageIntent('bat', false, ['is_image' => 1]) === 'chat', 'Explicit image mode must bypass automatic intent');
    checkIntent($manager->classifyImageIntent('bat', true, [], 'draw a cat', true) === 'edit', 'Explicit image selections must retain edit behavior');

    $provider->intent = 'generate';
    foreach (['draw a bat', 'draw bat', 'genrate bat', "\u{753b}\u{4e00}\u{53ea}\u{8759}\u{8760}"] as $prompt) {
        checkIntent($manager->classifyImageIntent($prompt, false) === 'generate', 'Explicit image requests must still generate');
    }
    $provider->intent = 'clarify';
    checkIntent($manager->classifyImageIntent('bat', false) === 'chat', 'Ambiguous requests must default to chat without image history too');
    $provider->intent = 'chat';
    foreach (['bat', 'design patterns', 'how to draw a bat'] as $prompt) {
        checkIntent($manager->classifyImageIntent($prompt, false) === 'chat', 'Provider text decisions must be preserved');
    }
    $provider->intent = 'edit';
    checkIntent($manager->classifyImageIntent('cartoon', true, [], 'draw a cat') === 'edit', 'Contextual image edits must be preserved');
    checkIntent($manager->classifyImageReply('image', 'bat') === 'image', 'Image choice must be accepted');
    checkIntent($manager->classifyImageReply('text', 'bat') === 'text', 'Text choice must be accepted');

    echo "Image intent checks passed\n";
} finally {
    unlink($temp . 'ai_response_track.php');
    rmdir($temp);
}
