import client from '../client';
import type { PaginatedResponse } from '../types';

// --- Types ---
export interface Session {
  id: string;
  patient_id: string;
  doctor_id?: string;
  patient_name?: string;
  doctor_name?: string;
  consultation_type: string;
  status: string;
  last_message_at?: string;
  unread_count_patient: number;
  unread_count_doctor: number;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateSessionReq {
  patient_id: string;
  doctor_id?: string;
  consultation_type?: string;
}

export interface Message {
  id: string;
  session_id: string;
  sender_id: string;
  sender_role: string;
  content_type: string;
  content: string;
  is_read: boolean;
  created_at: string;
}

export interface CreateMessageReq {
  session_id: string;
  content_type?: string;
  content: string;
}

// --- 咨询联动请求类型 ---
export interface CreateFollowUpFromConsultationReq {
  follow_up_type: string;
  planned_date: string;
  assigned_to?: string;
  content_template?: string;
}

export interface FollowUpFromConsultationResp {
  task_id: string;
  session_id: string;
  patient_id: string;
}

export interface TriggerAiAnalysisReq {
  analysis_type?: string;
}

export interface AiAnalysisTriggeredResp {
  session_id: string;
  patient_id: string;
  analysis_type: string;
}

// --- API ---
export const consultationApi = {
  listSessions: async (params: {
    page?: number;
    page_size?: number;
    status?: string;
    patient_id?: string;
    doctor_id?: string;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<Session>;
    }>('/health/consultation-sessions', { params });
    return data.data;
  },

  createSession: async (req: CreateSessionReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: Session;
    }>('/health/consultation-sessions', req);
    return data.data;
  },

  getSession: async (id: string) => {
    const { data } = await client.get<{
      success: boolean;
      data: Session;
    }>(`/health/consultation-sessions/${id}`);
    return data.data;
  },

  closeSession: async (
    id: string,
    req: { version: number },
  ) => {
    const { data } = await client.put<{
      success: boolean;
      data: Session;
    }>(`/health/consultation-sessions/${id}/close`, req);
    return data.data;
  },

  listMessages: async (
    sessionId: string,
    params: { page?: number; page_size?: number; after_id?: string },
  ) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<Message>;
    }>(`/health/consultation-sessions/${sessionId}/messages`, { params });
    return data.data;
  },

  createMessage: async (req: CreateMessageReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: Message;
    }>('/health/consultation-messages', req);
    return data.data;
  },

  pollMessages: async (
    sessionId: string,
    afterId?: string,
  ) => {
    const { data } = await client.get<{
      success: boolean;
      data: Message[];
    }>(`/health/consultation-sessions/${sessionId}/messages/poll`, {
      params: { after_id: afterId, timeout: 25 },
      timeout: 30000,
    });
    return data.data;
  },

  markSessionRead: async (id: string) => {
    await client.put(`/health/consultation-sessions/${id}/read`);
  },

  exportSessions: async (params?: {
    status?: string;
    patient_id?: string;
    doctor_id?: string;
    page?: number;
    page_size?: number;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<Session>;
    }>('/health/consultation-sessions/export', { params });
    return data.data;
  },

  /** 从咨询会话创建随访任务 */
  createFollowUpFromSession: async (
    sessionId: string,
    req: CreateFollowUpFromConsultationReq,
  ) => {
    const { data } = await client.post<{
      success: boolean;
      data: FollowUpFromConsultationResp;
    }>(`/health/consultation-sessions/${sessionId}/follow-up`, req);
    return data.data;
  },

  /** 从咨询会话触发 AI 分析 */
  triggerAiAnalysisFromSession: async (
    sessionId: string,
    req?: TriggerAiAnalysisReq,
  ) => {
    const { data } = await client.post<{
      success: boolean;
      data: AiAnalysisTriggeredResp;
    }>(`/health/consultation-sessions/${sessionId}/ai-analysis`, req ?? {});
    return data.data;
  },
};
