// Utility functions for availability display logic

export interface AvailabilityInfo {
  available: number;
  total: number;
  reserved: number;
  sold: number;
  is_available: boolean;
}

export interface EventAvailabilitySettings {
  availability_display_mode: 'available_only' | 'show_quantity' | 'smart_threshold';
  availability_threshold: number;
  show_sold_out: boolean;
  low_stock_threshold: number;
  availability_messages: {
    available: string;
    low_stock: string;
    sold_out: string;
    unlimited: string;
  };
}

export interface AvailabilityDisplay {
  text: string;
  className: string;
  showExactCount: boolean;
  isLowStock: boolean;
  isSoldOut: boolean;
}

export function formatAvailabilityDisplay(
  availability: AvailabilityInfo,
  settings: EventAvailabilitySettings
): AvailabilityDisplay {
  const {
    availability_display_mode,
    availability_threshold,
    low_stock_threshold,
    availability_messages
  } = settings;

  const { available, total, is_available } = availability;
  const isUnlimited = total === 999999;
  const isLowStock = !isUnlimited && available <= low_stock_threshold && available > 0;
  const isSoldOut = !is_available;

  // Determine if we should show exact count
  let showExactCount = false;
  switch (availability_display_mode) {
    case 'show_quantity':
      showExactCount = true;
      break;
    case 'smart_threshold':
      showExactCount = !isUnlimited && available <= availability_threshold;
      break;
    case 'available_only':
    default:
      showExactCount = false;
      break;
  }

  // Generate display text
  let text: string;
  let className: string;

  if (isSoldOut) {
    text = availability_messages.sold_out;
    className = 'text-red-600';
  } else if (isUnlimited) {
    text = availability_messages.unlimited;
    className = 'text-green-600';
  } else if (showExactCount) {
    if (isLowStock) {
      text = availability_messages.low_stock.replace('{count}', available.toString());
      className = 'text-orange-600';
    } else {
      text = `${available} available`;
      className = 'text-green-600';
    }
  } else {
    // Just show "Available" without count
    if (isLowStock) {
      // Even in available_only mode, we might want to show low stock warning
      text = availability_messages.low_stock.replace('{count}', available.toString());
      className = 'text-orange-600';
    } else {
      text = availability_messages.available;
      className = 'text-green-600';
    }
  }

  return {
    text,
    className,
    showExactCount,
    isLowStock,
    isSoldOut
  };
}

export function shouldShowTicketType(
  availability: AvailabilityInfo,
  settings: EventAvailabilitySettings
): boolean {
  // If sold out and show_sold_out is false, hide the ticket type
  if (!availability.is_available && !settings.show_sold_out) {
    return false;
  }
  
  return true;
}

// Default settings for events that don't have these fields set
export const defaultAvailabilitySettings: EventAvailabilitySettings = {
  availability_display_mode: 'available_only',
  availability_threshold: 10,
  show_sold_out: true,
  low_stock_threshold: 5,
  availability_messages: {
    available: 'Available',
    low_stock: '{count} left',
    sold_out: 'Sold out',
    unlimited: 'Available'
  }
};