import React from 'react';

import { HeartIcon } from '@/icons';
import { UsagePlanDescription } from '@/state/sessionStore';

export interface BadgeConfig {
  text: string;
  icon?: React.ReactNode;
  className?: string;
}

// Badge configuration for different badge texts
export const BADGE_TEXT_CONFIG: Record<string, Omit<BadgeConfig, 'text'>> = {
  'Current Plan': {
    className: 'bg-foreground-secondary/20 text-foreground-primary/90',
  },
  'Most Popular': {
    className: 'bg-accent-pink text-foreground-primary-on-dark',
  },
  'Best Value': {
    className: 'bg-foreground-secondary/20 text-foreground-primary/90',
  },
};

// Default fallback configuration
const DEFAULT_BADGE_CONFIG: Omit<BadgeConfig, 'text'> = {
  className: 'bg-white/20 text-white/90',
};

// Icon mapper - converts icon identifier to React component
const ICON_MAP: Record<string, React.ReactNode> = {
  heart: <HeartIcon width={12} height={12} />,
};

/**
 * Determines which badge to assign to a plan based on the plan description and current plan status
 * @param planDescription The plan description object from usagePlanDescriptions
 * @param isCurrentPlan Whether this is the user's current plan
 * @returns The appropriate badge configuration or undefined
 */
export const getPlanBadge = (
  planDescription?: UsagePlanDescription,
  isCurrentPlan: boolean = false
): BadgeConfig | undefined => {
  // First check if this is the current plan
  if (isCurrentPlan) {
    return {
      text: 'Current Plan',
      ...BADGE_TEXT_CONFIG['Current Plan'],
    };
  }

  // Check if we have a badge in the plan description
  if (planDescription?.badge?.text) {
    // Use badge data directly from backend (icon, className)
    return {
      text: planDescription.badge.text,
      icon: planDescription.badge.icon
        ? ICON_MAP[planDescription.badge.icon]
        : undefined,
      className:
        planDescription.badge.className ||
        BADGE_TEXT_CONFIG[planDescription.badge.text]?.className ||
        DEFAULT_BADGE_CONFIG.className,
    };
  }

  // No badge for this plan
  return undefined;
};

/**
 * Gets a discount badge when a discount is available, showing "Limited Time Offer"
 * @param planDescription The plan description object from usagePlanDescriptions
 * @param isCurrentPlan Whether this is the user's current plan
 * @param hasDiscount Whether this plan has an active discount
 * @returns The appropriate badge configuration or undefined
 */
export const getDiscountBadge = (
  planDescription?: UsagePlanDescription,
  isCurrentPlan: boolean = false,
  hasDiscount: boolean = false
): BadgeConfig | undefined => {
  // If current plan, always show current plan badge
  if (isCurrentPlan) {
    return getPlanBadge(planDescription, isCurrentPlan);
  }

  // If discount is available, show limited time offer badge
  if (hasDiscount) {
    return {
      text: 'Limited Time Offer',
      className: 'bg-accent-brand text-foreground-primary-glass',
    };
  }

  // Otherwise, show regular plan badge
  return getPlanBadge(planDescription, isCurrentPlan);
};
