import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface PendingAction {
  id: string;
  type: string;
  payload: any;
  timestamp: string;
  retryCount: number;
}

interface OfflineState {
  isOnline: boolean;
  pendingActions: PendingAction[];
  lastSyncTime: string | null;
  syncInProgress: boolean;
}

const initialState: OfflineState = {
  isOnline: typeof navigator !== 'undefined' ? navigator.onLine : true,
  pendingActions: [],
  lastSyncTime: null,
  syncInProgress: false,
};

const offlineSlice = createSlice({
  name: 'offline',
  initialState,
  reducers: {
    setOnlineStatus: (state, action: PayloadAction<boolean>) => {
      state.isOnline = action.payload;
      if (action.payload && state.pendingActions.length > 0) {
        state.syncInProgress = true;
      }
    },
    addPendingAction: (state, action: PayloadAction<Omit<PendingAction, 'id' | 'timestamp' | 'retryCount'>>) => {
      state.pendingActions.push({
        ...action.payload,
        id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
        timestamp: new Date().toISOString(),
        retryCount: 0,
      });
    },
    removePendingAction: (state, action: PayloadAction<string>) => {
      state.pendingActions = state.pendingActions.filter(a => a.id !== action.payload);
    },
    incrementRetryCount: (state, action: PayloadAction<string>) => {
      const action_ = state.pendingActions.find(a => a.id === action.payload);
      if (action_) {
        action_.retryCount += 1;
      }
    },
    clearPendingActions: (state) => {
      state.pendingActions = [];
    },
    setSyncInProgress: (state, action: PayloadAction<boolean>) => {
      state.syncInProgress = action.payload;
    },
    updateLastSyncTime: (state) => {
      state.lastSyncTime = new Date().toISOString();
    },
  },
});

export const {
  setOnlineStatus,
  addPendingAction,
  removePendingAction,
  incrementRetryCount,
  clearPendingActions,
  setSyncInProgress,
  updateLastSyncTime,
} = offlineSlice.actions;

export default offlineSlice.reducer;
