/** Super admin host (meshkee.app in production). Defaults to current hostname. */
export function getAdminDomain(): string {
  const fromEnv = import.meta.env.VITE_ADMIN_DOMAIN
  if (fromEnv) return fromEnv
  return window.location.hostname
}

export function isAllowedAdminHost(hostname = window.location.hostname): boolean {
  return hostname === getAdminDomain()
}

/**
 * Public invoice URL for platform invoices.
 * Local/dev: current origin (`https://meshkee.app:5174/invoices/{publicId}`) so the show page is reachable.
 * Production: `https://{VITE_INVOICE_PUBLIC_DOMAIN}/invoices/{publicId}` (default meshkee.com).
 * Override either with `VITE_INVOICE_PUBLIC_BASE_URL` (full origin, optional path prefix).
 */
export function getPlatformInvoicePublicUrl(publicId: string): string {
  const baseOverride = import.meta.env.VITE_INVOICE_PUBLIC_BASE_URL?.trim()
  if (baseOverride) {
    return `${baseOverride.replace(/\/$/, '')}/invoices/${publicId}`
  }
  if (import.meta.env.DEV) {
    return `${window.location.origin}/invoices/${publicId}`
  }
  const domain = import.meta.env.VITE_INVOICE_PUBLIC_DOMAIN?.trim() || 'meshkee.com'
  return `https://${domain}/invoices/${publicId}`
}

/** Marketing / main business site for platform invoices (default https://meshkee.com). */
export function getPlatformMarketingUrl(): string {
  const domain = import.meta.env.VITE_INVOICE_PUBLIC_DOMAIN?.trim() || 'meshkee.com'
  return `https://${domain.replace(/^https?:\/\//, '').replace(/\/$/, '')}`
}
