import { api } from "./client";

export type WatchItem = {
  identifier: string; code: string; name: string; sector: string | null; remark: string;
  pct_chg: number | null; return_5d: number | null; attention_score: number | null;
};
export type ReviewNote = {
  id: number; code: string; stock_name: string; trade_date: string;
  summary: string; content: string; plan: string; updated_at: string;
};
export type TradeEntry = {
  id: number; trade_date: string; code: string; name: string;
  action: "buy" | "sell" | "add" | "trim" | "watch"; action_label: string;
  price: number; quantity: number; position_pct: number | null;
  pnl_amount: number | null; pnl_pct: number | null;
  emotion: "calm" | "confident" | "hesitant" | "anxious" | "impulsive";
  emotion_label: string; tags: string[]; thesis: string; execution: string;
};
export type TradeSummary = {
  total: number; realized: number; win_rate: number | null;
  pnl_amount: number | null; average_position: number | null;
};
export type ReviewWorkspace = {
  trade_date: string; watchlist: WatchItem[]; daily: ReviewNote | null;
  history: ReviewNote[]; trades: TradeEntry[]; trade_summary: TradeSummary;
};
export type AlertItem = {
  id: number; kind: string; title: string; content: string; available_date: string;
  code: string; is_read: boolean; due: boolean; created_at: string;
};
export type AlertCenter = { items: AlertItem[]; unread_count: number; as_of: string };
export type AssistantMessage = {
  id: number; role: "user" | "assistant"; content: string;
  context_date: string; status: "complete" | "stopped" | "error"; created_at: string;
};
export type AssistantEvent = {
  type: "delta" | "done" | "error"; content?: string; message?: string; partial?: boolean;
};

export const reviewApi = {
  workspace(date: string): Promise<ReviewWorkspace> {
    return api.get(`/review?date=${encodeURIComponent(date)}`);
  },
  addWatch(identifier: string): Promise<{ identifier: string; name: string }> {
    return api.post("/review/watchlist", { identifier });
  },
  saveRemark(identifier: string, remark: string): Promise<{ message: string }> {
    return api.patch(`/review/watchlist/${encodeURIComponent(identifier)}/remark`, { remark });
  },
  removeWatch(identifier: string): Promise<{ message: string }> {
    return api.delete(`/review/watchlist/${encodeURIComponent(identifier)}`);
  },
  saveNote(note: Omit<ReviewNote, "id" | "updated_at">): Promise<{ id: number }> {
    return api.put("/review/notes", note);
  },
  stockNotes(code: string): Promise<ReviewNote[]> {
    return api.get(`/review/stock-notes/${encodeURIComponent(code)}`);
  },
  deleteNote(id: number): Promise<{ message: string }> {
    return api.delete(`/review/notes/${id}`);
  },
  saveTrade(trade: Omit<TradeEntry, "action_label" | "emotion_label">): Promise<{ id: number }> {
    return api.put("/review/trades", trade);
  },
  deleteTrade(id: number): Promise<{ message: string }> {
    return api.delete(`/review/trades/${id}`);
  },
  alerts(unread = false): Promise<AlertCenter> {
    return api.get(`/review/alerts?unread=${unread}`);
  },
  createAlert(value: { title: string; remind_date: string; code: string; content: string }): Promise<{ id: number }> {
    return api.post("/review/alerts", value);
  },
  markAlert(id: number): Promise<{ message: string }> {
    return api.patch(`/review/alerts/${id}/read`);
  },
  markAllAlerts(): Promise<{ updated: number }> {
    return api.patch("/review/alerts/read-all");
  },
  deleteAlert(id: number): Promise<{ message: string }> {
    return api.delete(`/review/alerts/${id}`);
  },
  assistantMessages(): Promise<AssistantMessage[]> {
    return api.get("/review/assistant/messages");
  },
  clearAssistant(): Promise<{ deleted: number }> {
    return api.delete("/review/assistant/messages");
  },
  chat(date: string, question: string, onEvent: (event: AssistantEvent) => void, signal?: AbortSignal): Promise<void> {
    return api.stream("/review/assistant/chat", { trade_date: date, question }, onEvent, signal);
  },
};
