/**
 * Product GenUI Edge Function 测试
 * 使用 Deno 测试框架验证商品Agent GenUI接口功能
 */
import "https://deno.land/std@0.224.0/dotenv/load.ts";
import { assertEquals, assertExists, assert } from "https://deno.land/std@0.224.0/assert/mod.ts";

const SUPABASE_URL = Deno.env.get("VITE_SUPABASE_URL")!;
const SUPABASE_ANON_KEY = Deno.env.get("VITE_SUPABASE_PUBLISHABLE_KEY")!;

const FUNCTION_URL = `${SUPABASE_URL}/functions/v1/product-genui`;

// ========== 类型定义 ==========
interface UIComponent {
  type: string;
  id?: string;
  props: Record<string, unknown>;
}

interface UISchema {
  layout: 'vertical' | 'horizontal' | 'grid';
  gap: 'sm' | 'md' | 'lg';
  components: UIComponent[];
}

interface GenUIResponse {
  success: boolean;
  answer?: string;
  ui_schema?: UISchema;
  insights?: unknown[];
  intent?: string;
}

// ========== 辅助函数 ==========
async function callGenUI(body: Record<string, unknown>): Promise<Response> {
  return await fetch(FUNCTION_URL, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'apikey': SUPABASE_ANON_KEY,
    },
    body: JSON.stringify(body),
  });
}

// ========== 基础功能测试 ==========
Deno.test("product-genui: CORS预检请求应返回200", async () => {
  const response = await fetch(FUNCTION_URL, { method: 'OPTIONS' });
  await response.text();
  assertEquals(response.status, 200);
});

Deno.test("product-genui: 基本消息应返回成功响应", async () => {
  const response = await callGenUI({ message: '商品概览' });
  const data: GenUIResponse = await response.json();
  
  assertEquals(response.status, 200);
  assertEquals(data.success, true);
});

// ========== 意图检测测试 ==========
Deno.test("product-genui: 库存查询应被正确识别", async () => {
  const response = await callGenUI({ message: '库存不足的商品' });
  const data: GenUIResponse = await response.json();
  
  assertEquals(data.success, true);
  assertExists(data.ui_schema);
});

Deno.test("product-genui: 分类查询应被识别", async () => {
  const response = await callGenUI({ message: '商品分类' });
  const data: GenUIResponse = await response.json();
  
  assertEquals(data.success, true);
});

Deno.test("product-genui: 品牌查询应被识别", async () => {
  const response = await callGenUI({ message: '品牌列表' });
  const data: GenUIResponse = await response.json();
  
  assertEquals(data.success, true);
});

// ========== UI Schema 契约测试 ==========
Deno.test("product-genui: 低库存预警应返回alert或data_table", async () => {
  const response = await callGenUI({ message: '低库存预警' });
  const data: GenUIResponse = await response.json();
  
  assertEquals(data.success, true);
  if (data.ui_schema) {
    const hasAlertOrTable = data.ui_schema.components.some(c => 
      c.type === 'alert' || c.type === 'data_table'
    );
    // 根据数据可能有不同呈现
    assert(data.ui_schema.components.length >= 0);
  }
});

Deno.test("product-genui: 统计数据应包含stats_row", async () => {
  const response = await callGenUI({ message: '商品统计' });
  const data: GenUIResponse = await response.json();
  
  assertEquals(data.success, true);
  if (data.ui_schema) {
    const hasStats = data.ui_schema.components.some(c => c.type === 'stats_row');
    assert(hasStats || data.ui_schema.components.length > 0);
  }
});
