/**
 * Tier Helper Functions
 *
 * Utilities for handling ProRank SEO tier system
 *
 * TIER HIERARCHY (4 tiers):
 * 1. FREE - Gray (default)
 * 2. PRO+ - Purple (purple)
 * 3. BUSINESS+ - Orange (warning)
 * 4. AGENCY+ - Red (error)
 */

/**
 * Normalize tier name to standard format
 * Handles various input formats, mapping legacy "core" to "pro"
 *
 * @param {string} tier - Raw tier name
 * @returns {string} - Normalized tier name (lowercase, no suffix)
 */
export const normalizeTier = (tier) => {
  if (!tier || typeof tier !== 'string') {
    return 'free';
  }

  const normalized = tier
    .toLowerCase()
    .replace('_plus', '')
    .replace('+', '')
    .trim();

  // Legacy alias: treat "core" as "pro"
  return normalized === 'core' ? 'pro' : normalized;
};

/**
 * Get display name for tier (with + suffix for paid tiers)
 *
 * @param {string} tier - Raw tier name
 * @returns {string} - Display name (e.g., 'FREE', 'PRO+', 'PRO+')
 */
export const getTierDisplay = (tier) => {
  const normalized = normalizeTier(tier);

  const displays = {
    free: 'FREE',
    pro: 'PRO+',
    business: 'BUSINESS+',
    agency: 'AGENCY+',
  };

  return displays[normalized] || tier.toUpperCase();
};

/**
 * Get Badge variant (color) for tier
 *
 * @param {string} tier - Raw tier name
 * @returns {string} - Badge variant name
 */
export const getTierBadgeVariant = (tier) => {
  const normalized = normalizeTier(tier);

  const variants = {
    free: 'default',      // Gray
    pro: 'purple',        // Purple
    business: 'warning',  // Orange
    agency: 'error',      // Red
  };

  return variants[normalized] || 'default';
};

/**
 * Get full tier info object
 *
 * @param {string} tier - Raw tier name
 * @returns {object} - Tier info { name, display, variant, level }
 */
export const getTierInfo = (tier) => {
  const normalized = normalizeTier(tier);

  const tierLevels = {
    free: 0,
    pro: 1,
    business: 2,
    agency: 3,
  };

  return {
    name: normalized,
    display: getTierDisplay(tier),
    variant: getTierBadgeVariant(tier),
    level: tierLevels[normalized] || 0,
  };
};

/**
 * Compare tiers (useful for checking if user has access)
 *
 * @param {string} requiredTier - Tier required for feature
 * @param {string} userTier - User's current tier
 * @returns {boolean} - True if user has access
 */
export const hasTierAccess = (requiredTier, userTier) => {
  const required = getTierInfo(requiredTier);
  const user = getTierInfo(userTier);

  return user.level >= required.level;
};

/**
 * Get tier color class (for custom styling)
 *
 * @param {string} tier - Raw tier name
 * @returns {string} - Tailwind color class
 */
export const getTierColorClass = (tier) => {
  const normalized = normalizeTier(tier);

  const colors = {
    free: 'pr:text-gray-500',
    pro: 'pr:text-purple-500',
    business: 'pr:text-warning-500',
    agency: 'pr:text-error-500',
  };

  return colors[normalized] || 'pr:text-gray-500';
};

/**
 * Get tier background color class
 *
 * @param {string} tier - Raw tier name
 * @returns {string} - Tailwind bg color class
 */
export const getTierBgClass = (tier) => {
  const normalized = normalizeTier(tier);

  const bgColors = {
    free: 'pr:bg-gray-100',
    pro: 'pr:bg-purple-100',
    business: 'pr:bg-warning-100',
    agency: 'pr:bg-error-100',
  };

  return bgColors[normalized] || 'pr:bg-gray-100';
};

/**
 * Check if tier is paid tier
 *
 * @param {string} tier - Raw tier name
 * @returns {boolean} - True if paid tier
 */
export const isPaidTier = (tier) => {
  const normalized = normalizeTier(tier);
  return normalized !== 'free';
};

/**
 * Get all tiers in order
 *
 * @returns {array} - Array of tier info objects
 */
export const getAllTiers = () => {
  return [
    'free',
    'pro',
    'business',
    'agency',
  ].map(tier => getTierInfo(tier));
};
