import dayjs from 'dayjs';
import 'dayjs/locale/da';

// Set Danish locale as default
dayjs.locale('da');

/**
 * Format a number as Danish currency (kr.)
 */
export function formatCurrency(
  value: number,
  options: {
    showSign?: boolean;
    decimalPlaces?: number;
    currency?: string;
  } = {}
): string {
  const { showSign = false, decimalPlaces = 2, currency = 'kr.' } = options;

  const absValue = Math.abs(value);
  const formattedNumber = absValue.toLocaleString('da-DK', {
    minimumFractionDigits: decimalPlaces,
    maximumFractionDigits: decimalPlaces,
  });

  let result = `${formattedNumber} ${currency}`;

  if (showSign && value !== 0) {
    result = value > 0 ? `+${result}` : `-${result}`;
  } else if (value < 0) {
    result = `-${result}`;
  }

  return result;
}

/**
 * Format a number with Danish locale (1.234,56)
 */
export function formatNumber(
  value: number,
  decimalPlaces: number = 2
): string {
  return value.toLocaleString('da-DK', {
    minimumFractionDigits: decimalPlaces,
    maximumFractionDigits: decimalPlaces,
  });
}

/**
 * Format a date in Danish format
 */
export function formatDate(
  date: string | Date,
  format: string = 'DD/MM/YYYY'
): string {
  return dayjs(date).format(format);
}

/**
 * Format a date with time
 */
export function formatDateTime(
  date: string | Date,
  format: string = 'DD/MM/YYYY HH:mm'
): string {
  return dayjs(date).format(format);
}

/**
 * Format a date in short Danish format (e.g., "15. jan 2025")
 */
export function formatDateShort(date: string | Date): string {
  return dayjs(date).format('D. MMM YYYY');
}

/**
 * Format a date in long Danish format (e.g., "15. januar 2025")
 */
export function formatDateLong(date: string | Date): string {
  return dayjs(date).format('D. MMMM YYYY');
}

/**
 * Format a date for API requests (ISO format)
 */
export function formatDateISO(date: string | Date): string {
  return dayjs(date).format('YYYY-MM-DD');
}

/**
 * Format period (month/year)
 */
export function formatPeriod(date: string | Date): string {
  return dayjs(date).format('MMMM YYYY');
}

/**
 * Format account number (e.g., "1000" -> "1000")
 */
export function formatAccountNumber(accountNumber: string): string {
  // Already formatted, but could add padding if needed
  return accountNumber.padStart(4, '0');
}

/**
 * Format CVR number (e.g., "12345678" -> "12 34 56 78")
 */
export function formatCVR(cvr: string): string {
  const cleaned = cvr.replace(/\D/g, '');
  if (cleaned.length !== 8) return cvr;
  return `${cleaned.slice(0, 2)} ${cleaned.slice(2, 4)} ${cleaned.slice(4, 6)} ${cleaned.slice(6, 8)}`;
}

/**
 * Get CSS class for amount (positive/negative/zero)
 */
export function getAmountClass(value: number): string {
  if (value > 0) return 'amount-positive';
  if (value < 0) return 'amount-negative';
  return 'amount-zero';
}

/**
 * Parse a Danish formatted number string to number
 */
export function parseDanishNumber(value: string): number {
  // Replace Danish thousand separator (.) with nothing
  // Replace Danish decimal separator (,) with dot
  const normalized = value
    .replace(/\./g, '')
    .replace(',', '.')
    .replace(/[^\d.-]/g, '');
  return parseFloat(normalized) || 0;
}

/**
 * Format transaction number
 */
export function formatTransactionNumber(number: string | number): string {
  return `#${number}`;
}

/**
 * Format percentage
 */
export function formatPercentage(value: number, decimalPlaces: number = 1): string {
  return `${formatNumber(value * 100, decimalPlaces)}%`;
}

/**
 * Truncate text with ellipsis
 */
export function truncateText(text: string, maxLength: number): string {
  if (text.length <= maxLength) return text;
  return `${text.slice(0, maxLength - 3)}...`;
}

/**
 * Format file size
 */
export function formatFileSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
  if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
  return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
}
