import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import type { SubscriptionStatus, TransactionStatus } from '@/types';

// ─── Tailwind Merge ───────────────────────────────────────────────────────────
export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

// ─── Date Formatters ──────────────────────────────────────────────────────────

function safeDate(dateString: string): Date | null {
  const date = new Date(dateString);
  return isNaN(date.getTime()) ? null : date;
}

export function formatDate(dateString: string): string {
  const date = safeDate(dateString);
  if (!date) return 'Invalid date';
  return new Intl.DateTimeFormat('en-US', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
  }).format(date);
}

export function formatDateShort(dateString: string): string {
  const date = safeDate(dateString);
  if (!date) return 'Invalid date';
  return new Intl.DateTimeFormat('en-US', {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
  }).format(date);
}

export function formatDateTime(dateString: string): string {
  const date = safeDate(dateString);
  if (!date) return 'Invalid date';
  return new Intl.DateTimeFormat('en-US', {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  }).format(date);
}

export function daysUntil(dateString: string): number {
  const target = safeDate(dateString);
  if (!target) return 0;
  const diff = target.getTime() - Date.now();
  return Math.max(0, Math.ceil(diff / (1000 * 60 * 60 * 24)));
}

// ─── Currency Formatters ──────────────────────────────────────────────────────
export function formatCurrency(amount: number, currency: string = 'USD'): string {
  return new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: currency.toUpperCase(),
    minimumFractionDigits: 2,
  }).format(amount);
}

// ─── Subscription Helpers ─────────────────────────────────────────────────────
export function getStatusLabel(status: SubscriptionStatus): string {
  const labels: Record<SubscriptionStatus, string> = {
    active: 'Active',
    trialing: 'Free Trial',
    trial_expired: 'Trial Expired',
    cancelled: 'Cancelled',
    expired: 'Expired',
    none: 'No Plan',
  };
  return labels[status] || 'Unknown';
}

export function getStatusColor(status: SubscriptionStatus): string {
  const colors: Record<SubscriptionStatus, string> = {
    active: 'text-emerald-400',
    trialing: 'text-blue-400',
    trial_expired: 'text-amber-400',
    cancelled: 'text-orange-400',
    expired: 'text-red-400',
    none: 'text-zinc-400',
  };
  return colors[status] || 'text-zinc-400';
}

export function getStatusBadgeClass(status: SubscriptionStatus): string {
  const classes: Record<SubscriptionStatus, string> = {
    active: 'bg-emerald-500/10 text-emerald-400 border-emerald-500/20',
    trialing: 'bg-blue-500/10 text-blue-400 border-blue-500/20',
    trial_expired: 'bg-amber-500/10 text-amber-400 border-amber-500/20',
    cancelled: 'bg-orange-500/10 text-orange-400 border-orange-500/20',
    expired: 'bg-red-500/10 text-red-400 border-red-500/20',
    none: 'bg-zinc-500/10 text-zinc-400 border-zinc-500/20',
  };
  return classes[status] || 'bg-zinc-500/10 text-zinc-400 border-zinc-500/20';
}

export function getTransactionStatusColor(status: TransactionStatus): string {
  const colors: Record<TransactionStatus, string> = {
    succeeded: 'text-emerald-400',
    failed: 'text-red-400',
    pending: 'text-amber-400',
    refunded: 'text-blue-400',
  };
  return colors[status] || 'text-zinc-400';
}

export function getTransactionBadgeClass(status: TransactionStatus): string {
  const classes: Record<TransactionStatus, string> = {
    succeeded: 'bg-emerald-500/10 text-emerald-400 border-emerald-500/20',
    failed: 'bg-red-500/10 text-red-400 border-red-500/20',
    pending: 'bg-amber-500/10 text-amber-400 border-amber-500/20',
    refunded: 'bg-blue-500/10 text-blue-400 border-blue-500/20',
  };
  return classes[status] || 'bg-zinc-500/10 text-zinc-400 border-zinc-500/20';
}

// ─── String Helpers ───────────────────────────────────────────────────────────
export function getInitials(name: string | undefined | null): string {
  if (!name) return '';
  return name
    .split(' ')
    .map((n) => n[0])
    .join('')
    .toUpperCase()
    .slice(0, 2);
}

export function capitalize(str: string): string {
  return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
