import { api } from '@/services/api';

export type AiConversationListItem = {
  conversationId: string;
  title?: string | null;
  lastMessageAt?: string | null;
  createdAt?: string | null;
};

export type AiConversationListResponse = {
  page: number;
  pageSize: number;
  total: number;
  items: AiConversationListItem[];
};

export type AiConversationDetail = {
  conversationId: string;
  title?: string | null;
  lastMessageAt?: string | null;
  createdAt?: string | null;
  messages: Array<{ role: 'user' | 'assistant' | 'system'; content: string; createdAt?: string | null }>;
};

export async function listConversations(page = 1, pageSize = 20): Promise<AiConversationListResponse> {
  const qs = `?page=${encodeURIComponent(page)}&pageSize=${encodeURIComponent(pageSize)}`;
  return api.get<AiConversationListResponse>(`/api/ai-coach/conversations${qs}`);
}

export async function getConversationDetail(conversationId: string): Promise<AiConversationDetail> {
  return api.get<AiConversationDetail>(`/api/ai-coach/conversations/${encodeURIComponent(conversationId)}`);
}

export async function deleteConversation(conversationId: string): Promise<{ success: boolean }> {
  return api.delete<{ success: boolean }>(`/api/ai-coach/conversations/${encodeURIComponent(conversationId)}`);
}


