import { apiEndpoints } from "@/lib/api/endpoints";
import { fetchWithAuth } from "@/lib/auth/client";
import type {
  CollaborationRequestItem,
  CollaborationRequestsResponse,
  DeviceItem,
  FollowListResponse,
  FollowStatusResponse,
  NotificationsResponse,
  PostsResponse,
  PresenceResponse,
  RegisterDevicePayload,
  SuccessMessage,
  UnregisterDevicePayload,
} from "@/types/api";

export async function getUserPresence(userId: string) {
  return fetchWithAuth<PresenceResponse>(apiEndpoints.users.publicPresence(userId));
}

export async function listUserPosts(
  userId: string,
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<PostsResponse>(apiEndpoints.users.publicPosts(userId, params));
}

export async function followUser(userId: string) {
  return fetchWithAuth<FollowStatusResponse>(apiEndpoints.users.follow(userId), {
    method: "POST",
    body: JSON.stringify({}),
  });
}

export async function unfollowUser(userId: string) {
  return fetchWithAuth<FollowStatusResponse>(apiEndpoints.users.unfollow(userId), {
    method: "DELETE",
  });
}

export async function getFollowStatus(userId: string) {
  return fetchWithAuth<FollowStatusResponse>(apiEndpoints.users.followStatus(userId));
}

export async function listUserFollowers(
  userId: string,
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<FollowListResponse>(apiEndpoints.users.followers(userId, params));
}

export async function listUserFollowing(
  userId: string,
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<FollowListResponse>(apiEndpoints.users.following(userId, params));
}

export async function registerDevice(payload: RegisterDevicePayload) {
  return fetchWithAuth<DeviceItem>(apiEndpoints.devices.register, {
    method: "POST",
    body: JSON.stringify(payload),
  });
}

export async function unregisterDevice(payload: UnregisterDevicePayload) {
  return fetchWithAuth<SuccessMessage>(apiEndpoints.devices.unregister, {
    method: "POST",
    body: JSON.stringify(payload),
  });
}

export async function createCollaborationRequest(postId: string, targetUserId: string) {
  return fetchWithAuth<CollaborationRequestItem>(apiEndpoints.collaborationRequests.create(postId), {
    method: "POST",
    body: JSON.stringify({ targetUserId }),
  });
}

export async function listCollaborationRequests(
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<CollaborationRequestsResponse>(apiEndpoints.collaborationRequests.list(params));
}

export async function approveCollaborationRequest(requestId: string) {
  return fetchWithAuth<CollaborationRequestItem>(apiEndpoints.collaborationRequests.approve(requestId), {
    method: "PATCH",
    body: JSON.stringify({}),
  });
}

export async function rejectCollaborationRequest(requestId: string) {
  return fetchWithAuth<CollaborationRequestItem>(apiEndpoints.collaborationRequests.reject(requestId), {
    method: "PATCH",
    body: JSON.stringify({}),
  });
}

export async function listMyNotifications(
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<NotificationsResponse>(apiEndpoints.notifications.mine(params));
}

export async function getMyUnreadNotificationCount() {
  return fetchWithAuth<{ unreadCount: number }>(apiEndpoints.notifications.unreadCount);
}

export async function markMyNotificationRead(notificationId: string) {
  return fetchWithAuth<SuccessMessage>(apiEndpoints.notifications.markRead(notificationId), {
    method: "PATCH",
    body: JSON.stringify({}),
  });
}

export async function markAllMyNotificationsRead() {
  return fetchWithAuth<SuccessMessage>(apiEndpoints.notifications.markAllRead, {
    method: "PATCH",
    body: JSON.stringify({}),
  });
}

export type MobileSocialRouteGroup = {
  title: string;
  routes: string[];
};

export const mobileSocialRouteGroups: MobileSocialRouteGroup[] = [
  {
    title: "users",
    routes: [
      "GET /users/:userId",
      "GET /users/:userId/presence",
      "GET /users/:userId/posts",
      "GET /users/:userId/profile-overview",
    ],
  },
  {
    title: "follows",
    routes: [
      "POST /users/:userId/follow",
      "DELETE /users/:userId/follow",
      "GET /users/:userId/follow-status",
      "GET /users/:userId/followers",
      "GET /users/:userId/following",
      "GET /users/me/followers",
      "GET /users/me/following",
    ],
  },
  {
    title: "devices",
    routes: ["POST /devices/register", "POST /devices/unregister"],
  },
  {
    title: "collaborationRequests",
    routes: [
      "POST /posts/:postId/collaboration-requests",
      "GET /collaboration-requests",
      "PATCH /collaboration-requests/:requestId/approve",
      "PATCH /collaboration-requests/:requestId/reject",
    ],
  },
  {
    title: "notifications",
    routes: [
      "GET /notifications",
      "GET /notifications/unread-count",
      "PATCH /notifications/:notificationId/read",
      "PATCH /notifications/read-all",
    ],
  },
];
