// Configuration for the wireframe generator
export const config = {
  // Backend API configuration
  backend: {
    baseUrl: process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000',
    endpoints: {
      health: '/health',
      generateWireframe: '/generate-wireframe',
      generateWireframeDesktop: '/generate-wireframe/desktop',
      generateWireframeTablet: '/generate-wireframe/tablet',
      generateWireframeMobile: '/generate-wireframe/mobile',
      generateAllDevices: '/generate-all-devices',
      wireframes: '/api/wireframes',
      wireframe: (id?: string) => id ? `/api/wireframes/${id}` : '/api/wireframes',
    },
    timeout: 30000, // 30 seconds
  },
  
  // User Authentication Service
  auth: {
    baseUrl: process.env.NEXT_PUBLIC_AUTH_URL || 'http://localhost:8000',
    endpoints: {
      health: '/health',
      register: '/api/auth/register',
      login: '/api/auth/login',
      logout: '/api/auth/logout',
      refresh: '/api/auth/refresh',
      profile: '/api/auth/me',
      preferences: '/api/auth/preferences',
      projects: '/api/auth/projects',
    },
    tokenKey: 'auth_token',
    refreshTokenKey: 'refresh_token',
  },
  
  // UI configuration
  ui: {
    maxPromptLength: 1000,
    statusCheckInterval: 10000, // 10 seconds
    generationTimeout: 30000, // 30 seconds
  },
  
  // Wireframe defaults
  wireframe: {
    defaultPageSize: { width: 1200, height: 800 },
    defaultSpacing: { gap: 16, padding: 20 },
    minElementSize: { width: 80, height: 40 },
  },
} as const

// Helper function to get full API URL
export const getApiUrl = (endpoint: string): string => {
  return `${config.backend.baseUrl}${endpoint}`
}

// Helper function to get full Auth Service URL
export const getAuthUrl = (endpoint: string): string => {
  return `${config.auth.baseUrl}${endpoint}`
}

// Helper function to get health check URL
export const getHealthUrl = (): string => {
  return getApiUrl(config.backend.endpoints.health)
}

// Helper function to get auth health check URL
export const getAuthHealthUrl = (): string => {
  return getAuthUrl(config.auth.endpoints.health)
}

// Helper function to get wireframe generation URL for specific device
export const getWireframeGenerationUrl = (device: 'desktop' | 'tablet' | 'mobile' = 'desktop'): string => {
  switch (device) {
    case 'tablet':
      return getApiUrl(config.backend.endpoints.generateWireframeTablet)
    case 'mobile':
      return getApiUrl(config.backend.endpoints.generateWireframeMobile)
    case 'desktop':
    default:
      return getApiUrl(config.backend.endpoints.generateWireframeDesktop)
  }
}

// Helper function to get universal wireframe generation URL (backward compatibility)
export const getUniversalWireframeGenerationUrl = (): string => {
  return getApiUrl(config.backend.endpoints.generateWireframe)
}

// Helper function to get all devices generation URL
export const getAllDevicesGenerationUrl = (): string => {
  return getApiUrl(config.backend.endpoints.generateAllDevices)
}

// Helper function to get wireframe persistence URLs
export const getWireframeUrl = (id?: string): string => {
  if (id) {
    return getApiUrl(config.backend.endpoints.wireframe(id))
  }
  return getApiUrl(config.backend.endpoints.wireframes)
}

// Helper function to get wireframe by ID URL
export const getWireframeByIdUrl = (id: string): string => {
  return getApiUrl(config.backend.endpoints.wireframe(id))
}

// Authentication helper functions
export const getAuthHeaders = (): HeadersInit => {
  const token = localStorage.getItem(config.auth.tokenKey)
  return token ? { 'Authorization': `Bearer ${token}` } : {}
}

export const getAuthHeadersWithContentType = (): HeadersInit => {
  const token = localStorage.getItem(config.auth.tokenKey)
  return {
    'Content-Type': 'application/json',
    ...(token ? { 'Authorization': `Bearer ${token}` } : {})
  }
}

export const isAuthenticated = (): boolean => {
  const token = localStorage.getItem(config.auth.tokenKey)
  return !!token
}

export const getCurrentUser = (): any => {
  try {
    const token = localStorage.getItem(config.auth.tokenKey)
    if (!token) return null
    
    // Decode JWT token to get user info
    const payload = JSON.parse(atob(token.split('.')[1]))
    return payload
  } catch {
    return null
  }
}
