import { create } from 'zustand';

// Auth user interface definition
export interface AuthUser {
  email: string;
  username: string;
  full_name?: string | null;
  phone: string;
  id: string;
  tenant_id: string;
  is_active: boolean;
  is_superuser: boolean;
  is_verified: boolean;
  created_at: string;
  updated_at: string;
  last_login_at: string;
  avatar_url: string | null;
  bio: string | null;
  display_name: string;
  department_id: string | null;
  department_name: string | null;
}

// Settings item interface
export interface SettingItem {
  id: string;
  key: string;
  json_value: Record<string, any>;
}

// Settings response interface
export interface SettingsResponse {
  data: SettingItem[];
  total: number;
  page: number;
  size: number;
  total_pages: number;
  has_next: boolean;
  has_prev: boolean;
}

// Auth state interface
export interface AuthState {
  user: AuthUser | null;
  settings: SettingsResponse | null;
  setAuthUser: (user: AuthUser | null) => void;
  getAuthUser: () => AuthUser | null;
  setSettings: (settings: SettingsResponse | null) => void;
  getSettings: () => SettingsResponse | null;
}

// Create auth store
export const useAuthStore = create<AuthState>((set, get) => ({
  user: null,
  settings: null,

  setAuthUser: (user: AuthUser | null) => {
    set({ user });
  },

  getAuthUser: () => {
    return get().user;
  },

  setSettings: (settings: SettingsResponse | null) => {
    set({ settings });
  },

  getSettings: () => {
    return get().settings;
  },
}));

// Export functions for direct usage
export const setAuthUser = (user: AuthUser | null) => {
  useAuthStore.getState().setAuthUser(user);
};

export const getAuthUser = (): AuthUser | null => {
  return useAuthStore.getState().getAuthUser();
};

export const setSettings = (settings: SettingsResponse | null) => {
  useAuthStore.getState().setSettings(settings);
};

export const getSettings = (): SettingsResponse | null => {
  return useAuthStore.getState().getSettings();
};


export default useAuthStore;