import { api } from "./client";

export type Mentor = {
  id: string;
  name: string;
  description: string;
  tagline: string;
  focus: string[];
  grade: "A" | "B" | "C";
  evidence_label: string;
  evidence_note: string;
  private: boolean;
  pinned: boolean;
  sort_order: number;
};
export type MentorSetup = { trade_date: string; mentors: Mentor[] };
export type MentorMessage = {
  id?: number;
  role: "user" | "assistant";
  content: string;
  status: "complete" | "stopped" | "error";
  created_at?: string;
};
export type MentorStreamEvent = {
  type: "delta" | "done" | "error";
  content?: string;
  code?: string;
  message?: string;
  partial?: boolean;
  request_id: string;
};

export const mentorApi = {
  setup(date: string): Promise<MentorSetup> {
    return api.get(`/mentors/setup?date=${encodeURIComponent(date)}`);
  },
  preferences(order: string[], pinned: string[]): Promise<{ order: string[]; pinned: string[] }> {
    return api.put("/mentors/preferences", { order, pinned });
  },
  messages(mentorId: string, date: string): Promise<MentorMessage[]> {
    return api.get(`/mentors/messages?mentor_id=${encodeURIComponent(mentorId)}&date=${encodeURIComponent(date)}`);
  },
  clear(mentorId: string, date: string): Promise<{ deleted: number }> {
    return api.delete(`/mentors/messages?mentor_id=${encodeURIComponent(mentorId)}&date=${encodeURIComponent(date)}`);
  },
  chat(
    mentorId: string,
    tradeDate: string,
    question: string,
    onEvent: (event: MentorStreamEvent) => void,
    signal: AbortSignal,
  ): Promise<void> {
    return api.stream("/mentors/chat", { mentor_id: mentorId, trade_date: tradeDate, question }, onEvent, signal);
  },
};
