import { create } from "zustand";
import { ABILITIES_KEY, TOKEN_KEY, USER_KEY } from "../config/AppKey";
import { useNavigate } from "react-router-dom";

interface AuthStore {
  token: string | null | undefined;
  abilities: any;
  isAuthenticated: boolean;

  login: (Data: any) => Promise<void>;
  logout: () => Promise<void>;
}

const useAuthState = create<AuthStore>((set) => {
  const storedToken = localStorage.getItem(TOKEN_KEY);
  const storedAbilities = localStorage.getItem(ABILITIES_KEY);

  return {
    isAuthenticated: true,
    token: storedToken,
    abilities: storedAbilities,

    login: async (Data) => {
      console.log(Data);
      localStorage.setItem(TOKEN_KEY, Data?.token);
      localStorage.setItem(USER_KEY, JSON.stringify(Data?.user));
      localStorage.setItem(ABILITIES_KEY, JSON.stringify(Data?.abilities));

      set((state) => ({
        isAuthenticated: true,
        token: Data?.token,
        abilities: Data?.abilities,
      }));
    },
    logout: async () => {
      localStorage.removeItem(TOKEN_KEY);
      localStorage.removeItem(USER_KEY);
      localStorage.removeItem(ABILITIES_KEY);

      set(() => ({
        isAuthenticated: false,
        token: null,
        abilities: null,
      }));
    },
  };
});

export default useAuthState;
