import { Socket } from 'socket.io';
import { UserEntity } from '@/server/modules/users/user.entity';

export interface AuthenticatedSocket extends Socket {
  user?: UserEntity;
}

export interface SocketContext {
  socket: AuthenticatedSocket;
  user: UserEntity;
}

// 考试相关的事件类型
export interface ExamRoomData {
  roomId: string;
}

export interface ExamQuestionData {
  roomId: string;
  question: QuizContent;
}

export interface ExamAnswerData {
  roomId: string;
  questionId: string;
  answer: Answer;
}

export interface ExamPriceData {
  roomId: string;
  date: string;
  price: string;
}

// 与前端共享的类型
export interface QuizContent {
  date: string;
  price: string;
  gain?: number;
}

export interface QuizState {
  id: string;
  date: string;
  price: string;
  gain?: number;
}

export interface Answer {
  username: string;
  date: string;
  price: string;
  holdingStock: number;
  holdingCash: number;
  profitAmount: number;
  profitPercent: number;
  totalProfitAmount: number;
  totalProfitPercent: number;
}

// Socket.IO 事件映射
export interface ClientToServerEvents {
  'exam:join': (data: ExamRoomData) => void;
  'exam:leave': (data: ExamRoomData) => void;
  'exam:question': (data: ExamQuestionData) => void;
  'exam:storeAnswer': (data: ExamAnswerData, callback: (success: boolean) => void) => void;
  'exam:getAnswers': (data: { roomId: string; questionId?: string }, callback: (answers: Answer[]) => void) => void;
  'exam:storePrice': (data: ExamPriceData) => void;
  'exam:getPrice': (data: { roomId: string; date: string }, callback: (price: string) => void) => void;
  'exam:getPrices': (data: { roomId: string }, callback: (prices: Record<string, number>) => void) => void;
  'exam:getUserAnswers': (data: { roomId: string; userId: string }, callback: (answers: Answer[]) => void) => void;
  'exam:currentQuestion': (data: ExamRoomData, callback: (question: QuizState | null) => void) => void;
  'exam:settle': (data: ExamRoomData) => void;
  'exam:cleanup': (data: { roomId: string; questionId?: string }) => void;
}

export interface ServerToClientEvents {
  'exam:joined': (data: { roomId: string; message: string }) => void;
  'exam:left': (data: { roomId: string; message: string }) => void;
  'exam:memberJoined': (data: { roomId: string; userId: number; username: string }) => void;
  'exam:memberLeft': (data: { roomId: string; userId: number; username: string }) => void;
  'exam:question': (quizState: QuizState) => void;
  'exam:answerUpdated': (data: { roomId: string; questionId: string; userId: number; username: string }) => void;
  'exam:cleaned': (data: { roomId: string; message: string }) => void;
  'exam:settle': () => void;
  'error': (message: string) => void;
}

export interface InterServerEvents {}

export interface SocketData {
  user: UserEntity;
}