File size: 13,574 Bytes
b996603
48eca97
 
 
 
 
 
 
 
 
 
 
 
 
 
b996603
11a0309
 
 
ea4ab6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b996603
 
 
 
 
 
 
 
 
7d996b5
 
cfef7c6
 
 
7d996b5
 
 
cfef7c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b996603
7d996b5
 
 
 
48eca97
7d996b5
48eca97
 
7d996b5
 
b996603
 
 
 
 
 
cfef7c6
 
 
 
 
 
7d996b5
cfef7c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48eca97
7d996b5
 
 
48eca97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b996603
48eca97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfef7c6
48eca97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfef7c6
 
 
 
 
 
 
48eca97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfef7c6
 
 
 
 
 
 
48eca97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d996b5
 
 
 
48eca97
7d996b5
 
 
 
 
 
 
 
 
48eca97
7d996b5
 
48eca97
b996603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d996b5
 
b996603
 
 
 
 
 
 
 
 
7d996b5
b996603
 
 
 
 
 
 
 
7f304b3
 
b996603
7d996b5
 
 
 
b996603
 
 
 
 
 
 
 
 
 
 
 
 
 
7d996b5
 
b996603
 
 
 
 
 
 
 
 
 
815d7a1
 
 
 
 
 
 
 
 
 
 
b996603
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import axios from 'axios';
import {
  createUserWithEmailAndPassword,
  signInWithEmailAndPassword,
  signInWithPopup,
  signOut,
  updateProfile,
} from 'firebase/auth';
import { doc, setDoc, serverTimestamp } from 'firebase/firestore';
import {
  firebaseAuth,
  firebaseDb,
  firebaseGoogleProvider,
  isFirebaseConfigured,
} from './firebase';

const runtimeEnv =
  typeof window !== 'undefined' && window.__ENV__ ? window.__ENV__ : {};

const normalizeApiUrl = (candidate) => {
  const value = (candidate || '').trim();
  if (!value) {
    return '/api';
  }

  if (typeof window !== 'undefined') {
    const isHttpsPage = window.location.protocol === 'https:';
    const isLocalHostTarget = /^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?/i.test(value);
    const isRelative = value.startsWith('/');

    // On hosted HTTPS deployments (like HF Space), avoid mixed-content localhost API URLs.
    if (isHttpsPage && isLocalHostTarget) {
      return '/api';
    }

    if (isRelative) {
      return value;
    }
  }

  return value;
};

const API_URL = normalizeApiUrl(
  runtimeEnv.VITE_API_URL || import.meta.env.VITE_API_URL || '/api'
);

// Set up axios instance
const api = axios.create({
  baseURL: API_URL,
  headers: {
    'Content-Type': 'application/json'
  }
});

let logoutInProgress = false;

const clearClientAuthState = () => {
  localStorage.removeItem('token');
  localStorage.removeItem('user');
  if (api?.defaults?.headers?.common?.Authorization) {
    delete api.defaults.headers.common.Authorization;
  }
  window.dispatchEvent(new Event('authchange'));
};

const getFirebaseAuthErrorMessage = (error, fallbackMessage) => {
  const code = error?.code || '';

  if (
    code === 'auth/invalid-credential' ||
    code === 'auth/wrong-password' ||
    code === 'auth/user-not-found' ||
    code === 'auth/invalid-login-credentials'
  ) {
    return 'Invalid email or password';
  }

  if (code === 'auth/invalid-email') {
    return 'Please enter a valid email address';
  }

  if (code === 'auth/email-already-in-use') {
    return 'This email is already registered';
  }

  if (code === 'auth/weak-password') {
    return 'Password is too weak. Use at least 6 characters';
  }

  if (code === 'auth/popup-closed-by-user') {
    return 'Google sign-in was cancelled';
  }

  return fallbackMessage;
};

// Add token to requests
api.interceptors.request.use((config) => {
  if (logoutInProgress) {
    if (config?.headers?.Authorization) {
      delete config.headers.Authorization;
    }
    return config;
  }

  const token = localStorage.getItem('token');

  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

api.interceptors.response.use(
  (response) => response,
  async (error) => {
    const originalRequest = error.config || {};
    const status = error?.response?.status;

    if (status === 401 && !originalRequest._retry && !logoutInProgress) {
      originalRequest._retry = true;

      if (isFirebaseConfigured && firebaseAuth?.currentUser) {
        try {
          const refreshedToken = await firebaseAuth.currentUser.getIdToken(true);
          if (refreshedToken) {
            localStorage.setItem('token', refreshedToken);
            originalRequest.headers = {
              ...(originalRequest.headers || {}),
              Authorization: `Bearer ${refreshedToken}`,
            };
            return api(originalRequest);
          }
        } catch (_refreshError) {
          // Fall through to clear stale client auth state.
        }
      }

      if (isFirebaseConfigured && firebaseAuth) {
        try {
          await signOut(firebaseAuth);
        } catch (_signOutError) {
          // Ignore signOut errors; local state reset below is enough.
        }
      }

      clearClientAuthState();
    }

    return Promise.reject(error);
  }
);

const hydrateLocalUserFromBackend = async (accessToken) => {
  logoutInProgress = false;
  sessionStorage.removeItem('suppressSocialAutoLogin');

  if (accessToken) {
    localStorage.setItem('token', accessToken);
  }

  try {
    const profile = await api.get('/players/me');
    localStorage.setItem('user', JSON.stringify(profile.data));
    window.dispatchEvent(new Event('authchange'));

    return {
      access_token: accessToken,
      token_type: 'bearer',
      user: profile.data,
    };
  } catch (error) {
    // If backend profile fetch fails, create a minimal user object from Firebase user state.
    if (isFirebaseConfigured && firebaseAuth?.currentUser) {
      const firebaseUser = firebaseAuth.currentUser;

      if (firebaseUser) {
        const minimalUser = {
          id: firebaseUser.uid,
          email: firebaseUser.email,
          name: firebaseUser.displayName || (firebaseUser.email ? firebaseUser.email.split('@')[0] : 'player'),
          role: 'player',
          is_active: true,
        };
        
        localStorage.setItem('user', JSON.stringify(minimalUser));
        window.dispatchEvent(new Event('authchange'));
        
        return {
          access_token: accessToken,
          token_type: 'bearer',
          user: minimalUser,
        };
      }
    }
    
    // Re-throw the original error if fallback also fails
    throw error;
  }
};

export const authService = {
  register: async (name, email, password) => {
    if (!isFirebaseConfigured || !firebaseAuth) {
      return api.post('/auth/register', { name, email, password });
    }

    try {
      const cred = await createUserWithEmailAndPassword(firebaseAuth, email, password);
      if (name) {
        await updateProfile(cred.user, { displayName: name });
      }

      if (firebaseDb) {
        await setDoc(
          doc(firebaseDb, 'users', cred.user.uid),
          {
            uid: cred.user.uid,
            email,
            name: name || email.split('@')[0],
            role: 'player',
            createdAt: serverTimestamp(),
          },
          { merge: true }
        );
      }

      const accessToken = await cred.user.getIdToken();
      return { data: await hydrateLocalUserFromBackend(accessToken) };
    } catch (error) {
      const message = getFirebaseAuthErrorMessage(error, error?.message || 'Registration failed');
      throw { response: { data: { detail: message } } };
    }

  },

  login: async (email, password) => {
    if (!isFirebaseConfigured || !firebaseAuth) {
      return api.post('/auth/login', { email, password });
    }

    try {
      const cred = await signInWithEmailAndPassword(firebaseAuth, email, password);
      const accessToken = await cred.user.getIdToken();
      return { data: await hydrateLocalUserFromBackend(accessToken) };
    } catch (error) {
      throw {
        response: {
          data: {
            detail: getFirebaseAuthErrorMessage(error, error?.message || 'Login failed'),
          },
        },
      };
    }
  },

  loginWithGoogle: async () => {
    if (!isFirebaseConfigured || !firebaseAuth) {
      throw { response: { data: { detail: 'Firebase is not configured' } } };
    }

    try {
      const cred = await signInWithPopup(firebaseAuth, firebaseGoogleProvider);

      if (firebaseDb) {
        await setDoc(
          doc(firebaseDb, 'users', cred.user.uid),
          {
            uid: cred.user.uid,
            email: cred.user.email,
            name: cred.user.displayName || (cred.user.email ? cred.user.email.split('@')[0] : 'player'),
            role: 'player',
            lastLoginAt: serverTimestamp(),
          },
          { merge: true }
        );
      }

      const accessToken = await cred.user.getIdToken();
      return { data: await hydrateLocalUserFromBackend(accessToken) };
    } catch (error) {
      throw {
        response: {
          data: {
            detail: getFirebaseAuthErrorMessage(error, error?.message || 'Google login failed'),
          },
        },
      };
    }
  },

  completeSocialLogin: async () => {
    if (!isFirebaseConfigured || !firebaseAuth?.currentUser) {
      return null;
    }

    const accessToken = await firebaseAuth.currentUser.getIdToken();
    if (!accessToken) {
      return null;
    }

    return { data: await hydrateLocalUserFromBackend(accessToken) };
  },

  logout: async () => {
    logoutInProgress = true;
    sessionStorage.setItem('suppressSocialAutoLogin', '1');
    clearClientAuthState();

    if (isFirebaseConfigured && firebaseAuth) {
      // Do not block UI logout on remote Firebase sign-out latency.
      signOut(firebaseAuth)
        .catch(() => {
          // Ignore remote sign-out failures; local logout is already complete.
        })
        .finally(() => {
          logoutInProgress = false;
        });
      return;
    }

    logoutInProgress = false;
  },
};

export const playerService = {
  getCurrentPlayer: () => api.get('/players/me'),
  updateProfile: (data) => api.put('/players/me', data),
  updateCareerStats: (data) => api.put('/players/me/career-stats', data),
  getPlayer: (id) => api.get(`/players/${id}`),
  getAllPlayers: (skip = 0, limit = 50) =>
    api.get(`/players?skip=${skip}&limit=${limit}`),
  getPremiumPlayers: () => api.get('/players/premium'),
  getTopPerformers: (limit = 10) =>
    api.get(`/players/leaderboard/top-performers?limit=${limit}`),
  getTopWicketTakers: (limit = 10) =>
    api.get(`/players/leaderboard/by-wickets?limit=${limit}`),
};

export const premiumService = {
  upgradePremium: (days = 30) =>
    api.post('/premium/upgrade', { plan_days: days }),
  getPremiumStatus: () => api.get('/premium/status'),
  cancelPremium: () => api.post('/premium/cancel'),
  getPaymentHistory: () => api.get('/premium/payments'),
};

export const performanceService = {
  logPerformance: (data) => api.post('/performance', data),
  updateLog: (logId, data) => api.put(`/performance/${logId}`, data),
  deleteLog: (logId) => api.delete(`/performance/${logId}`),
  getMyLogs: (skip = 0, limit = 20) =>
    api.get(`/performance/my-logs?skip=${skip}&limit=${limit}`),
  getMatchHistory: (skip = 0, limit = 30) =>
    api.get(`/performance/match-history?skip=${skip}&limit=${limit}`),
  getPlayerLogs: (playerId, skip = 0, limit = 20) =>
    api.get(`/performance/player/${playerId}?skip=${skip}&limit=${limit}`),
  getPlayerStats: (playerId) => api.get(`/performance/stats/${playerId}`),
  getAiInsights: (playerId, forceRefresh = false) =>
    api.get(`/performance/ai-insights/player/${playerId}${forceRefresh ? '?force_refresh=true' : ''}`),
};

export const dashboardService = {
  getOverview: () => api.get('/dashboard/overview'),
  getExtendedOverview: () => api.get('/dashboard/extended-overview'),
  getFeaturedPlayers: () => api.get('/dashboard/featured-players'),
  getRecentPlayers: () => api.get('/dashboard/recent-players'),
  getTopStats: () => api.get('/dashboard/top-stats'),
  getCharts: () => api.get('/dashboard/charts'),
  getTeamInsights: (forceRefresh = false) => api.get(`/performance/ai-insights/team-pulse${forceRefresh ? '?force_refresh=true' : ''}`),
  getFundsSummary: () => api.get('/dashboard/funds-summary'),
};

export const adminService = {
  getAllUsers: (skip = 0, limit = 100) =>
    api.get(`/admin/users?skip=${skip}&limit=${limit}`),
  toggleUserPremium: (userId, days = 30) =>
    api.put(`/admin/users/${userId}/premium?days=${days}`),
  approvePremiumRequest: (userId, days = 30) =>
    api.post(`/admin/users/${userId}/approve-premium?days=${days}`),
  deactivateUser: (userId) => api.delete(`/admin/users/${userId}`),
  activateUser: (userId) => api.post(`/admin/users/${userId}/activate`),
  updateUserRole: (userId, role) => api.put(`/admin/users/${userId}/role?role=${role}`),
  resetUserPassword: (userId) => api.post(`/admin/users/${userId}/reset-password`),
  hardDeleteUser: (userId) => api.delete(`/admin/users/${userId}/hard-delete`),
  getSystemStats: () => api.get('/admin/stats'),
  getChatThreads: () => api.get('/admin/chats'),
  getChatThread: (userId) => api.get(`/admin/chats/${userId}`),
  sendAdminMessage: (userId, message) =>
    api.post(`/admin/chats/${userId}`, { message }),
  getMyChat: () => api.get('/admin/my-chat'),
  sendPlayerMessage: (message) => api.post('/admin/my-chat', { message }),
};

export const notificationService = {
  getMine: () => api.get('/notifications/me'),
  checkExpiry: () => api.post('/notifications/check-expiry'),
  markRead: (id) => api.put(`/notifications/${id}/read`),
  markAllRead: () => api.put('/notifications/read-all'),
  removeRead: (id) => api.delete(`/notifications/${id}`),
  clearAllRead: () => api.delete('/notifications/clear-read'),
};

export const financeService = {
  getOverview: () => api.get('/finance/overview'),
  getPlayerPayments: () => api.get('/finance/player-payments'),
  getTransactions: () => api.get('/finance/transactions'),
  addGuestFundExpense: (payload) => api.post('/finance/guest-fund', payload),
  addManualCredit: (payload) => api.post('/finance/manual-credit', payload),
};

export const matchesService = {
  createMatch: (payload) => api.post('/matches', payload),
  listMatches: () => api.get('/matches'),
  getMatch: (matchId) => api.get(`/matches/${matchId}`),
  setupTeams: (matchId, payload) => api.post(`/matches/${matchId}/teams`, payload),
  startMatch: (matchId, payload) => api.post(`/matches/${matchId}/start`, payload),
  recordBall: (matchId, payload) => api.post(`/matches/${matchId}/ball`, payload),
  getScoreboard: (matchId, innings = 1) => api.get(`/matches/${matchId}/scoreboard?innings=${innings}`),
  completeMatch: (matchId) => api.post(`/matches/${matchId}/complete`),
};

export default api;