import { apiRequest } from "./client";
import { normalizeMutualAid } from "../utils/normalizers";

export type MutualAidItem = {
  id: number;
  title?: string;
  content?: string;
  description?: string;
  tags?: string[];
  tagsText?: string;
  tags_text?: string;
  publisherTitle?: string;
  publisher_title?: string;
  author?: string;
  publisherName?: string;
  publisher_name?: string;
  role?: string;
  branchName?: string;
  branch_name?: string;
  status?: string;
  statusText?: string;
  status_text?: string;
  createdAt?: string;
  publisher_id?: number;
  contactVisible?: boolean;
  contact_visible?: boolean;
  contactPhone?: string;
  contact_phone?: string;
  contactWechat?: string;
  contact_wechat?: string;
  contactRequestStatus?: string;
  contact_request_status?: string;
  contactRequests?: MutualAidContactRequest[];
};

export type MutualAidContactRequest = {
  id: number;
  requester_name?: string;
  requesterName?: string;
  requester_phone?: string;
  requesterPhone?: string;
  message?: string;
  status?: string;
  decision_reason?: string;
  decisionReason?: string;
  created_at?: string;
  createdAt?: string;
};

type MutualAidListResponse =
  | MutualAidItem[]
  | {
      publicPosts?: MutualAidItem[];
      myPendingPosts?: MutualAidItem[];
    };

export async function getMutualAidPosts(type: "resource" | "need") {
  const data = (await apiRequest(`/api/mutual-aid-posts?type=${type}`)) as MutualAidListResponse;
  if (Array.isArray(data)) {
    return { publicPosts: data.map(normalizeMutualAid) as unknown as MutualAidItem[], myPendingPosts: [] as MutualAidItem[] };
  }
  return {
    publicPosts: (Array.isArray(data?.publicPosts) ? data.publicPosts : []).map(normalizeMutualAid) as unknown as MutualAidItem[],
    myPendingPosts: (Array.isArray(data?.myPendingPosts) ? data.myPendingPosts : []).map(normalizeMutualAid) as unknown as MutualAidItem[]
  };
}

export async function getMutualAidDetail(id: number) {
  return normalizeMutualAid(await apiRequest(`/api/mutual-aid-posts/${id}`)) as unknown as MutualAidItem & {
    contactVisible?: boolean;
    contact?: string;
  };
}

export type CreateMutualAidPayload = {
  postType: "resource" | "need";
  title: string;
  description?: string;
  branchId: number;
  contactPhone?: string;
  contactWechat?: string;
  tagsText?: string;
  publisherTitle?: string;
};

export function createMutualAidPost(data: CreateMutualAidPayload) {
  return apiRequest("/api/mutual-aid-posts", {
    method: "POST",
    data
  });
}

export function updateMutualAidPost(id: number, data: CreateMutualAidPayload) {
  return apiRequest(`/api/mutual-aid-posts/${id}`, {
    method: "PUT",
    data
  });
}

export function resubmitMutualAidPost(id: number) {
  return apiRequest(`/api/mutual-aid-posts/${id}/resubmit`, {
    method: "POST"
  });
}

export function requestMutualAidContact(id: number, message?: string) {
  return apiRequest(`/api/mutual-aid-posts/${id}/contact-request`, {
    method: "POST",
    data: { message: message || "" }
  });
}

export function acceptMutualAidContactRequest(requestId: number) {
  return apiRequest(`/api/mutual-aid-posts/contact-requests/${requestId}/accept`, {
    method: "POST"
  });
}

export function rejectMutualAidContactRequest(requestId: number, reason?: string) {
  return apiRequest(`/api/mutual-aid-posts/contact-requests/${requestId}/reject`, {
    method: "POST",
    data: { reason: reason || "" }
  });
}
