/**
 * API Configuration
 * Centralized configuration for API endpoints and settings
 */

// Determine API base URL based on environment
const getApiBaseUrl = (): string => {
  // In production, this would be set via environment variable
  if (import.meta.env.VITE_API_URL) {
    return import.meta.env.VITE_API_URL;
  }

  // Development: use api subdomain
  return 'http://api.lvh.me:8000';
};

export const API_BASE_URL = getApiBaseUrl();

/**
 * Extract subdomain from current hostname
 * Returns null if on root domain or invalid subdomain
 */
export const getSubdomain = (): string | null => {
  const hostname = window.location.hostname;
  const parts = hostname.split('.');

  // lvh.me without subdomain (root domain) - no business context
  if (hostname === 'lvh.me') {
    return null;
  }

  // Has subdomain
  if (parts.length > 1) {
    const subdomain = parts[0];
    // Exclude special subdomains
    if (['www', 'api', 'platform'].includes(subdomain)) {
      return subdomain === 'platform' ? null : subdomain;
    }
    return subdomain;
  }

  return null;
};

/**
 * Check if current page is platform site
 */
export const isPlatformSite = (): boolean => {
  const hostname = window.location.hostname;
  return hostname.startsWith('platform.');
};

/**
 * Check if current page is business site
 */
export const isBusinessSite = (): boolean => {
  const subdomain = getSubdomain();
  return subdomain !== null && subdomain !== 'platform';
};
