// import { createSlice, PayloadAction } from "@reduxjs/toolkit";
// import { AuthState, User } from "../types";

// const initialState: AuthState = {
//   user: null,
//   isAuthenticated: false,
//   isLoading: true,
// };

// const authSlice = createSlice({
//   name: "auth",
//   initialState,
//   reducers: {
//     setCurrentUser: (state, action: PayloadAction<User>) => {
//       state.user = action.payload;
//       state.isAuthenticated = true;
//       state.isLoading = false;
//     },
//     clearCurrentUser: (state) => {
//       state.user = null;
//       state.isAuthenticated = false;
//       state.isLoading = false;
//     },
//     setAuthLoading: (state, action: PayloadAction<boolean>) => {
//       state.isLoading = action.payload;
//     },
//   },
// });

// export const { setCurrentUser, clearCurrentUser, setAuthLoading } =
//   authSlice.actions;
// export default authSlice.reducer;

// import { createSlice, PayloadAction } from "@reduxjs/toolkit";
// import { AuthState, User } from "../types";
// import { clearUserCookieValue, serializeUserCookie } from "../utils/authCookie";

// const STORAGE_KEY = "auth_user";

// const getStoredUser = (): User | null => {
//   if (typeof window === "undefined") return null;
//   try {
//     const raw = localStorage.getItem(STORAGE_KEY);
//     return raw ? (JSON.parse(raw) as User) : null;
//   } catch {
//     return null;
//   }
// };

// const storedUser = getStoredUser();

// const initialState: AuthState = {
//   user: storedUser,
//   isAuthenticated: !!storedUser,
//   isLoading: !storedUser, // cached user hai to turant false, warna true
// };

// const authSlice = createSlice({
//   name: "auth",
//   initialState,
//   reducers: {
//     setCurrentUser: (state, action: PayloadAction<User>) => {
//       state.user = action.payload;
//       state.isAuthenticated = true;
//       state.isLoading = false;
//       try {
//         localStorage.setItem(STORAGE_KEY, JSON.stringify(action.payload));
//       } catch {}
//       try {
//         document.cookie = serializeUserCookie(action.payload);
//       } catch {}
//     },
//     clearCurrentUser: (state) => {
//       state.user = null;
//       state.isAuthenticated = false;
//       state.isLoading = false;
//       try {
//         localStorage.removeItem(STORAGE_KEY);
//       } catch {}
//       try {
//         document.cookie = clearUserCookieValue;
//       } catch {}
//     },
//     setAuthLoading: (state, action: PayloadAction<boolean>) => {
//       state.isLoading = action.payload;
//     },
//   },
// });

// export const { setCurrentUser, clearCurrentUser, setAuthLoading } =
//   authSlice.actions;
// export default authSlice.reducer;

// authSlice.ts
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { AuthState, User } from "../types";
import { clearUserCookieValue, serializeUserCookie } from "../utils/authCookie";

export const STORAGE_KEY = "auth_user";

// Server aur client ka PEHLA render hamesha isi state se shuru ho — koi localStorage read yahan nahi
const initialState: AuthState = {
  user: null,
  isAuthenticated: false,
  isLoading: true,
};

const authSlice = createSlice({
  name: "auth",
  initialState,
  reducers: {
    setCurrentUser: (state, action: PayloadAction<User>) => {
      state.user = action.payload;
      state.isAuthenticated = true;
      state.isLoading = false;
      try {
        localStorage.setItem(STORAGE_KEY, JSON.stringify(action.payload));
      } catch {}
      try {
        document.cookie = serializeUserCookie(action.payload);
      } catch {}
    },
    clearCurrentUser: (state) => {
      state.user = null;
      state.isAuthenticated = false;
      state.isLoading = false;
      try {
        localStorage.removeItem(STORAGE_KEY);
      } catch {}
      try {
        document.cookie = clearUserCookieValue;
      } catch {}
    },
    setAuthLoading: (state, action: PayloadAction<boolean>) => {
      state.isLoading = action.payload;
    },
  },
});

export const { setCurrentUser, clearCurrentUser, setAuthLoading } =
  authSlice.actions;
export default authSlice.reducer;
