import { apiEndpoints } from "@/lib/api/endpoints"; import { fetchWithAuth } from "@/lib/auth/client"; import type { ApiComment, ApiPost, ApiUser, CreateSuperAdminCasePayload, ModerationStatus, SuperAdminCase, SuperAdminCasesResponse, SuperAdminChartsResponse, SuperAdminOpsResponse, SuperAdminOverviewResponse, SuperAdminRecentActivityResponse, SuperAdminReportsResponse, SuperAdminSessionResponse, SuperAdminSettings, SuperAdminSettingsHistoryResponse, SuperAdminSettingsResponse, } from "@/types/api"; export async function getSuperAdminSession() { return fetchWithAuth(apiEndpoints.superadmin.session); } export async function getSuperAdminOverview() { return fetchWithAuth(apiEndpoints.superadmin.overview); } export async function getSuperAdminCharts( params: Record = {}, ) { return fetchWithAuth(apiEndpoints.superadmin.charts(params)); } export async function getSuperAdminRecentActivity( params: Record = {}, ) { return fetchWithAuth(apiEndpoints.superadmin.recentActivity(params)); } export async function getSuperAdminReports( params: Record = {}, ) { return fetchWithAuth(apiEndpoints.superadmin.reports(params)); } export async function getSuperAdminOps() { return fetchWithAuth(apiEndpoints.superadmin.ops); } export async function getSuperAdminCases( params: Record = {}, ) { return fetchWithAuth(apiEndpoints.superadmin.cases(params)); } export async function createSuperAdminCase(payload: CreateSuperAdminCasePayload) { return fetchWithAuth(apiEndpoints.superadmin.createCase, { method: "POST", body: JSON.stringify(payload), }); } export async function updateSuperAdminCase( caseId: string, payload: Partial & { note?: string }, ) { return fetchWithAuth(apiEndpoints.superadmin.caseById(caseId), { method: "PATCH", body: JSON.stringify(payload), }); } export async function performSuperAdminBulkAction(payload: { resourceType: string; targetIds: string[]; action: string; reason?: string; priority?: string; assignToMe?: boolean; }) { return fetchWithAuth>(apiEndpoints.superadmin.bulkActions, { method: "POST", body: JSON.stringify(payload), }); } export async function getSuperAdminSettings() { return fetchWithAuth(apiEndpoints.superadmin.settings); } export async function getSuperAdminSettingsHistory( params: Record = {}, ) { return fetchWithAuth( apiEndpoints.superadmin.settingsHistory(params), ); } export async function updateSuperAdminSettings(payload: Partial) { return fetchWithAuth(apiEndpoints.superadmin.settings, { method: "PATCH", body: JSON.stringify(payload), }); } export async function restoreSuperAdminSettingsHistory(historyId: string) { return fetchWithAuth( apiEndpoints.superadmin.restoreSettingsHistory(historyId), { method: "POST", }, ); } export async function updateSuperAdminPostStatus(postId: string, status: ModerationStatus, reason?: string) { return fetchWithAuth(apiEndpoints.superadmin.updatePostStatus(postId), { method: "PATCH", body: JSON.stringify({ status, reason }), }); } export async function deleteSuperAdminPost(postId: string) { return fetchWithAuth>(apiEndpoints.superadmin.deletePost(postId), { method: "DELETE", }); } export async function restoreSuperAdminPost(postId: string) { return fetchWithAuth(apiEndpoints.superadmin.restorePost(postId), { method: "POST", }); } export async function updateSuperAdminCommentStatus( commentId: string, status: ModerationStatus, reason?: string, ) { return fetchWithAuth(apiEndpoints.superadmin.updateCommentStatus(commentId), { method: "PATCH", body: JSON.stringify({ status, reason }), }); } export async function deleteSuperAdminComment(commentId: string) { return fetchWithAuth>(apiEndpoints.superadmin.deleteComment(commentId), { method: "DELETE", }); } export async function restoreSuperAdminComment(commentId: string) { return fetchWithAuth(apiEndpoints.superadmin.restoreComment(commentId), { method: "POST", }); } export async function updateSuperAdminUserStatus(userId: string, isDisabled: boolean, reason?: string) { try { return await fetchWithAuth(apiEndpoints.superadmin.updateUserStatus(userId), { method: "PATCH", body: JSON.stringify({ isDisabled, reason }), }); } catch (error) { const message = String(error); if (!message.includes("404")) { throw error; } if (isDisabled) { return fetchWithAuth(apiEndpoints.users.disable(userId), { method: "PATCH", body: JSON.stringify({ reason: reason ?? "Disabled by SuperAdmin dashboard" }), }); } return fetchWithAuth(apiEndpoints.users.enable(userId), { method: "PATCH", }); } }