/**
 * Notifications API
 *
 * Endpoints for managing user notifications
 */

import { get, post, patch, del } from './client';
import type { NotificationResponse, NotificationListResponse, SuccessResponse } from '@/types/api';

// TODO: These types should be moved to @/types/api.ts
export interface CreateNotificationRequest {
  userId?: string;
  type: string;
  title: string;
  message: string;
}

export interface UpdateNotificationPreferencesRequest {
  emailNotifications?: boolean;
  pushNotifications?: boolean;
  smsNotifications?: boolean;
  notificationTypes?: string[];
}

export interface NotificationPreferencesResponse {
  userId: string;
  emailNotifications: boolean;
  pushNotifications: boolean;
  smsNotifications: boolean;
  notificationTypes: string[];
  updatedAt: string;
}

/**
 * List user notifications with pagination
 * GET /api/v1/notifications?page=1&limit=20&read=false&type=booking_created
 */
export async function listNotifications(params?: {
  page?: number;
  limit?: number;
  isRead?: boolean;
  type?: string;
}): Promise<NotificationListResponse> {
  const queryParams = new URLSearchParams();
  if (params?.page) queryParams.append('page', params.page.toString());
  if (params?.limit) queryParams.append('limit', params.limit.toString());
  if (params?.isRead !== undefined) queryParams.append('read', params.isRead.toString());
  if (params?.type) queryParams.append('type', params.type);

  const queryString = queryParams.toString();
  return get<NotificationListResponse>(
    `/api/v1/notifications${queryString ? `?${queryString}` : ''}`
  );
}

/**
 * Get notification by ID
 * GET /api/v1/notifications/:id
 */
export async function getNotification(id: string): Promise<NotificationResponse> {
  return get<NotificationResponse>(`/api/v1/notifications/${id}`);
}

/**
 * Create notification (admin only)
 * POST /api/v1/notifications
 * Requires: ADMIN role
 */
export async function createNotification(
  data: CreateNotificationRequest
): Promise<NotificationResponse> {
  return post<NotificationResponse>('/api/v1/notifications', data);
}

/**
 * Mark notification as read
 * PATCH /api/v1/notifications/:id/read
 */
export async function markNotificationAsRead(id: string): Promise<SuccessResponse> {
  return patch<SuccessResponse>(`/api/v1/notifications/${id}/read`, {});
}

/**
 * Mark all notifications as read
 * POST /api/v1/notifications/read-all
 */
export async function markAllNotificationsAsRead(): Promise<SuccessResponse> {
  return post<SuccessResponse>('/api/v1/notifications/read-all', {});
}

/**
 * Delete notification
 * DELETE /api/v1/notifications/:id
 */
export async function deleteNotification(id: string): Promise<SuccessResponse> {
  return del<SuccessResponse>(`/api/v1/notifications/${id}`);
}

/**
 * Get notification preferences
 * GET /api/v1/notifications/preferences
 */
export async function getNotificationPreferences(): Promise<NotificationPreferencesResponse> {
  return get<NotificationPreferencesResponse>('/api/v1/notifications/preferences');
}

/**
 * Update notification preferences
 * PATCH /api/v1/notifications/preferences
 */
export async function updateNotificationPreferences(
  data: UpdateNotificationPreferencesRequest
): Promise<NotificationPreferencesResponse> {
  return patch<NotificationPreferencesResponse>('/api/v1/notifications/preferences', data);
}
