import { create } from 'zustand';

export type NotificationType = 'success' | 'error' | 'warning' | 'info';

export interface Notification {
  id: string;
  type: NotificationType;
  title: string;
  message?: string;
  duration?: number;
  dismissible?: boolean;
}

interface NotificationState {
  // State
  notifications: Notification[];

  // Actions
  addNotification: (notification: Omit<Notification, 'id'>) => void;
  removeNotification: (id: string) => void;
  clearNotifications: () => void;

  // Helpers
  success: (title: string, message?: string) => void;
  error: (title: string, message?: string) => void;
  warning: (title: string, message?: string) => void;
  info: (title: string, message?: string) => void;
}

const generateId = () => Math.random().toString(36).substring(2, 9);

const DEFAULT_DURATION = 5000;

export const useNotificationStore = create<NotificationState>((set, get) => ({
  // Initial state
  notifications: [],

  // Actions
  addNotification: (notification) => {
    const id = generateId();
    const newNotification: Notification = {
      id,
      duration: DEFAULT_DURATION,
      dismissible: true,
      ...notification,
    };

    set((state) => ({
      notifications: [...state.notifications, newNotification],
    }));

    // Auto-remove after duration
    if (newNotification.duration && newNotification.duration > 0) {
      setTimeout(() => {
        get().removeNotification(id);
      }, newNotification.duration);
    }
  },

  removeNotification: (id) => {
    set((state) => ({
      notifications: state.notifications.filter((n) => n.id !== id),
    }));
  },

  clearNotifications: () => {
    set({ notifications: [] });
  },

  // Helpers
  success: (title, message) => {
    get().addNotification({ type: 'success', title, message });
  },

  error: (title, message) => {
    get().addNotification({ type: 'error', title, message, duration: 8000 });
  },

  warning: (title, message) => {
    get().addNotification({ type: 'warning', title, message });
  },

  info: (title, message) => {
    get().addNotification({ type: 'info', title, message });
  },
}));
