import { apiRequest } from "./client";
import { arr, normalizeMember } from "../utils/normalizers";

export function getSocialMembers() {
  return apiRequest("/api/social/members").then((rows) => arr(rows).map(normalizeMember));
}

export function getMemberDetail(userId) {
  return apiRequest(`/api/social/member-cards/${userId}`).then(normalizeMember);
}

export function getFriendInbox(status = "") {
  const suffix = status ? `?status=${encodeURIComponent(status)}` : "";
  return apiRequest(`/api/social/friendships/inbox${suffix}`);
}

export function getFriendOutbox(status = "") {
  const suffix = status ? `?status=${encodeURIComponent(status)}` : "";
  return apiRequest(`/api/social/friendships/outbox${suffix}`);
}

export function acceptFriend(id) {
  return apiRequest(`/api/social/friendships/${id}/accept`, { method: "POST" });
}

export function rejectFriend(id) {
  return apiRequest(`/api/social/friendships/${id}/reject`, { method: "POST" });
}

export function cancelFriendRequest(id) {
  return apiRequest(`/api/social/friendships/${id}/cancel`, { method: "POST" });
}

export function requestFriend(targetUserId, message = "") {
  return apiRequest("/api/social/friendships", {
    method: "POST",
    data: { targetUserId, message }
  });
}

export function deleteFriend(targetUserId) {
  return apiRequest(`/api/social/friends/${targetUserId}`, { method: "DELETE" });
}

export function restoreFriend(targetUserId) {
  return apiRequest(`/api/social/friends/${targetUserId}/restore`, { method: "POST" });
}

export function saveFriendNote(targetUserId, data) {
  return apiRequest(`/api/social/friends/${targetUserId}/note`, {
    method: "PUT",
    data
  });
}

export function getFriendGroups() {
  return apiRequest("/api/social/friend-groups");
}

export function createFriendGroup(data) {
  return apiRequest("/api/social/friend-groups", {
    method: "POST",
    data
  });
}

export function updateFriendGroup(id, data) {
  return apiRequest(`/api/social/friend-groups/${id}`, {
    method: "PUT",
    data
  });
}

export function deleteFriendGroup(id) {
  return apiRequest(`/api/social/friend-groups/${id}`, { method: "DELETE" });
}

export function getBlockedUsers() {
  return apiRequest("/api/social/blocks").then((rows) => arr(rows).map(normalizeMember));
}

export function blockUser(targetUserId, reason = "") {
  return apiRequest("/api/social/blocks", {
    method: "POST",
    data: { targetUserId, reason }
  });
}

export function unblockUser(targetUserId) {
  return apiRequest(`/api/social/blocks/${targetUserId}`, { method: "DELETE" });
}

export function submitReport(data) {
  return apiRequest("/api/reports", {
    method: "POST",
    data
  });
}

export function getPassportCard(passportId) {
  return apiRequest(`/api/social/passports/${encodeURIComponent(String(passportId))}`);
}

export function addFriendByPassport(passportId) {
  return apiRequest(`/api/social/passports/${encodeURIComponent(String(passportId))}/friendships`, { method: "POST" });
}

export function getSocialProfile(userId) {
  return apiRequest(`/api/social/profiles/${userId}`).then(normalizeMember);
}

export function saveMySocialProfile(payload) {
  return apiRequest("/api/social/profiles/me", {
    method: "PUT",
    data: payload
  });
}
