// FE-OP002: 前端状态管理优化
// 使用umi的model格式

export interface UserState {
  isLoggedIn: boolean;
  userInfo: any;
  token: string | null;
}

export interface ProductState {
  list: any[];
  selected: any | null;
  loading: boolean;
}

export interface OrderState {
  list: any[];
  selected: any | null;
  loading: boolean;
}

export interface AppState {
  loading: boolean;
  error: string | null;
}

interface State {
  user: UserState;
  products: ProductState;
  orders: OrderState;
  app: AppState;
}

const initialState: State = {
  user: {
    isLoggedIn: false,
    userInfo: null,
    token: null,
  },
  products: {
    list: [],
    selected: null,
    loading: false,
  },
  orders: {
    list: [],
    selected: null,
    loading: false,
  },
  app: {
    loading: false,
    error: null,
  },
};

export default {
  namespace: 'global',
  state: initialState,
  reducers: {
    // User reducers
    setUser(state: State, { payload }: { payload: { userInfo: any; token: string } }) {
      return {
        ...state,
        user: {
          isLoggedIn: true,
          userInfo: payload.userInfo,
          token: payload.token,
        },
      };
    },
    logout(state: State) {
      return {
        ...state,
        user: initialState.user,
      };
    },
    
    // Product reducers
    setProducts(state: State, { payload }: { payload: any[] }) {
      return {
        ...state,
        products: {
          ...state.products,
          list: payload,
        },
      };
    },
    setSelectedProduct(state: State, { payload }: { payload: any | null }) {
      return {
        ...state,
        products: {
          ...state.products,
          selected: payload,
        },
      };
    },
    setProductsLoading(state: State, { payload }: { payload: boolean }) {
      return {
        ...state,
        products: {
          ...state.products,
          loading: payload,
        },
      };
    },
    
    // Order reducers
    setOrders(state: State, { payload }: { payload: any[] }) {
      return {
        ...state,
        orders: {
          ...state.orders,
          list: payload,
        },
      };
    },
    setSelectedOrder(state: State, { payload }: { payload: any | null }) {
      return {
        ...state,
        orders: {
          ...state.orders,
          selected: payload,
        },
      };
    },
    setOrdersLoading(state: State, { payload }: { payload: boolean }) {
      return {
        ...state,
        orders: {
          ...state.orders,
          loading: payload,
        },
      };
    },
    
    // App reducers
    setLoading(state: State, { payload }: { payload: boolean }) {
      return {
        ...state,
        app: {
          ...state.app,
          loading: payload,
        },
      };
    },
    setError(state: State, { payload }: { payload: string | null }) {
      return {
        ...state,
        app: {
          ...state.app,
          error: payload,
        },
      };
    },
    clearError(state: State) {
      return {
        ...state,
        app: {
          ...state.app,
          error: null,
        },
      };
    },
  },
  effects: {
    // 可以在这里添加异步操作
  },
  subscriptions: {
    // 可以在这里添加订阅
  },
};
