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
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
// Real API service for authentication with backend
import AsyncStorage from '@react-native-async-storage/async-storage';

class AuthService {
  constructor() {
    // Use centralized config for API base URL
    try {
      const { API_BASE_URL } = require('./config');
      this.baseURL = API_BASE_URL;
    } catch (e) {
      this.baseURL = 'http://192.168.1.8:5000'; // fallback
    }
    this.isRefreshing = false;
    this.failedQueue = [];
  }

  // Process queue of failed requests after token refresh
  processQueue(error, token = null) {
    this.failedQueue.forEach(prom => {
      if (error) {
        prom.reject(error);
      } else {
        prom.resolve(token);
      }
    });
    this.failedQueue = [];
  }

  // Get stored access token
  async getAccessToken() {
    try {
      // 1. Check NEW system token first (authToken)
      const authToken = await AsyncStorage.getItem('authToken');
      if (authToken) {
        // console.log('πŸ”‘ AuthService: Using authToken (New System)');
        return authToken;
      }

      // 2. Fallback to OLD system token (accessToken)
      const token = await AsyncStorage.getItem('accessToken');
      if (token) {
        // console.log('πŸ”‘ AuthService: Using accessToken (Legacy)');
        return token;
      }

      console.log('⚠️ AuthService: No token found');
      return null;
    } catch (error) {
      console.error('Error getting access token:', error);
      return null;
    }
  }

  // Get stored refresh token
  async getRefreshToken() {
    try {
      return await AsyncStorage.getItem('refreshToken');
    } catch (error) {
      console.error('Error getting refresh token:', error);
      return null;
    }
  }

  // Save tokens to AsyncStorage
  async saveTokens(accessToken, refreshToken) {
    try {
      await AsyncStorage.setItem('accessToken', accessToken);
      await AsyncStorage.setItem('refreshToken', refreshToken);
      console.log('βœ… Tokens saved to AsyncStorage');
    } catch (error) {
      console.error('❌ Error saving tokens:', error);
    }
  }

  // Refresh access token using refresh token
  async refreshAccessToken() {
    // The new backend system currently handles session expiration via re-login
    // There is no explicit refresh token flow in the current router inspection
    console.log('⚠️ Token refresh not supported in new backend. Forcing logout.');
    await this.clearTokens();
    throw new Error('Session expired. Please login again.');
  }

  // Make authenticated API call with automatic token refresh
  async authenticatedFetch(url, options = {}) {
    let accessToken = await this.getAccessToken();

    if (!accessToken) {
      throw new Error('No access token available. Please login.');
    }

    // Add authorization header
    const headers = {
      ...options.headers,
      'Authorization': `Bearer ${accessToken}`,
    };

    let response = await fetch(url, { ...options, headers });

    // If unauthorized, try to refresh token
    if (response.status === 401) {
      if (!this.isRefreshing) {
        this.isRefreshing = true;
        try {
          accessToken = await this.refreshAccessToken();
          this.isRefreshing = false;
          this.processQueue(null, accessToken);
        } catch (error) {
          this.isRefreshing = false;
          this.processQueue(error, null);
          throw error;
        }
      }

      // Retry the request with new token
      return new Promise((resolve, reject) => {
        this.failedQueue.push({ resolve, reject });
      }).then(token => {
        headers['Authorization'] = `Bearer ${token}`;
        return fetch(url, { ...options, headers });
      });
    }

    return response;
  }

  // Clear all stored tokens
  async clearTokens() {
    try {
      await AsyncStorage.removeItem('accessToken');
      await AsyncStorage.removeItem('refreshToken');
      await AsyncStorage.removeItem('token'); // Old token key
      await AsyncStorage.removeItem('authToken'); // New token key
      console.log('βœ… Tokens cleared');
    } catch (error) {
      console.error('❌ Error clearing tokens:', error);
    }
  }

  // Real authentication with backend API
  async authenticateUser(email, password) {
    try {
      console.log('πŸ” Authenticating user with backend:', email);

      const response = await fetch(`${this.baseURL}/api/auth/login`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ email, password }),
      });

      const data = await response.json();

      if (response.ok && data.success) {
        console.log('βœ… Authentication successful for:', email);

        // Save both tokens
        await this.saveTokens(data.accessToken, data.refreshToken);

        // Save user data
        await AsyncStorage.setItem('userData', JSON.stringify(data.user));

        return {
          success: true,
          user: data.user,
          accessToken: data.accessToken,
          refreshToken: data.refreshToken
        };
      } else {
        console.log('❌ Authentication failed:', data.error);
        return {
          success: false,
          error: data.error || 'Authentication failed'
        };
      }
    } catch (error) {
      console.error('❌ Authentication error:', error);
      return {
        success: false,
        error: error.message
      };
    }
  }

  // Real registration with backend API
  async registerUser(userData, userType) {
    try {
      console.log('πŸ“ Registering user with backend:', userData.email, 'Type:', userType);

      const registerData = {
        name: userData.name,
        email: userData.email,
        password: userData.password,
        role: userType === 'central_authority' ? 'authority' : 'trainer'
      };

      const response = await fetch(`${this.baseURL}/api/auth/register`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(registerData),
      });

      const data = await response.json();

      if (response.ok && data.success) {
        console.log('βœ… Registration successful for:', userData.email);

        // Save both tokens
        await this.saveTokens(data.accessToken, data.refreshToken);

        // Save user data
        await AsyncStorage.setItem('userData', JSON.stringify(data.user));

        return {
          success: true,
          user: data.user,
          message: 'Registration successful! You can now login.'
        };
      } else {
        console.log('❌ Registration failed:', data.error);
        return {
          success: false,
          error: data.error || 'Registration failed'
        };
      }
    } catch (error) {
      console.error('❌ Registration error:', error);
      return {
        success: false,
        error: error.message
      };
    }
  }

  // Get user profile
  async getUserProfile(userId, token) {
    try {
      // Simulate API call
      await this.delay(1000);

      // Mock user profile data
      return {
        success: true,
        user: {
          id: userId,
          name: 'Demo User',
          email: 'demo@ndma.gov.in',
          type: 'general',
          verified: true,
          lastLogin: new Date().toISOString()
        }
      };
    } catch (error) {
      return {
        success: false,
        error: error.message
      };
    }
  }

  // Update user profile
  async updateUserProfile(userId, updateData, token) {
    try {
      await this.delay(1500);

      console.log('Updating user profile:', userId, updateData);

      return {
        success: true,
        message: 'Profile updated successfully'
      };
    } catch (error) {
      return {
        success: false,
        error: error.message
      };
    }
  }

  // Reset password
  async resetPassword(email) {
    try {
      await this.delay(2000);

      console.log('Password reset requested for:', email);

      return {
        success: true,
        message: 'Password reset instructions sent to your email'
      };
    } catch (error) {
      return {
        success: false,
        error: error.message
      };
    }
  }

  // Logout
  async logout() {
    try {
      await this.clearTokens();
      await AsyncStorage.removeItem('userData');
      await AsyncStorage.removeItem('user'); // New user key
      await AsyncStorage.removeItem('@ndma_session_user');
      console.log('βœ… User logged out and storage cleared');
      return {
        success: true,
        message: 'Logged out successfully'
      };
    } catch (error) {
      return {
        success: false,
        error: error.message
      };
    }
  }

  // Get current user from AsyncStorage
  async getCurrentUser() {
    try {
      let userDataString = await AsyncStorage.getItem('userData');
      if (!userDataString) {
        userDataString = await AsyncStorage.getItem('user'); // Fallback for NewAuthService
      }

      if (userDataString) {
        const userData = JSON.parse(userDataString);
        console.log('βœ… Retrieved current user:', userData.email || userData.phone);
        return userData;
      }
      console.log('❌ No user data found in storage');
      return null;
    } catch (error) {
      console.error('❌ Error getting current user:', error);
      throw error;
    }
  }

  // Update user profile
  async updateProfile(updateData) {
    try {
      let userDataString = await AsyncStorage.getItem('userData');
      let key = 'userData';

      if (!userDataString) {
        userDataString = await AsyncStorage.getItem('user');
        key = 'user';
      }

      if (userDataString) {
        const userData = JSON.parse(userDataString);
        const updatedUser = { ...userData, ...updateData };
        await AsyncStorage.setItem(key, JSON.stringify(updatedUser));
        console.log('βœ… Profile updated successfully');
        return updatedUser;
      }
      throw new Error('No user data found');
    } catch (error) {
      console.error('❌ Error updating profile:', error);
      throw error;
    }
  }

  // Upload profile picture (mock implementation)
  async uploadProfilePicture(imageUri) {
    try {
      // In real implementation, this would upload to a server
      console.log('πŸ“Έ Uploading profile picture:', imageUri);
      await this.delay(1500);

      // Return the image URI as the "uploaded" URL
      return imageUri;
    } catch (error) {
      console.error('❌ Error uploading profile picture:', error);
      throw error;
    }
  }

  // Delay helper for simulating async operations
  delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  // Helper methods

  // Validate email format
  validateEmail(email) {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
  }

  // Validate password strength
  validatePassword(password) {
    if (password.length < 6) {
      return { valid: false, message: 'Password must be at least 6 characters long' };
    }
    return { valid: true };
  }

  // Get test credentials
  getTestCredentials() {
    return {
      email: 'test@example.com',
      password: 'password123',
      description: 'Test account for development'
    };
  }
}

// Export singleton instance
const authService = new AuthService();

// Export as both default and named export for compatibility
export default authService;
export { authService };