import { injectService, SERVICE_TOKENS } from '@/core/di-container'
import type { AuthService } from '@/modules/base/auth/auth-service'

/**
 * Composable to access the injected auth service
 * This replaces the global auth composable with the injected service
 */
export function useAuth() {
  const authService = injectService(SERVICE_TOKENS.AUTH_SERVICE) as AuthService
  
  if (!authService) {
    throw new Error('AuthService not available. Make sure base module is installed.')
  }
  
  return {
    // State
    currentUser: authService.currentUser,
    isAuthenticated: authService.isAuthenticated,
    isLoading: authService.isLoading,
    error: authService.error,
    userDisplay: authService.userDisplay,
    
    // Compatibility aliases
    user: authService.user,
    
    // Actions
    initialize: () => authService.initialize(),
    login: (credentials: any) => authService.login(credentials),
    register: (data: any) => authService.register(data),
    logout: () => authService.logout(),
    updatePassword: (current: string, newPass: string) => authService.updatePassword(current, newPass),
    updateProfile: (data: any) => authService.updateProfile(data),
    checkAuth: () => authService.checkAuth(),
    refresh: () => authService.refresh()
  }
}

// Export singleton reference for compatibility
export function getAuthService() {
  return injectService(SERVICE_TOKENS.AUTH_SERVICE) as AuthService
}

// For files that import { auth } directly
export const auth = {
  get currentUser() {
    const service = getAuthService()
    return service?.currentUser
  },
  get isAuthenticated() {
    const service = getAuthService()
    return service?.isAuthenticated
  },
  get isLoading() {
    const service = getAuthService()
    return service?.isLoading
  },
  get error() {
    const service = getAuthService()
    return service?.error
  },
  get userDisplay() {
    const service = getAuthService()
    return service?.userDisplay
  },
  initialize: () => getAuthService()?.initialize(),
  login: (credentials: any) => getAuthService()?.login(credentials),
  register: (data: any) => getAuthService()?.register(data),
  logout: () => getAuthService()?.logout(),
  updatePassword: (current: string, newPass: string) => getAuthService()?.updatePassword(current, newPass),
  updateProfile: (data: any) => getAuthService()?.updateProfile(data),
  checkAuth: () => getAuthService()?.checkAuth()
}