export type Provider = "openai" | "anthropic" | "xai" | "gemini" | "hermes-agent";

export type ProviderModelInfo = {
  models: string[];
  loadedAt: string | null;
  error: string | null;
};

export type ModelCatalogResponse = {
  providers: Partial<Record<Provider, ProviderModelInfo>>;
};

export type ChatSummary = {
  id: string;
  title: string | null;
  parentChatId: string | null;
  titleGenerationPending: boolean;
  createdAt: string;
  updatedAt: string;
  starred: boolean;
  starredAt: string | null;
  initiatedProvider: Provider | null;
  initiatedModel: string | null;
  lastUsedProvider: Provider | null;
  lastUsedModel: string | null;
};

export type SearchSummary = {
  id: string;
  title: string | null;
  query: string | null;
  createdAt: string;
  updatedAt: string;
  starred: boolean;
  starredAt: string | null;
};

export type ChatWorkspaceItem = ChatSummary & {
  type: "chat";
};

export type SearchWorkspaceItem = SearchSummary & {
  type: "search";
};

export type WorkspaceItem = ChatWorkspaceItem | SearchWorkspaceItem;

export type Message = {
  id: string;
  createdAt: string;
  role: "system" | "user" | "assistant" | "tool";
  content: string;
  name: string | null;
  metadata: unknown | null;
};

export type ToolCallEvent = {
  toolCallId: string;
  name: string;
  status: "initiated" | "completed" | "failed";
  summary: string;
  args: Record<string, unknown>;
  startedAt: string;
  completedAt?: string;
  durationMs?: number;
  error?: string;
  resultPreview?: string;
};

export type ChatDetail = {
  id: string;
  title: string | null;
  parentChatId: string | null;
  titleGenerationPending: boolean;
  createdAt: string;
  updatedAt: string;
  starred: boolean;
  starredAt: string | null;
  initiatedProvider: Provider | null;
  initiatedModel: string | null;
  lastUsedProvider: Provider | null;
  lastUsedModel: string | null;
  messages: Message[];
};

export type SearchResultItem = {
  id: string;
  createdAt: string;
  rank: number;
  title: string | null;
  url: string;
  publishedDate: string | null;
  author: string | null;
  text: string | null;
  highlights: string[] | null;
  highlightScores: number[] | null;
  score: number | null;
  favicon: string | null;
  image: string | null;
};

export type SearchDetail = {
  id: string;
  title: string | null;
  query: string | null;
  createdAt: string;
  updatedAt: string;
  starred: boolean;
  starredAt: string | null;
  requestId: string | null;
  latencyMs: number | null;
  error: string | null;
  answerText: string | null;
  answerRequestId: string | null;
  answerCitations: Array<{
    id?: string;
    url?: string;
    title?: string | null;
    publishedDate?: string | null;
    author?: string | null;
    text?: string | null;
  }> | null;
  answerError: string | null;
  results: SearchResultItem[];
};

export type SearchRunRequest = {
  query?: string;
  title?: string;
  type?: "auto" | "fast" | "deep" | "instant";
  numResults?: number;
  includeDomains?: string[];
  excludeDomains?: string[];
};

export type CompletionRequestMessage = {
  role: "system" | "user" | "assistant" | "tool";
  content: string;
  name?: string;
};

export type CompletionStreamHandlers = {
  onMeta?: (payload: { chatId: string; callId: string; provider: Provider; model: string }) => void;
  onToolCall?: (payload: ToolCallEvent) => void;
  onDelta?: (payload: { text: string }) => void;
  onDone?: (payload: { text: string; usage?: { inputTokens?: number; outputTokens?: number; totalTokens?: number } }) => void;
  onError?: (payload: { message: string }) => void;
};

export type SearchStreamHandlers = {
  onSearchResults?: (payload: { requestId: string | null; results: SearchResultItem[] }) => void;
  onSearchError?: (payload: { error: string }) => void;
  onAnswer?: (payload: { answerText: string | null; answerRequestId: string | null; answerCitations: SearchDetail["answerCitations"] }) => void;
  onAnswerError?: (payload: { error: string }) => void;
  onDone?: (payload: { search: SearchDetail }) => void;
  onError?: (payload: { message: string }) => void;
};

export type SessionStatus = {
  authenticated: true;
  mode: "open" | "token";
};
