/**
 * @deprecated Scheduled for removal in v2.0.0
 * --------------------------------------------------------------------------
 * ⚠️ NOTICE: V2 DATA&UI REFACTORING (by 0xfullex)
 * --------------------------------------------------------------------------
 * STOP: Feature PRs affecting this file are currently BLOCKED.
 * Only critical bug fixes are accepted during this migration phase.
 *
 * This file is being refactored to v2 standards.
 * Any non-critical changes will conflict with the ongoing work.
 *
 * 🔗 Context & Status:
 * - Contribution Hold: https://github.com/CherryHQ/cherry-studio/issues/10954
 * - v2 Refactor PR   : https://github.com/CherryHQ/cherry-studio/pull/10162
 * --------------------------------------------------------------------------
 */
import type { PayloadAction } from '@reduxjs/toolkit'
import { createSlice } from '@reduxjs/toolkit'

import type { RemoteSyncState } from './backup'

export interface NutstoreSyncState extends RemoteSyncState {}

export interface NutstoreState {
  nutstoreToken: string | null
  nutstorePath: string
  nutstoreAutoSync: boolean
  nutstoreSyncInterval: number
  nutstoreSyncState: NutstoreSyncState
  nutstoreSkipBackupFile: boolean
  nutstoreMaxBackups: number
}

const initialState: NutstoreState = {
  nutstoreToken: '',
  nutstorePath: '/cherry-studio',
  nutstoreAutoSync: false,
  nutstoreSyncInterval: 0,
  nutstoreSyncState: {
    lastSyncTime: null,
    syncing: false,
    lastSyncError: null
  },
  nutstoreSkipBackupFile: false,
  nutstoreMaxBackups: 0
}

const nutstoreSlice = createSlice({
  name: 'nutstore',
  initialState,
  reducers: {
    setNutstoreToken: (state, action: PayloadAction<string>) => {
      state.nutstoreToken = action.payload
    },
    setNutstorePath: (state, action: PayloadAction<string>) => {
      state.nutstorePath = action.payload
    },
    setNutstoreAutoSync: (state, action: PayloadAction<boolean>) => {
      state.nutstoreAutoSync = action.payload
    },
    setNutstoreSyncInterval: (state, action: PayloadAction<number>) => {
      state.nutstoreSyncInterval = action.payload
    },
    setNutstoreSyncState: (state, action: PayloadAction<Partial<RemoteSyncState>>) => {
      state.nutstoreSyncState = { ...state.nutstoreSyncState, ...action.payload }
    },
    setNutstoreSkipBackupFile: (state, action: PayloadAction<boolean>) => {
      state.nutstoreSkipBackupFile = action.payload
    },
    setNutstoreMaxBackups: (state, action: PayloadAction<number>) => {
      state.nutstoreMaxBackups = action.payload
    }
  }
})

export const {
  setNutstoreToken,
  setNutstorePath,
  setNutstoreAutoSync,
  setNutstoreSyncInterval,
  setNutstoreSyncState,
  setNutstoreSkipBackupFile,
  setNutstoreMaxBackups
} = nutstoreSlice.actions

export default nutstoreSlice.reducer
