/**
 * Chat Service
 * API client for LLM Copilot chat endpoints
 */

import { apiClient } from '../lib/apiClient';
import type {
  ChatSession,
  SendMessageResponse,
  CreateSessionResponse,
} from '../types/chat.types';

// Uses centralized apiClient from lib/apiClient.ts (auto-refresh, multi-tab sync)
// All paths prefixed with /llm/ since apiClient baseURL is /api/v1

// ============================================================================
// Chat Service
// ============================================================================

export const chatService = {
  /**
   * Create a new chat session
   */
  async createSession(): Promise<CreateSessionResponse> {
    const response = await apiClient.post('/llm/sessions');
    return response.data;
  },

  /**
   * Get all chat sessions for the current user
   */
  async getSessions(): Promise<ChatSession[]> {
    const response = await apiClient.get('/llm/sessions');
    return response.data;
  },

  /**
   * Get a specific chat session with all messages
   */
  async getSession(sessionId: string): Promise<ChatSession> {
    const response = await apiClient.get(`/llm/sessions/${sessionId}`);
    return response.data;
  },

  /**
   * Send a message and get AI response
   */
  async sendMessage(
    sessionId: string,
    message: string
  ): Promise<SendMessageResponse> {
    const response = await apiClient.post(`/llm/sessions/${sessionId}/chat`, {
      message,
    });
    return response.data;
  },

  /**
   * Delete a chat session
   */
  async deleteSession(sessionId: string): Promise<void> {
    await apiClient.delete(`/llm/sessions/${sessionId}`);
  },

  /**
   * Quick analysis of a symbol (public endpoint, no auth required)
   */
  async analyzeSymbol(symbol: string): Promise<{ analysis: string }> {
    const response = await apiClient.get(`/llm/analyze/${symbol}`);
    return response.data;
  },
};

export default chatService;
