// ============================================================
// scenarios API — 用户自建情景 API 调用
// 职责：封装 /api/scenarios 的 CRUD 操作
// ============================================================

// 开发环境通过 Vite 代理，生产环境使用同域名
const API_BASE = "";

export interface UserScenario {
  id: string;
  user_id: string;
  name: string;
  icon: string;
  description: string;
  prompt: string;
  greeting: string;
  language: string;
  created_at: string;
  updated_at: string;
}

export interface CreateScenarioRequest {
  name: string;
  icon?: string;
  description?: string;
  prompt: string;
  greeting?: string;
  language?: string;
}

export interface UpdateScenarioRequest {
  name?: string;
  icon?: string;
  description?: string;
  prompt?: string;
  greeting?: string;
  language?: string;
}

export interface ScenariosListResponse {
  scenarios: UserScenario[];
  total: number;
}

// 获取用户的所有自建情景
export async function listUserScenarios(token: string): Promise<ScenariosListResponse> {
  const res = await fetch(`${API_BASE}/api/scenarios`, {
    headers: { Authorization: `Bearer ${token}` },
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: "Network error" }));
    throw new Error(err.error || "Failed to list scenarios");
  }
  return res.json();
}

// 创建新情景
export async function createUserScenario(
  token: string,
  data: CreateScenarioRequest
): Promise<UserScenario> {
  const res = await fetch(`${API_BASE}/api/scenarios`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(data),
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: "Network error" }));
    throw new Error(err.error || "Failed to create scenario");
  }
  return res.json();
}

// 获取单个情景详情
export async function getUserScenario(token: string, id: string): Promise<UserScenario> {
  const res = await fetch(`${API_BASE}/api/scenarios/${id}`, {
    headers: { Authorization: `Bearer ${token}` },
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: "Network error" }));
    throw new Error(err.error || "Failed to get scenario");
  }
  return res.json();
}

// 更新情景
export async function updateUserScenario(
  token: string,
  id: string,
  data: UpdateScenarioRequest
): Promise<UserScenario> {
  const res = await fetch(`${API_BASE}/api/scenarios/${id}`, {
    method: "PATCH",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(data),
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: "Network error" }));
    throw new Error(err.error || "Failed to update scenario");
  }
  return res.json();
}

// 删除情景
export async function deleteUserScenario(token: string, id: string): Promise<void> {
  const res = await fetch(`${API_BASE}/api/scenarios/${id}`, {
    method: "DELETE",
    headers: { Authorization: `Bearer ${token}` },
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: "Network error" }));
    throw new Error(err.error || "Failed to delete scenario");
  }
}
