import type {
  Notification,
  NotificationUnreadCount,
} from "@/domain/entities/notification";
import type { NotificationRepository } from "@/domain/repositories/notification.repository";
import type { BackendPaginatedData } from "@/shared/types";
import { api } from "@/infrastructure/api/api-client";

export class NotificationRepositoryImpl implements NotificationRepository {
  getUnreadCount() {
    return api.get<NotificationUnreadCount>("/notifications/unread-count");
  }

  async getRecent() {
    // The list endpoint may return a paginated envelope or a flat array.
    const res = await api.get<Notification[] | BackendPaginatedData<Notification>>(
      "/notifications",
      { params: { per_page: 20 } },
    );
    return Array.isArray(res) ? res : res.items;
  }

  markRead(id: number) {
    return api.post<void>(`/notifications/${id}/read`);
  }

  markAllRead() {
    return api.post<void>("/notifications/mark-all-read");
  }
}
