import api from './api';

export interface AppNotification {
  id: string;
  userId: string;
  type: 'connection' | 'message' | 'system' | 'update' | 'request';
  title: string;
  description: string;
  read: boolean;
  actionUrl?: string;
  data?: Record<string, string>;
  createdAt: string;
}

interface NotificationsResponse {
  notifications: AppNotification[];
  unreadCount: number;
  pagination: {
    page: number;
    limit: number;
    total: number;
    pages: number;
  };
}

class NotificationsApiService {
  async getNotifications(
    filter: 'all' | 'unread' = 'all',
    page = 1,
    limit = 20,
  ): Promise<NotificationsResponse> {
    const response = await api.get('/notifications', {
      params: { filter, page, limit },
    });
    return response.data?.data || response.data;
  }

  async getUnreadCount(): Promise<number> {
    const response = await api.get('/notifications/unread-count');
    const data = response.data?.data || response.data;
    return data.unreadCount;
  }

  async markAsRead(notificationId: string): Promise<void> {
    await api.patch(`/notifications/${notificationId}/read`);
  }

  async markAllAsRead(): Promise<void> {
    await api.patch('/notifications/read-all');
  }
}

export const notificationsApiService = new NotificationsApiService();
