/**
 * Clean Event Context for Global State Management
 * 
 * NO BACKWARD COMPATIBILITY - Complete rewrite with i18n parameterized content
 */

import React, { createContext, useContext, useReducer, ReactNode } from 'react';
import { Event, Alert, Notification, Recommendation } from '../api/types/events';

// Action types
type EventAction =
  | { type: 'ADD_EVENT'; payload: Event }
  | { type: 'UPDATE_EVENT'; payload: Event }
  | { type: 'REMOVE_EVENT'; payload: string } // event ID
  | { type: 'SET_EVENTS'; payload: Event[] }
  | { type: 'CLEAR_EVENTS' }
  | { type: 'MARK_AS_READ'; payload: string } // event ID
  | { type: 'ACKNOWLEDGE_EVENT'; payload: string } // event ID
  | { type: 'RESOLVE_EVENT'; payload: string }; // event ID

// State type
interface EventState {
  events: Event[];
  unreadCount: number;
  activeAlerts: Alert[];
  recentNotifications: Notification[];
  recommendations: Recommendation[];
}

// Initial state
const initialState: EventState = {
  events: [],
  unreadCount: 0,
  activeAlerts: [],
  recentNotifications: [],
  recommendations: [],
};

// Reducer
const eventReducer = (state: EventState, action: EventAction): EventState => {
  switch (action.type) {
    case 'SET_EVENTS':
      const alerts = action.payload.filter(e => e.event_class === 'alert') as Alert[];
      const notifications = action.payload.filter(e => e.event_class === 'notification') as Notification[];
      const recommendations = action.payload.filter(e => e.event_class === 'recommendation') as Recommendation[];

      return {
        ...state,
        events: action.payload,
        activeAlerts: alerts.filter(a => a.status === 'active'),
        recentNotifications: notifications.slice(0, 10), // Most recent 10
        recommendations: recommendations,
        unreadCount: action.payload.filter(e => !e.event_metadata?.read).length,
      };

    case 'ADD_EVENT':
      const existingIndex = state.events.findIndex(e => e.id === action.payload.id);
      if (existingIndex !== -1) {
        // Update existing event
        const updatedEvents = [...state.events];
        updatedEvents[existingIndex] = action.payload;
        return eventReducer({ ...state, events: updatedEvents }, { type: 'SET_EVENTS', payload: updatedEvents });
      } else {
        // Add new event
        const newEvents = [...state.events, action.payload];
        return eventReducer({ ...state, events: newEvents }, { type: 'SET_EVENTS', payload: newEvents });
      }

    case 'UPDATE_EVENT':
      const updatedEvents = state.events.map(e => 
        e.id === action.payload.id ? action.payload : e
      );
      return eventReducer({ ...state, events: updatedEvents }, { type: 'SET_EVENTS', payload: updatedEvents });

    case 'REMOVE_EVENT':
      const filteredEvents = state.events.filter(e => e.id !== action.payload);
      return eventReducer({ ...state, events: filteredEvents }, { type: 'SET_EVENTS', payload: filteredEvents });

    case 'MARK_AS_READ':
      const eventsWithRead = state.events.map(e => 
        e.id === action.payload ? { ...e, event_metadata: { ...e.event_metadata, read: true } } : e
      );
      return eventReducer({ ...state, events: eventsWithRead }, { type: 'SET_EVENTS', payload: eventsWithRead });

    case 'ACKNOWLEDGE_EVENT':
      const eventsWithAck = state.events.map(e => 
        e.id === action.payload && e.event_class === 'alert' 
          ? { ...e, status: 'acknowledged' as const } as Event
          : e
      );
      return eventReducer({ ...state, events: eventsWithAck }, { type: 'SET_EVENTS', payload: eventsWithAck });

    case 'RESOLVE_EVENT':
      const eventsWithResolved = state.events.map(e => 
        e.id === action.payload && e.event_class === 'alert' 
          ? { ...e, status: 'resolved' as const, resolved_at: new Date().toISOString() } as Event
          : e
      );
      return eventReducer({ ...state, events: eventsWithResolved }, { type: 'SET_EVENTS', payload: eventsWithResolved });

    case 'CLEAR_EVENTS':
      return initialState;

    default:
      return state;
  }
};

// Context types
interface EventContextType extends EventState {
  addEvent: (event: Event) => void;
  updateEvent: (event: Event) => void;
  removeEvent: (eventId: string) => void;
  setEvents: (events: Event[]) => void;
  markAsRead: (eventId: string) => void;
  acknowledgeEvent: (eventId: string) => void;
  resolveEvent: (eventId: string) => void;
  clearEvents: () => void;
}

// Create context
const EventContext = createContext<EventContextType | undefined>(undefined);

// Provider component
interface EventProviderProps {
  children: ReactNode;
}

export const EventProvider: React.FC<EventProviderProps> = ({ children }) => {
  const [state, dispatch] = useReducer(eventReducer, initialState);

  const value = {
    ...state,
    addEvent: (event: Event) => dispatch({ type: 'ADD_EVENT', payload: event }),
    updateEvent: (event: Event) => dispatch({ type: 'UPDATE_EVENT', payload: event }),
    removeEvent: (eventId: string) => dispatch({ type: 'REMOVE_EVENT', payload: eventId }),
    setEvents: (events: Event[]) => dispatch({ type: 'SET_EVENTS', payload: events }),
    markAsRead: (eventId: string) => dispatch({ type: 'MARK_AS_READ', payload: eventId }),
    acknowledgeEvent: (eventId: string) => dispatch({ type: 'ACKNOWLEDGE_EVENT', payload: eventId }),
    resolveEvent: (eventId: string) => dispatch({ type: 'RESOLVE_EVENT', payload: eventId }),
    clearEvents: () => dispatch({ type: 'CLEAR_EVENTS' }),
  };

  return <EventContext.Provider value={value}>{children}</EventContext.Provider>;
};

// Hook to use the context
export const useEventContext = (): EventContextType => {
  const context = useContext(EventContext);
  if (!context) {
    throw new Error('useEventContext must be used within an EventProvider');
  }
  return context;
};