import { create } from "zustand";

interface AuthStore {
  setToken: (token: string, rememberMe?: boolean) => void;
  getToken: () => string | null;
  clearToken: () => void;
}

const useAuthStore = create<AuthStore>((set) => ({
  setToken: (token: string, rememberMe: boolean = false) => {
    if (rememberMe) {
      localStorage.setItem("token", token);
    } else {
      sessionStorage.setItem("token", token);
    }
  },

  getToken: (): string | null => {
    const sessionToken = sessionStorage.getItem("token");
    if (sessionToken) {
      return sessionToken;
    }

    const localToken = localStorage.getItem("token");
    if (localToken) {
      return localToken;
    }

    return null;
  },

  clearToken: () => {
    sessionStorage.removeItem("token");
    localStorage.removeItem("token");
  },
}));

export default useAuthStore;
