File size: 11,569 Bytes
eb6a2f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ─────────────────────────────────────────────────────────────────────────────
//  src/lib/api/adminApi.ts
// ─────────────────────────────────────────────────────────────────────────────

import { getApiBaseUrl } from './config';

const BASE_URL = getApiBaseUrl();
const TOKEN_KEY = 'authToken';

// ─── Custom Error Class ───────────────────────────────────────────────────────

export class ApiError extends Error {
  readonly status: number;
  readonly path: string;

  constructor(
    status: number,
    message: string,
    path: string,
  ) {
    super(`[${status}] ${path} β†’ ${message}`);
    this.status = status;
    this.path = path;
    this.name = 'ApiError';
  }
}

// ─── Shared helper ────────────────────────────────────────────────────────────

function getHeaders(): HeadersInit {
  const token = localStorage.getItem(TOKEN_KEY) ?? '';
  return {
    'Content-Type': 'application/json',
    ...(token ? { Authorization: `Bearer ${token}` } : {}),
  };
}

async function request<T>(path: string, options?: RequestInit): Promise<T | null> {
  const url = `${BASE_URL}${path}`;

  // Guard: expired token
  const token = localStorage.getItem(TOKEN_KEY);
  if (token) {
    try {
      const payload = JSON.parse(atob(token.split('.')[1]));
      if (payload?.exp && payload.exp * 1000 < Date.now()) {
        localStorage.removeItem(TOKEN_KEY);
        throw new ApiError(401, 'Session expired β€” please log in again', path);
      }
    } catch (e) {
      if (e instanceof ApiError) throw e;
      localStorage.removeItem(TOKEN_KEY);
    }
  }

  try {
    const res = await fetch(url, {
      ...options,
      headers: { ...getHeaders(), ...options?.headers },
    });

    if (res.status === 204) return null;

    if (!res.ok) {
      const message = await res.text().catch(() => res.statusText);
      throw new ApiError(res.status, message, path);
    }

    const text = await res.text();
    if (!text.trim()) return null;

    try {
      return JSON.parse(text) as T;
    } catch {
      return text as unknown as T;
    }
  } catch (error) {
    if (error instanceof ApiError) throw error;
    throw new Error(`Network error while calling ${path}`);
  }
}

// ─── Response Types ───────────────────────────────────────────────────────────

export interface PendingSheikhDto {
  id: number;
  name: string;
  County: string;   // API returns capital-C "County"
  specialization: string;
  experienceYears: number;
}

/**
 * Matches actual API response from:
 * GET /api/admin/dashboard/platform-analytics-and-stats
 */
export interface PlatformAnalyticsDto {
  totalUsers: number;
  usersThisMonth: number;
  pendingSheikhs: number;
  ongoingSessions: number;
  revenueThisMonth: number;
  totalRevenue: number;
  totalStudents: number;
  totalSheikhs: number;
  growth: number;
}

export interface ApiUser {
  id: number;
  fullName: string;
  email: string;
  role: 'STUDENT' | 'SHEIKH' | 'ADMIN';
  status: 'ACTIVE' | 'BLOCKED' | 'PENDING';
  createdAt: string;
  totalSessions?: number;
  rating?: number;
  earnings?: number;
  streak?: number;
}

export interface PaginatedResponse<T> {
  content: T[];
  totalElements: number;
  totalPages: number;
  size: number;
  number: number;
}

// ─── API Functions ────────────────────────────────────────────────────────────

/**
 * GET /api/admin/sheikh-approval?status=PENDING  (or UNDER_REVIEW, etc.)
 */
export interface SheikhApprovalDto {
  id: number;
  name: string;
  email: string;
  country: string;
  experienceYears: number;
  description: string;
  specializations: string;
  pricePerHour: number;
  statusOfSheikh: 'PENDING' | 'UNDER_REVIEW' | 'APPROVED' | 'REJECTED';
  interviewDateTime: string | null; // ISO: "2026-05-25T14:00:00", null if not scheduled
}

export async function fetchSheikhsByStatus(
  status: 'PENDING' | 'UNDER_REVIEW' | 'APPROVED' | 'REJECTED',
): Promise<SheikhApprovalDto[]> {
  const res = await request<SheikhApprovalDto[]>(
    `/api/admin/sheikh-approval?status=${status}`,
  );
  return Array.isArray(res) ? res : [];
}


/**
 * GET /api/admin/dashboard/platform-analytics-and-stats
 */
export async function fetchPlatformAnalytics(): Promise<PlatformAnalyticsDto> {
  const empty: PlatformAnalyticsDto = {
    totalUsers: 0, usersThisMonth: 0, pendingSheikhs: 0,
    ongoingSessions: 0, revenueThisMonth: 0, totalRevenue: 0,
    totalStudents: 0, totalSheikhs: 0, growth: 0,
  };
  try {
    const res = await request<PlatformAnalyticsDto>(
      '/api/admin/dashboard/platform-analytics-and-stats',
    );
    return res ?? empty;
  } catch (error) {
    if (error instanceof ApiError && error.status === 403) {
      throw new ApiError(403, 'Admin access required.', '/api/admin/dashboard/platform-analytics-and-stats');
    }
    return empty;
  }
}

/**
 * GET /api/admin/users  (may return 403 if endpoint not implemented yet)
 * Falls back to empty paginated response gracefully.
 */
export async function fetchAllUsers(params?: {
  role?: string;
  status?: string;
  page?: number;
  size?: number;
}): Promise<PaginatedResponse<ApiUser>> {
  const empty: PaginatedResponse<ApiUser> = {
    content: [], totalElements: 0, totalPages: 0, size: 0, number: 0,
  };

  const queryParams = new URLSearchParams();
  if (params?.role) queryParams.append('role', params.role);
  if (params?.status) queryParams.append('status', params.status);
  if (params?.page !== undefined) queryParams.append('page', String(params.page));
  if (params?.size !== undefined) queryParams.append('size', String(params.size));
  const query = queryParams.toString() ? `?${queryParams.toString()}` : '';

  try {
    const res = await request<PaginatedResponse<ApiUser>>(`/api/admin/users${query}`);
    if (!res) return empty;
    // Handle both paginated { content: [] } and plain array responses
    if (Array.isArray(res)) {
      return { content: res as ApiUser[], totalElements: (res as ApiUser[]).length, totalPages: 1, size: (res as ApiUser[]).length, number: 0 };
    }
    return res;
  } catch (error) {
    // 403 β†’ endpoint not available for this role/token, return empty silently
    if (error instanceof ApiError && (error.status === 403 || error.status === 404)) {
      return empty;
    }
    throw error;
  }
}

/**
 * POST /api/admin/users/:id/block  or  /api/admin/users/:id/unblock
 */
export async function toggleUserBlock(
  userId: number,
  block: boolean,
): Promise<{ success: boolean; message: string }> {
  const action = block ? 'block' : 'unblock';
  try {
    const res = await request<{ success: boolean; message: string }>(
      `/api/admin/users/${userId}/${action}`,
      { method: 'POST' },
    );
    return res ?? { success: true, message: 'Done' };
  } catch (error) {
    if (error instanceof ApiError) throw error;
    throw new Error(`Failed to ${action} user`);
  }
}

/**
 * DELETE /api/admin/users/:id
 */
export async function deleteUser(
  userId: number,
): Promise<{ success: boolean; message: string }> {
  try {
    const res = await request<{ success: boolean; message: string }>(
      `/api/admin/user-management/users/${userId}`,
      { method: 'DELETE' },
    );
    return res ?? { success: true, message: 'Deleted' };
  } catch (error) {
    if (error instanceof ApiError) throw error;
    throw new Error('Failed to delete user');
  }
}

/**
 * GET /api/admin/dashboard/pending-sheikhs  (used by AdminDashboard)
 */
export async function fetchPendingSheikhs(): Promise<PendingSheikhDto[]> {
  const res = await request<PendingSheikhDto[]>('/api/admin/dashboard/pending-sheikhs');
  return Array.isArray(res) ? res : [];
}

export async function approveSheikh(
  sheikhId: number,
): Promise<void> {
  await request<void>(
    `/api/admin/sheikh-approval/${sheikhId}/approve`,
    { method: 'PATCH' },
  );
}

export async function rejectSheikh(
  sheikhId: number,
  reason: string,
): Promise<void> {
  await request<void>(
    `/api/admin/sheikh-approval/${sheikhId}/reject`,
    { method: 'PATCH', body: JSON.stringify({ rejectionReason: reason }) },
  );
}

/**
 * POST /api/admin/sheikh-approval/:id/schedule-interview
 * Schedules an interview and moves the sheikh status to UNDER_REVIEW.
 */
export async function scheduleInterview(
  sheikhId: number,
  interviewDate: string, // "YYYY-MM-DD"
  interviewTime: string, // "HH:mm"
  optionalMessage?: string,
): Promise<{ success: boolean; message: string }> {
  const res = await request<{ success: boolean; message: string }>(
    `/api/admin/sheikh-approval/${sheikhId}/schedule-interview`,
    { method: 'POST', body: JSON.stringify({ interviewDate, interviewTime, optionalMessage: optionalMessage ?? '' }) },
  );
  return res ?? { success: true, message: 'Interview scheduled' };
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function fetchSheikhDetails(sheikhId: number): Promise<any> {
  try {
    return await request(`/api/admin/sheikhs/${sheikhId}`);
  } catch {
    return null;
  }
}

export async function saveInterviewNotes(
  sheikhId: string | number | undefined,
  notes: string,
): Promise<void> {
  if (!sheikhId) return;
  await request<void>(
    `/api/admin/sheikh-approval/${sheikhId}/notes`,
    { method: 'PATCH', body: JSON.stringify({ notes }) },
  );
}

// ─── Dashboard loader ─────────────────────────────────────────────────────────

export interface AdminDashboardData {
  pendingSheikhs: PendingSheikhDto[];
  platformAnalytics: PlatformAnalyticsDto;
}

export async function fetchAdminDashboardData(): Promise<{
  data: Partial<AdminDashboardData>;
  errors: Record<string, string>;
}> {
  const [sheikhsRes, analyticsRes] = await Promise.allSettled([
    fetchPendingSheikhs(),
    fetchPlatformAnalytics(),
  ]);

  const data: Partial<AdminDashboardData> = {};
  const errors: Record<string, string> = {};

  data.pendingSheikhs =
    sheikhsRes.status === 'fulfilled'
      ? sheikhsRes.value
      : (errors.pendingSheikhs = sheikhsRes.reason?.message ?? 'Error', []);

  data.platformAnalytics =
    analyticsRes.status === 'fulfilled'
      ? analyticsRes.value
      : (errors.platformAnalytics = analyticsRes.reason?.message ?? 'Error',
      {
        totalUsers: 0, usersThisMonth: 0, pendingSheikhs: 0, ongoingSessions: 0,
        revenueThisMonth: 0, totalRevenue: 0, totalStudents: 0, totalSheikhs: 0, growth: 0
      });

  return { data, errors };
}

const adminApi = {
  fetchPendingSheikhs, fetchSheikhsByStatus, fetchPlatformAnalytics, fetchAdminDashboardData,
  approveSheikh, rejectSheikh, scheduleInterview, fetchSheikhDetails, saveInterviewNotes,
  fetchAllUsers, toggleUserBlock, deleteUser,
};

export default adminApi;