import { adminApiClient } from './client' import type { AxiosError } from 'axios' export interface ParticipantDto { id: string userId: string name: string role: 'HOST' | 'PLAYER' | 'SPECTATOR' score: number joinedAt: string isActive: boolean user: { id: string name?: string email?: string } } export interface RoomDto { id: string code: string status: 'WAITING' | 'PLAYING' | 'FINISHED' hostId: string createdAt: string expiresAt?: string | null isAdminRoom: boolean customCode?: string | null activeFrom?: string | null activeTo?: string | null themeId?: string | null questionPackId?: string | null uiControls?: { allowThemeChange?: boolean allowPackChange?: boolean allowNameChange?: boolean allowScoreEdit?: boolean } | null maxPlayers: number allowSpectators: boolean timerEnabled: boolean timerDuration: number autoAdvance?: boolean voiceMode?: boolean currentQuestionIndex?: number totalQuestions?: number answeredQuestions?: number currentQuestionId?: string | null currentPlayerId?: string | null isGameOver?: boolean revealedAnswers?: Record | null host: { id: string name?: string | null email?: string | null } theme?: { id: string name: string isPublic: boolean } | null questionPack?: { id: string name: string description?: string | null questionCount?: number } | null participants?: ParticipantDto[] _count?: { participants: number } startedAt?: string | null finishedAt?: string | null } export interface CreateAdminRoomDto { hostId: string hostName?: string customCode?: string activeFrom?: string activeTo?: string themeId?: string questionPackId?: string uiControls?: { allowThemeChange?: boolean allowPackChange?: boolean allowNameChange?: boolean allowScoreEdit?: boolean } settings?: { maxPlayers?: number allowSpectators?: boolean timerEnabled?: boolean timerDuration?: number } } export interface RoomsResponse { rooms: RoomDto[] total: number page: number limit: number totalPages: number } export interface RoomsApiError { message: string statusCode?: number originalError?: unknown name: 'RoomsApiError' } export function createRoomsApiError( message: string, statusCode?: number, originalError?: unknown ): RoomsApiError { return { message, statusCode, originalError, name: 'RoomsApiError', } } export function isRoomsApiError(error: unknown): error is RoomsApiError { return ( typeof error === 'object' && error !== null && 'name' in error && error.name === 'RoomsApiError' ) } export const roomsApi = { getRooms: async (params?: { page?: number limit?: number status?: 'WAITING' | 'PLAYING' | 'FINISHED' dateFrom?: string dateTo?: string }): Promise => { try { const response = await adminApiClient.get('/api/admin/rooms', { params: { page: params?.page || 1, limit: params?.limit || 20, status: params?.status, dateFrom: params?.dateFrom, dateTo: params?.dateTo, }, }) return response.data } catch (error) { const axiosError = error as AxiosError<{ message?: string }> throw createRoomsApiError( axiosError.response?.data?.message || 'Failed to load rooms', axiosError.response?.status, error ) } }, getRoom: async (roomId: string): Promise => { try { const response = await adminApiClient.get(`/api/admin/rooms/${roomId}`) return response.data } catch (error) { const axiosError = error as AxiosError<{ message?: string }> if (axiosError.response?.status === 404) { throw createRoomsApiError( `Room not found: The room with ID "${roomId}" does not exist.`, axiosError.response.status, error ) } throw createRoomsApiError( axiosError.response?.data?.message || `Failed to load room ${roomId}`, axiosError.response?.status, error ) } }, createAdminRoom: async (room: CreateAdminRoomDto): Promise => { try { const response = await adminApiClient.post('/api/admin/rooms', room) return response.data } catch (error) { const axiosError = error as AxiosError<{ message?: string }> throw createRoomsApiError( axiosError.response?.data?.message || 'Failed to create room', axiosError.response?.status, error ) } }, deleteRoom: async (roomId: string): Promise<{ message: string }> => { try { const response = await adminApiClient.delete(`/api/admin/rooms/${roomId}`) return response.data } catch (error) { const axiosError = error as AxiosError<{ message?: string }> if (axiosError.response?.status === 404) { throw createRoomsApiError( `Room not found: The room with ID "${roomId}" does not exist.`, axiosError.response.status, error ) } throw createRoomsApiError( axiosError.response?.data?.message || `Failed to delete room ${roomId}`, axiosError.response?.status, error ) } }, }