import { api } from "@/lib/api-client";
import type {
  Notification,
  NotificationRule,
  NotificationPreferences,
  NotificationRuleCreateRequest,
} from "@/types/notification";
import type { ApiSuccessResponse, PaginatedResponse, PaginationParams } from "@/types";

export const notificationService = {
  async listNotifications(
    params?: PaginationParams & { type?: string; is_read?: boolean },
  ): Promise<PaginatedResponse<Notification>> {
    return api.get<PaginatedResponse<Notification>>(
      "/notifications",
      params as Record<string, string | number | boolean | undefined>,
    );
  },

  async markNotificationRead(id: number): Promise<Notification> {
    return api.post<Notification>(`/notifications/${id}/read`);
  },

  async markAllNotificationsRead(): Promise<ApiSuccessResponse> {
    return api.post<ApiSuccessResponse>("/notifications/read-all");
  },

  async getUnreadNotificationCount(): Promise<{ count: number }> {
    return api.get<{ count: number }>("/notifications/unread-count");
  },

  async listNotificationRules(): Promise<NotificationRule[]> {
    return api.get<NotificationRule[]>("/notification-rules");
  },

  async createNotificationRule(data: NotificationRuleCreateRequest): Promise<NotificationRule> {
    return api.post<NotificationRule>("/notification-rules", data);
  },

  async updateNotificationRule(id: number, data: Partial<NotificationRuleCreateRequest>): Promise<NotificationRule> {
    return api.patch<NotificationRule>(`/notification-rules/${id}`, data);
  },

  async deleteNotificationRule(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/notification-rules/${id}`);
  },

  async getNotificationPreferences(): Promise<NotificationPreferences> {
    return api.get<NotificationPreferences>("/notification-preferences");
  },

  async updateNotificationPreferences(data: Partial<NotificationPreferences>): Promise<NotificationPreferences> {
    return api.patch<NotificationPreferences>("/notification-preferences", data);
  },
};
