import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { Note } from "../../types/note";

interface NotesState {
  notes: Note[];
  allNotes: Note[]; // Все заметки пользователя для тегов и календаря
  archivedNotes: Note[];
  selectedDate: string | null;
  selectedTag: string | null;
  searchQuery: string;
  loading: boolean;
  editingNoteId: number | null;
  offlineMode: boolean;
  pendingSyncCount: number;
}

const initialState: NotesState = {
  notes: [],
  allNotes: [],
  archivedNotes: [],
  selectedDate: null,
  selectedTag: null,
  searchQuery: "",
  loading: false,
  editingNoteId: null,
  offlineMode: false,
  pendingSyncCount: 0,
};

const notesSlice = createSlice({
  name: "notes",
  initialState,
  reducers: {
    setNotes: (state, action: PayloadAction<Note[]>) => {
      state.notes = action.payload;
    },
    setAllNotes: (state, action: PayloadAction<Note[]>) => {
      state.allNotes = action.payload;
    },
    addNote: (state, action: PayloadAction<Note>) => {
      state.notes.unshift(action.payload);
      state.allNotes.unshift(action.payload);
    },
    updateNote: (state, action: PayloadAction<Note>) => {
      const index = state.notes.findIndex((n) => n.id === action.payload.id);
      if (index !== -1) {
        state.notes[index] = action.payload;
      }
      const allIndex = state.allNotes.findIndex(
        (n) => n.id === action.payload.id
      );
      if (allIndex !== -1) {
        state.allNotes[allIndex] = action.payload;
      }
    },
    deleteNote: (state, action: PayloadAction<number>) => {
      state.notes = state.notes.filter((n) => n.id !== action.payload);
      state.allNotes = state.allNotes.filter((n) => n.id !== action.payload);
    },
    setSelectedDate: (state, action: PayloadAction<string | null>) => {
      state.selectedDate = action.payload;
    },
    setSelectedTag: (state, action: PayloadAction<string | null>) => {
      state.selectedTag = action.payload;
    },
    setSearchQuery: (state, action: PayloadAction<string>) => {
      state.searchQuery = action.payload;
    },
    setEditingNote: (state, action: PayloadAction<number | null>) => {
      state.editingNoteId = action.payload;
    },
    setOfflineMode: (state, action: PayloadAction<boolean>) => {
      state.offlineMode = action.payload;
    },
    setPendingSyncCount: (state, action: PayloadAction<number>) => {
      state.pendingSyncCount = action.payload;
    },
    updateNoteSyncStatus: (
      state,
      action: PayloadAction<{ id: number | string; syncStatus: 'synced' | 'pending' | 'error' }>
    ) => {
      const index = state.notes.findIndex((n) => n.id === action.payload.id);
      if (index !== -1) {
        state.notes[index].syncStatus = action.payload.syncStatus;
      }
      const allIndex = state.allNotes.findIndex((n) => n.id === action.payload.id);
      if (allIndex !== -1) {
        state.allNotes[allIndex].syncStatus = action.payload.syncStatus;
      }
    },
  },
});

export const {
  setNotes,
  setAllNotes,
  addNote,
  updateNote,
  deleteNote,
  setSelectedDate,
  setSelectedTag,
  setSearchQuery,
  setEditingNote,
  setOfflineMode,
  setPendingSyncCount,
  updateNoteSyncStatus,
} = notesSlice.actions;
export default notesSlice.reducer;
