<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require __DIR__ . '/../../app/admin-sidebar.php';
require_login();

$adminName = isset($_SESSION['admin_name']) ? (string)$_SESSION['admin_name'] : 'Admin';
$adminUsername = isset($_SESSION['admin_username']) ? (string)$_SESSION['admin_username'] : 'admin';
$avatarText = strtoupper(substr($adminName !== '' ? $adminName : 'A', 0, 1));
$error = '';
$success = '';
$result = null;
$model = null;
$modelId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$prompt = ai_model_test_prompt_default();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    require_csrf();
    $modelId = isset($_POST['id']) ? (int)$_POST['id'] : $modelId;
    $prompt = isset($_POST['prompt']) ? trim((string)$_POST['prompt']) : $prompt;
}

if ($modelId <= 0) {
    $error = '缺少有效的 AI 模型 ID，无法进行测试。';
} else {
    try {
        $model = find_ai_model_by_id($modelId);
        if (!$model) {
            $error = '未找到对应的 AI 模型配置。';
        } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $result = test_ai_model_by_id($modelId, $prompt);
            $model = $result['model'];
            if (!empty($result['success'])) {
                $success = 'DeepSeek 接口调用成功。';
            } else {
                $error = $result['error_message'] !== '' ? $result['error_message'] : '接口已返回响应，但状态异常，请检查返回内容。';
            }
        }
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
}
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo e(APP_NAME); ?> - AI模型测试</title>
    <link rel="stylesheet" href="/assets/css/admin-ui.css">
</head>
<body>
<div class="admin-shell">
    <?php render_admin_sidebar('ai_models', $adminName, $adminUsername, $avatarText); ?>
    <main class="admin-content">
        <div class="content-wrap">
            <header class="topbar">
                <div><h1>AI模型测试</h1><p>基于当前已保存的 DeepSeek 配置发起一次真实接口调用，验证地址、Key 与模型可用性。</p></div>
                <div class="topbar-actions"><a class="btn-ghost" href="/admin/ai-model/list.php">返回列表</a><a class="btn-primary" href="/admin/ai-model/edit.php?id=<?php echo (int)$modelId; ?>">编辑配置</a></div>
            </header>
            <section class="page-card">
                <div class="card-head"><div><h2>接口测试表单</h2><p>默认会使用 DeepSeek OpenAI 兼容聊天补全接口格式。</p></div></div>
                <?php if ($success !== ''): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(34,197,94,0.18); background: rgba(34,197,94,0.06);">
                        <strong style="color:#15803d;">调用成功</strong>
                        <p style="margin:0; color:#166534;"><?php echo e($success); ?></p>
                    </div>
                <?php endif; ?>
                <?php if ($error !== ''): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(220,38,38,0.18); background: rgba(220,38,38,0.06);">
                        <strong style="color:#dc2626;">调用结果</strong>
                        <p style="margin:0; color:#b91c1c;"><?php echo e($error); ?></p>
                    </div>
                <?php endif; ?>
                <?php if ($model): ?>
                    <div class="mini-grid" style="margin-bottom:16px;">
                        <div class="mini-item"><strong>模型名称</strong><p><?php echo e($model['name']); ?></p></div>
                        <div class="mini-item"><strong>模型ID</strong><p><?php echo e($model['model_name']); ?></p></div>
                        <div class="mini-item"><strong>接口地址</strong><p><?php echo e($model['api_url']); ?></p></div>
                        <div class="mini-item"><strong>状态</strong><p><?php echo e($model['status']); ?></p></div>
                    </div>
                    <form method="post" action="/admin/ai-model/test.php?id=<?php echo (int)$modelId; ?>">
                        <?php echo csrf_field(); ?>
                        <input type="hidden" name="id" value="<?php echo (int)$modelId; ?>">
                        <div class="input-group" style="margin-bottom:16px;">
                            <span>测试提示词</span>
                            <textarea class="input" name="prompt" rows="5" placeholder="请输入测试提示词"><?php echo e($prompt); ?></textarea>
                        </div>
                        <div class="filter-row">
                            <button class="btn-primary" type="submit">立即测试</button>
                            <a class="btn-ghost" href="/admin/ai-model/test.php?id=<?php echo (int)$modelId; ?>">重置</a>
                        </div>
                    </form>
                <?php endif; ?>
            </section>
            <?php if ($result): ?>
                <section class="page-card" style="margin-top:20px;">
                    <div class="card-head"><div><h2>测试结果</h2><p>以下内容用于排查接口响应与参数问题。</p></div></div>
                    <div class="mini-grid" style="margin-bottom:16px;">
                        <div class="mini-item"><strong>HTTP状态码</strong><p><?php echo (int)$result['http_code']; ?></p></div>
                        <div class="mini-item"><strong>调用状态</strong><p><?php echo !empty($result['success']) ? '成功' : '失败'; ?></p></div>
                    </div>
                    <div class="input-group" style="margin-bottom:16px;">
                        <span>模型回复</span>
                        <textarea class="input" rows="6" readonly><?php echo e($result['assistant_text'] !== '' ? $result['assistant_text'] : '未提取到 assistant 回复内容'); ?></textarea>
                    </div>
                    <div class="input-group" style="margin-bottom:16px;">
                        <span>请求 JSON</span>
                        <textarea class="input" rows="12" readonly><?php echo e($result['request_payload_json']); ?></textarea>
                    </div>
                    <div class="input-group">
                        <span>原始响应</span>
                        <textarea class="input" rows="16" readonly><?php echo e($result['response_body']); ?></textarea>
                    </div>
                </section>
            <?php endif; ?>
        </div>
    </main>
</div>
<script src="/assets/js/admin-ui.js"></script>
</body>
</html>
