import type { StatusColor } from '@/types'

export function resolveSubscriptionStatusColor(status: string): StatusColor {
  const map: Record<string, StatusColor> = {
    active: 'success',
    canceled: 'error',
    past_due: 'warning',
    trialing: 'info',
    incomplete: 'secondary',
  }
  return map[status] ?? 'secondary'
}

export function resolveInvoiceStatusColor(status: string): StatusColor {
  const map: Record<string, StatusColor> = {
    paid: 'success',
    pending: 'warning',
    draft: 'info',
    overdue: 'error',
    void: 'secondary',
  }
  return map[status] ?? 'secondary'
}

export function resolveTransactionStatusColor(status: string): StatusColor {
  const map: Record<string, StatusColor> = {
    succeeded: 'success',
    failed: 'error',
    pending: 'warning',
    refunded: 'secondary',
  }
  return map[status] ?? 'secondary'
}

export function resolveOrderStatusColor(status: string): StatusColor {
  const map: Record<string, StatusColor> = {
    pending: 'warning',
    processing: 'info',
    completed: 'success',
    cancelled: 'error',
    failed: 'error',
  }
  return map[status] ?? 'secondary'
}

export function resolveServiceStatusColor(status: string): StatusColor {
  const map: Record<string, StatusColor> = {
    active: 'success',
    pending: 'warning',
    suspended: 'error',
    terminated: 'secondary',
  }
  return map[status] ?? 'secondary'
}

export function resolveServiceTypeColor(type: string): StatusColor {
  const map: Record<string, StatusColor> = {
    vps: 'info',
    dedicated: 'success',
    hosting: 'warning',
    mysql: 'secondary',
    game: 'error',
    backups: 'primary',
  }
  return map[type] ?? 'secondary'
}

export function crossDomainUrl(domain: string | undefined | null): string {
  if (!domain) return ''
  const scheme = typeof window !== 'undefined' ? window.location.protocol : 'https:'
  return `${scheme}//${domain}`
}

export function resolvePlatformUrl(
  platform: string,
  platformServiceId: string | null,
  bases: Record<string, string>,
): string | null {
  if (!platformServiceId) return null
  const base = bases[platform]
  if (!base) return null
  return `${base.replace(/\/$/, '')}/server/${platformServiceId}`
}

export function resolveTicketStatusColor(status: string): StatusColor {
  const map: Record<string, StatusColor> = {
    open: 'info',
    in_progress: 'success',
    waiting: 'warning',
    closed: 'secondary',
  }
  return map[status] ?? 'secondary'
}

export function resolveTicketPriorityColor(priority: string): StatusColor {
  const map: Record<string, StatusColor> = {
    low: 'success',
    medium: 'info',
    high: 'warning',
    urgent: 'error',
  }
  return map[priority] ?? 'secondary'
}

export function formatPrice(price: string | number, cycle?: string): string {
  const amount = parseFloat(String(price)).toFixed(2)
  return cycle ? `$${amount}/${cycle}` : `$${amount}`
}
