import axios from 'axios'; import type { Game, Route, RouteLeg, Team, User, AuthResponse, LocationHistory, UserGameHistory } from '../types'; const api = axios.create({ baseURL: 'http://localhost:3001/api', }); api.interceptors.request.use((config) => { const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }); export const authService = { register: (data: { email: string; password: string; name: string }) => api.post('/auth/register', data), login: (data: { email: string; password: string }) => api.post('/auth/login', data), me: () => api.get('/auth/me'), }; export const gameService = { list: (params?: { search?: string; status?: string }) => api.get('/games', { params }), myGames: () => api.get('/games/my-games'), get: (id: string) => api.get(`/games/${id}`), getByInvite: (code: string) => api.get(`/games/invite/${code}`), create: (data: Partial) => api.post('/games', data), update: (id: string, data: Partial) => api.put(`/games/${id}`, data), delete: (id: string) => api.delete(`/games/${id}`), publish: (id: string) => api.post(`/games/${id}/publish`), end: (id: string) => api.post(`/games/${id}/end`), archive: (id: string) => api.post(`/games/${id}/archive`), unarchive: (id: string) => api.post(`/games/${id}/unarchive`), getInvite: (id: string) => api.get<{ inviteCode: string }>(`/games/${id}/invite`), }; export const routeService = { getByGame: (gameId: string) => api.get(`/routes/game/${gameId}`), get: (routeId: string) => api.get(`/routes/${routeId}`), create: (data: { gameId: string; name: string; description?: string; color?: string }) => api.post('/routes', data), update: (routeId: string, data: Partial) => api.put(`/routes/${routeId}`, data), delete: (routeId: string) => api.delete(`/routes/${routeId}`), copy: (routeId: string) => api.post(`/routes/${routeId}/copy`), addLeg: (routeId: string, data: Partial) => api.post(`/routes/${routeId}/legs`, data), updateLeg: (routeId: string, legId: string, data: Partial) => api.put(`/routes/${routeId}/legs/${legId}`, data), deleteLeg: (routeId: string, legId: string) => api.delete(`/routes/${routeId}/legs/${legId}`), }; export const teamService = { getByGame: (gameId: string) => api.get(`/teams/game/${gameId}`), create: (gameId: string, data: { name: string }) => api.post(`/teams/game/${gameId}`, data), get: (teamId: string) => api.get(`/teams/${teamId}`), join: (teamId: string) => api.post(`/teams/${teamId}/join`), leave: (teamId: string) => api.post(`/teams/${teamId}/leave`), assignRoute: (teamId: string, routeId: string) => api.post(`/teams/${teamId}/assign-route`, { routeId }), advance: (teamId: string) => api.post(`/teams/${teamId}/advance`), deduct: (teamId: string, seconds: number) => api.post(`/teams/${teamId}/deduct`, { seconds }), disqualify: (teamId: string) => api.post(`/teams/${teamId}/disqualify`), updateLocation: (teamId: string, lat: number, lng: number) => api.post(`/teams/${teamId}/location`, { lat, lng }), }; export const uploadService = { upload: (file: File) => { const formData = new FormData(); formData.append('photo', file); return api.post<{ url: string }>('/upload/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, }); }, }; export const userService = { getProfile: () => api.get('/users/me'), updateProfile: (data: { name?: string; screenName?: string; avatarUrl?: string }) => api.put('/users/me', data), getLocationHistory: () => api.get<{ totalLocations: number; byGame: { game: { id: string; name: string }; locations: LocationHistory[]; locationCount: number }[] }>('/users/me/location-history'), getGamesHistory: () => api.get('/users/me/games'), deleteLocationData: () => api.delete('/users/me/location-data'), deleteAccount: () => api.delete('/users/me/account'), }; export default api;