import AsyncStorage from "@react-native-async-storage/async-storage";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import { MainUser } from "../types/discord";

type ThemeOption = "auto" | "light" | "dark";

interface BaseStore {
  theme: ThemeOption;
  token: string | null;
  baseUrl: string | null;
  user: MainUser | null;
  setTheme: (mode: ThemeOption) => void;
  setToken: (token: string | null) => void;
  setBaseUrl: (baseUrl: string | null) => void;
  setUser: (user: MainUser | null) => void;
  clearAuth: () => void;
}

export const useBaseStore = create<BaseStore>()(
  persist(
    (set) => ({
      theme: "auto",
      token: null,
      baseUrl: null,
      user: null,
      setTheme: (theme) => set({ theme }),
      setToken: (token) => set({ token }),
      setBaseUrl: (baseUrl) => set({ baseUrl }),
      setUser: (user) => set({ user }),
      clearAuth: () => set({ token: null, baseUrl: null, user: null }),
    }),
    {
      name: "auth-storage",
      storage: createJSONStorage(() => AsyncStorage),
      partialize: (state) => ({ theme: state.theme, token: state.token, baseUrl: state.baseUrl }),
    }
  )
);