import apiClient from '../../../shared/services/apiClient';

export interface Notification {
  id: number;
  user_id?: number;
  notification_type: 'booking_confirmation' | 'payment_receipt' | 'pre_arrival_reminder' | 'check_in_reminder' | 'check_out_reminder' | 'marketing_campaign' | 'loyalty_update' | 'system_alert' | 'custom';
  channel: 'email' | 'sms' | 'push' | 'whatsapp' | 'in_app';
  subject?: string;
  content: string;
  status: 'pending' | 'sent' | 'delivered' | 'failed' | 'read';
  priority: string;
  sent_at?: string;
  delivered_at?: string;
  read_at?: string;
  created_at: string;
}

export interface NotificationTemplate {
  id: number;
  name: string;
  notification_type: string;
  channel: string;
  subject?: string;
  content: string;
  variables?: string[];
}

export interface NotificationPreferences {
  email_enabled: boolean;
  sms_enabled: boolean;
  push_enabled: boolean;
  whatsapp_enabled: boolean;
  in_app_enabled: boolean;
  booking_confirmation_email: boolean;
  booking_confirmation_sms: boolean;
  payment_receipt_email: boolean;
  payment_receipt_sms: boolean;
  pre_arrival_reminder_email: boolean;
  pre_arrival_reminder_sms: boolean;
  check_in_reminder_email: boolean;
  check_in_reminder_sms: boolean;
  check_out_reminder_email: boolean;
  check_out_reminder_sms: boolean;
  marketing_campaign_email: boolean;
  marketing_campaign_sms: boolean;
  loyalty_update_email: boolean;
  loyalty_update_sms: boolean;
  system_alert_email: boolean;
  system_alert_push: boolean;
}

export interface SendNotificationRequest {
  user_id?: number;
  notification_type: string;
  channel: string;
  content: string;
  subject?: string;
  template_id?: number;
  priority?: string;
  scheduled_at?: string;
  booking_id?: number;
  payment_id?: number;
  meta_data?: Record<string, unknown>;
}

const notificationService = {
  // Notifications
  getNotifications: async (params?: {
    user_id?: number;
    notification_type?: string;
    channel?: string;
    status?: string;
    skip?: number;
    limit?: number;
  }) => {
    return apiClient.get<{ status: string; data: Notification[] }>('/notifications/', { params });
  },

  getMyNotifications: async (params?: {
    status?: string;
    skip?: number;
    limit?: number;
  }) => {
    // Tokens are in httpOnly cookies, so authentication is handled automatically
    // No need to check localStorage for tokens
    return apiClient.get<{ status: string; data: Notification[] }>('/notifications/my-notifications', { params });
  },

  sendNotification: async (data: SendNotificationRequest) => {
    return apiClient.post<{ status: string; data: Notification }>('/notifications/send', data);
  },

  markAsRead: async (notificationId: number) => {
    return apiClient.post<{ status: string; data: Notification }>(`/notifications/${notificationId}/read`);
  },

  // Templates
  getTemplates: async (params?: {
    notification_type?: string;
    channel?: string;
  }) => {
    return apiClient.get<{ status: string; data: NotificationTemplate[] }>('/notifications/templates', { params });
  },

  createTemplate: async (data: {
    name: string;
    notification_type: string;
    channel: string;
    content: string;
    subject?: string;
    variables?: string[];
  }) => {
    return apiClient.post<{ status: string; data: NotificationTemplate }>('/notifications/templates', data);
  },

  // Preferences
  getPreferences: async () => {
    return apiClient.get<{ status: string; data: NotificationPreferences }>('/notifications/preferences');
  },

  updatePreferences: async (data: Partial<NotificationPreferences>) => {
    return apiClient.put<{ status: string; data: NotificationPreferences }>('/notifications/preferences', data);
  },
};

export default notificationService;

