/**
 * AI Assistant router — provides a chat endpoint that uses OpenAI Function Calling
 * to answer questions about CapaKraken data and modify resources/projects.
 */

import { createTRPCRouter, protectedProcedure } from "../trpc.js";
import {
  TOOL_DEFINITIONS,
} from "./assistant-tools.js";
import { type ChatMessage } from "./assistant-confirmation.js";
import {
  assistantChatInputSchema,
  listPendingApprovalPayloads,
  runAssistantChat,
} from "./assistant-procedure-support.js";

export {
  AssistantApprovalStorageUnavailableError,
  createPendingAssistantApproval,
  clearPendingAssistantApproval,
  consumePendingAssistantApproval,
  listPendingAssistantApprovals,
  peekPendingAssistantApproval,
  resetAssistantApprovalStorageWarningStateForTests,
  toApprovalPayload,
  type AssistantApprovalPayload,
  type PendingAssistantApproval,
} from "./assistant-approvals.js";
export {
  ASSISTANT_CONFIRMATION_PREFIX,
  buildApprovalSummary,
  canExecuteMutationTool,
  formatApprovalValue,
  hasPendingAssistantConfirmation,
  isAffirmativeConfirmationReply,
  isCancellationReply,
  parseToolArguments,
  type ChatMessage,
} from "./assistant-confirmation.js";
export { getAvailableAssistantTools } from "./assistant-tool-policy.js";
export { selectAssistantToolsForRequest } from "./assistant-tool-selection.js";

export const assistantRouter = createTRPCRouter({
  listPendingApprovals: protectedProcedure
    .query(({ ctx }) => listPendingApprovalPayloads(ctx)),
  chat: protectedProcedure
    .input(assistantChatInputSchema)
    .mutation(({ ctx, input }) => runAssistantChat(ctx, input)),
});
