import React, { useEffect } from "react";
import { View } from "@tarojs/components";
import { silentLogin } from "@/services/loginService";
import { useUserActions } from "@/store/userStore";
import Taro from "@tarojs/taro";
import "./index.scss";
const HomePage: React.FC = () => {
  const { fetchUserInfo, checkNicknameChangeStatus } = useUserActions();

  useEffect(() => {
    const handleLoginRedirect = async () => {
      // 先执行静默登录，然后再跳转
      try {
        console.log("开始静默登录...");
        const loginResult = await silentLogin();
        console.log("静默登录结果:", loginResult);
        if (loginResult.success) {
          // 静默登录成功，获取用户信息
          fetchUserInfo().catch((error) => {
            console.error("获取用户信息失败:", error);
          });
          checkNicknameChangeStatus().catch((error) => {
            console.error("检查昵称变更状态失败:", error);
          });
        }
      } catch (error) {
        console.error("静默登录失败:", error);
        // 静默登录失败不影响使用
      }

      // 无论静默登录是否成功，都跳转到主页面
      Taro.redirectTo({ url: "/main_pages/index" });
    };

    handleLoginRedirect();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []); // fetchUserInfo 是稳定的函数引用，不需要加入依赖项

  return <View className="home_page"></View>;
};

export default HomePage;
