import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";

type User = {
  id: string;
  username: string;
};

interface State {
  accessToken: string | null;
  user: User | null;
}

interface Actions {
  setAccessToken: (accessToken: string) => void;
  setUser: (user: User) => void;
  removeAuthStore: () => void;
}

const useAuthStore = create<State & Actions>()(
  devtools(
    persist(
      (set) => ({
        accessToken: null,
        user: null,
        setAccessToken: (accessToken) => set({ accessToken }),
        setUser: (user) => set({ user }),
        removeAuthStore: () => set({ accessToken: null, user: null }),
      }),
      {
        name: "auth",
      }
    )
  )
);

export default useAuthStore;
