/**
 * Conversation related type definitions
 */

import type { UUID, Timestamps } from './common.types.js';
import type { ImmigrationCategory } from './immigration.types.js';

export enum ConversationStatus {
  ACTIVE = 'ACTIVE',
  ENDED = 'ENDED',
  ARCHIVED = 'ARCHIVED',
}

export enum MessageRole {
  USER = 'user',
  ASSISTANT = 'assistant',
  SYSTEM = 'system',
}

export enum MessageType {
  TEXT = 'TEXT',
  TOOL_CALL = 'TOOL_CALL',
  TOOL_RESULT = 'TOOL_RESULT',
  PAYMENT_REQUEST = 'PAYMENT_REQUEST',
  ASSESSMENT_START = 'ASSESSMENT_START',
  ASSESSMENT_RESULT = 'ASSESSMENT_RESULT',
}

export interface Conversation extends Timestamps {
  id: UUID;
  userId: UUID;
  status: ConversationStatus;
  title?: string;
  summary?: string;
  category?: ImmigrationCategory;
  messageCount: number;
  endedAt?: Date;
}

export interface Message extends Timestamps {
  id: UUID;
  conversationId: UUID;
  role: MessageRole;
  type: MessageType;
  content: string;
  metadata?: MessageMetadata;
}

export interface MessageMetadata {
  toolName?: string;
  toolInput?: Record<string, unknown>;
  toolResult?: Record<string, unknown>;
  paymentInfo?: PaymentMessageInfo;
  assessmentInfo?: AssessmentMessageInfo;
  tokenUsage?: TokenUsage;
}

export interface PaymentMessageInfo {
  orderId: UUID;
  amount: number;
  qrCodeUrl?: string;
  paymentMethod?: string;
}

export interface AssessmentMessageInfo {
  category: ImmigrationCategory;
  score?: number;
  recommendation?: string;
  details?: Record<string, unknown>;
}

export interface TokenUsage {
  inputTokens: number;
  outputTokens: number;
}

// WebSocket Message Types
export interface WsMessage {
  type: WsMessageType;
  payload: unknown;
}

export enum WsMessageType {
  // Client -> Server
  MESSAGE = 'message',
  TYPING_START = 'typing_start',
  TYPING_END = 'typing_end',

  // Server -> Client
  STREAM_START = 'stream_start',
  STREAM_CHUNK = 'stream_chunk',
  STREAM_END = 'stream_end',
  TOOL_CALL = 'tool_call',
  TOOL_RESULT = 'tool_result',
  ERROR = 'error',
  CONNECTED = 'connected',

  // Multi-Agent Events
  AGENT_START = 'agent_start',
  AGENT_COMPLETE = 'agent_complete',
  COORDINATOR_THINKING = 'coordinator_thinking',
}

export interface SendMessageDto {
  conversationId?: UUID;
  content: string;
}

export interface StreamChunk {
  messageId: UUID;
  content: string;
  index: number;
  isComplete: boolean;
}

export interface CreateConversationDto {
  title?: string;
}

export interface ConversationListItem {
  id: UUID;
  title?: string;
  summary?: string;
  lastMessageAt: Date;
  messageCount: number;
  status: ConversationStatus;
}
