import { api } from './api';
import { API_ENDPOINTS } from '@/utils/constants';

export interface NewsletterSubscribeData {
  email: string;
}

export interface ApiResponse<T = any> {
  success: boolean;
  message?: string;
  data?: T;
  count?: number;
}

export const newsletterService = {
  // Subscribe to newsletter
  subscribe: async (data: NewsletterSubscribeData): Promise<ApiResponse> => {
    // Newsletter subscription doesn't require authentication
    const response = await api.post<ApiResponse>(
      API_ENDPOINTS.NEWSLETTER.SUBSCRIBE,
      data,
      { token: '' } // No token needed for public endpoint
    );
    return response;
  },

  // Unsubscribe from newsletter
  unsubscribe: async (email: string): Promise<ApiResponse> => {
    const response = await api.post<ApiResponse>(
      API_ENDPOINTS.NEWSLETTER.UNSUBSCRIBE,
      { email },
      { token: '' } // No token needed for public endpoint
    );
    return response;
  },

  // Get all subscribers (Admin only)
  getSubscribers: async (): Promise<ApiResponse> => {
    const response = await api.get<ApiResponse>(
      API_ENDPOINTS.NEWSLETTER.SUBSCRIBERS
    );
    return response;
  },

  // Get active subscriber count (Admin only)
  getActiveCount: async (): Promise<number> => {
    const response = await api.get<ApiResponse>(
      API_ENDPOINTS.NEWSLETTER.COUNT
    );
    return response.count || 0;
  },
};
