import { apiRequest } from "./client";
import { arr, normalizeActivity, normalizeContent, normalizeMutualAid, normalizeServiceProject, normalizeSurvey } from "../utils/normalizers";

export function getLearningRecords() {
  return apiRequest("/api/me/learning-records").then((data) => arr(data).map(normalizeContent));
}

export function getMySuggestions() {
  return apiRequest("/api/me/suggestions").then((data) => arr(data).map(normalizeContent));
}

export function getMyCreatedActivities() {
  return apiRequest("/api/me/activities/created").then((data) => arr(data).map(normalizeActivity));
}

export function getMyRegisteredActivities() {
  return apiRequest("/api/me/activities/registered").then((data) => arr(data).map(normalizeActivity));
}

export function getMySurveys() {
  return apiRequest("/api/me/surveys").then((data) => arr(data).map(normalizeSurvey));
}

export function getMyServiceProjects() {
  return apiRequest("/api/me/service-projects").then((data) => arr(data).map(normalizeServiceProject));
}

export function getMyServiceHelpRequests() {
  return apiRequest("/api/me/service-help-requests");
}

export function getMyServiceHelpRequestDetail(id) {
  return apiRequest(`/api/me/service-help-requests/${id}`);
}

export function getMyMutualAidResources() {
  return apiRequest("/api/me/mutual-aid/resources").then((data) => arr(data).map(normalizeMutualAid));
}

export function getMyMutualAidNeeds() {
  return apiRequest("/api/me/mutual-aid/needs").then((data) => arr(data).map(normalizeMutualAid));
}

export function getMyBrowsingHistory(page = 1, pageSize = 20) {
  const query = new URLSearchParams({ page: String(page), pageSize: String(pageSize) });
  return apiRequest(`/api/me/browsing-history?${query.toString()}`).then((data) => arr(data).map(normalizeContent));
}

export function getMyProfile() {
  return apiRequest("/api/me/profile");
}

export function getMyPassport() {
  return apiRequest("/api/me/passport");
}

export function getMyPointTasks() {
  return apiRequest("/api/me/point-tasks");
}

export function getMyPointRecords() {
  return apiRequest("/api/me/point-records");
}

export function getMyPerformanceRecords() {
  return apiRequest("/api/me/performance-records");
}

export function createPerformanceRecord(body: any) {
  return apiRequest("/api/me/performance-records", {
    method: "POST",
    data: body
  });
}

export function getAccountSecurityStatus() {
  return apiRequest("/api/me/security");
}

export function sendAccountPhoneCode(phone: string) {
  return apiRequest("/api/me/security/phone/send-code", {
    method: "POST",
    data: { phone }
  });
}

export function bindAccountPhone(phone: string, code: string) {
  return apiRequest("/api/me/security/phone/bind", {
    method: "POST",
    data: { phone, code }
  });
}

export function bindAccountEmail(email: string) {
  return apiRequest("/api/me/security/email/bind", {
    method: "POST",
    data: { email }
  });
}

export function changeAccountPassword(oldPassword: string, newPassword: string) {
  return apiRequest("/api/me/security/password", {
    method: "POST",
    data: { oldPassword, newPassword }
  });
}
