import api from "src/api";

// ─── Rotas do Associado ───────────────────────────────────────────────────────

export const getMyNotificationsAssociado = async () => {
  const { data } = await api.get("/associado/notification/my");
  return data.payload;
};

export const getMyUnreadNotificationsAssociado = async () => {
  const { data } = await api.get("/associado/notification/my/unread");
  return data.payload;
};

export const markNotificationAsReadAssociado = async (sendId) => {
  const { data } = await api.patch(`/associado/notification/${sendId}/read`);
  return data.payload;
};

// ─── Rotas do Parceiro ────────────────────────────────────────────────────────

export const getMyNotificationsParceiro = async () => {
  const { data } = await api.get("/parceiro/notification/my");
  return data.payload;
};

export const getMyUnreadNotificationsParceiro = async () => {
  const { data } = await api.get("/parceiro/notification/my/unread");
  return data.payload;
};

export const markNotificationAsReadParceiro = async (sendId) => {
  const { data } = await api.patch(`/parceiro/notification/${sendId}/read`);
  return data.payload;
};

// ─── Rotas Admin ──────────────────────────────────────────────────────────────

export const getMyUnreadNotifications = async () => {
  const { data } = await api.get("/notification/my/unread");
  return data.payload;
};

export const getMyNotifications = async () => {
  const { data } = await api.get("/notification/my");
  return data.payload;
};

export const markNotificationAsRead = async (sendId) => {
  const { data } = await api.patch(`/notification/${sendId}/read`);
  return data.payload;
};

export const getNotificationsPaginated = async ({ page = 1, perPage = 12 } = {}) => {
  const { data } = await api.get("/notification/paginated", {
    params: { page, per_page: perPage },
  });
  return data.payload;
};

export const sendNotification = async (payload) => {
  const form = new FormData();
  form.append("title", payload.title);
  form.append("message", payload.message);
  form.append("recipient", payload.recipient);

  if (payload.recipient_position_id != null) {
    form.append("recipient_position_id", payload.recipient_position_id);
  }
  if (payload.recipient_sector_id != null) {
    form.append("recipient_sector_id", payload.recipient_sector_id);
  }
  if (payload.image instanceof File) {
    form.append("image", payload.image);
  }

  const { data } = await api.post("/notification", form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data.payload;
};
