import { axiosSuccessStatusOnly } from "@/lib/applicationReviewApi";
import { apiClient } from "@/shared/api/client";

export type UserNotificationDecision = "approved" | "rejected";

export interface UserNotificationDto {
  id: string;
  type: string;
  title: string;
  body: string;
  applicationId: string;
  relatedInitiativeId: string | null;
  sourceAdminResultId: string | null;
  decision: UserNotificationDecision;
  meritCategoryLabel: string | null;
  feedback: string;
  rationale: string | null;
  readAt: string | null;
  createdAt: string | null;
}

export interface NotificationsListResponse {
  data: UserNotificationDto[];
  pagination: { page: number; pageSize: number; totalItems: number; totalPages: number };
}

export async function fetchNotifications(page = 1, pageSize = 20): Promise<NotificationsListResponse> {
  return apiClient.get<NotificationsListResponse>("/api/notifications", {
    params: { page, pageSize },
    ...axiosSuccessStatusOnly,
  });
}

export async function fetchNotificationsUnreadCount(): Promise<number> {
  const res = await apiClient.get<{ count: number }>("/api/notifications/unread-count", axiosSuccessStatusOnly);
  return typeof res.count === "number" ? res.count : 0;
}

export async function markNotificationRead(notificationId: string): Promise<void> {
  await apiClient.patch<{ ok: boolean }>(
    `/api/notifications/${encodeURIComponent(notificationId)}/read`,
    undefined,
    axiosSuccessStatusOnly,
  );
}
