import { describe, it, expect } from 'vitest'

function slugify(name: string): string {
  return name
    .toLowerCase()
    .replace(/[()]/g, '')
    .replace(/&/g, 'and')
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/(^-|-$)/g, '')
}

describe('useTranslatedData utilities', () => {
  describe('slugify', () => {
    it('should convert to lowercase', () => {
      expect(slugify('Push-Ups')).toBe('push-ups')
      expect(slugify('JUMPING JACKS')).toBe('jumping-jacks')
    })

    it('should replace spaces with hyphens', () => {
      expect(slugify('mountain climbers')).toBe('mountain-climbers')
      expect(slugify('high knees fast')).toBe('high-knees-fast')
    })

    it('should remove parentheses', () => {
      expect(slugify('Exercise (Modified)')).toBe('exercise-modified')
      expect(slugify('Move (Advanced)')).toBe('move-advanced')
    })

    it('should replace & with "and"', () => {
      expect(slugify('Stretch & Cool')).toBe('stretch-and-cool')
      expect(slugify('Core & Abs')).toBe('core-and-abs')
    })

    it('should handle multiple special characters', () => {
      expect(slugify('Full Body (HIIT) & Cardio')).toBe('full-body-hiit-and-cardio')
    })

    it('should collapse multiple hyphens', () => {
      expect(slugify('exercise   name')).toBe('exercise-name')
    })

    it('should trim leading and trailing hyphens', () => {
      expect(slugify('-exercise-')).toBe('exercise')
      expect(slugify('--test--')).toBe('test')
    })

    it('should handle empty string', () => {
      expect(slugify('')).toBe('')
    })

    it('should handle already clean strings', () => {
      expect(slugify('burpees')).toBe('burpees')
    })

    it('should handle numbers', () => {
      expect(slugify('Level 1 Beginner')).toBe('level-1-beginner')
      expect(slugify('30 Second Sprint')).toBe('30-second-sprint')
    })

    it('should handle equipment names', () => {
      expect(slugify('Dumbbells')).toBe('dumbbells')
      expect(slugify('Resistance Band')).toBe('resistance-band')
      expect(slugify('Yoga Mat')).toBe('yoga-mat')
    })

    it('should handle complex exercise names', () => {
      expect(slugify('Renegade Row (Each Arm)')).toBe('renegade-row-each-arm')
      expect(slugify('Plank to Push-Up')).toBe('plank-to-push-up')
    })
  })

  describe('translation key generation', () => {
    it('should generate valid i18n keys for workouts', () => {
      const workoutId = 'full-body-burn'
      const key = `workouts.${workoutId}`
      expect(key).toBe('workouts.full-body-burn')
    })

    it('should generate valid i18n keys for exercises', () => {
      const exerciseName = 'Mountain Climbers'
      const key = `exercises.${slugify(exerciseName)}`
      expect(key).toBe('exercises.mountain-climbers')
    })

    it('should generate valid i18n keys for equipment', () => {
      const equipmentName = 'Resistance Band'
      const key = `equipment.${slugify(equipmentName)}`
      expect(key).toBe('equipment.resistance-band')
    })

    it('should generate valid i18n keys for collections', () => {
      const collectionId = 'morning-energizer'
      const titleKey = `collections.${collectionId}.title`
      const descKey = `collections.${collectionId}.description`
      expect(titleKey).toBe('collections.morning-energizer.title')
      expect(descKey).toBe('collections.morning-energizer.description')
    })

    it('should generate valid i18n keys for programs', () => {
      const programId = '4-week-strength'
      const titleKey = `programs.${programId}.title`
      const descKey = `programs.${programId}.description`
      expect(titleKey).toBe('programs.4-week-strength.title')
      expect(descKey).toBe('programs.4-week-strength.description')
    })
  })

  describe('defaultValue fallback', () => {
    it('should use original value as defaultValue', () => {
      const originalTitle = 'High Intensity Interval Training'
      const translationOptions = {
        defaultValue: originalTitle,
      }
      expect(translationOptions.defaultValue).toBe(originalTitle)
    })

    it('should preserve workout structure when translating', () => {
      const workout = {
        id: 'test-workout',
        title: 'Test Workout',
        exercises: [
          { name: 'Push-Ups', duration: 20 },
          { name: 'Squats', duration: 20 },
        ],
        equipment: ['Mat', 'Dumbbells'],
      }

      const translatedWorkout = {
        ...workout,
        title: 'Translated Title',
        exercises: workout.exercises.map((ex) => ({
          ...ex,
          name: slugify(ex.name),
        })),
        equipment: workout.equipment.map((item) => slugify(item)),
      }

      expect(translatedWorkout.exercises[0].name).toBe('push-ups')
      expect(translatedWorkout.equipment[0]).toBe('mat')
    })
  })

  describe('category mapping', () => {
    const categoryKeyMap: Record<string, string> = {
      'full-body': 'categories.fullBody',
      'upper-body': 'categories.upperBody',
      'lower-body': 'categories.lowerBody',
      'core': 'categories.core',
      'cardio': 'categories.cardio',
    }

    it('should map full-body category', () => {
      expect(categoryKeyMap['full-body']).toBe('categories.fullBody')
    })

    it('should map upper-body category', () => {
      expect(categoryKeyMap['upper-body']).toBe('categories.upperBody')
    })

    it('should map lower-body category', () => {
      expect(categoryKeyMap['lower-body']).toBe('categories.lowerBody')
    })

    it('should map core category', () => {
      expect(categoryKeyMap['core']).toBe('categories.core')
    })

    it('should map cardio category', () => {
      expect(categoryKeyMap['cardio']).toBe('categories.cardio')
    })
  })

  describe('music vibe mapping', () => {
    const vibeKeyMap: Record<string, string> = {
      electronic: 'musicVibes.electronic',
      'hip-hop': 'musicVibes.hipHop',
      pop: 'musicVibes.pop',
      rock: 'musicVibes.rock',
      chill: 'musicVibes.chill',
    }

    it('should map electronic vibe', () => {
      expect(vibeKeyMap['electronic']).toBe('musicVibes.electronic')
    })

    it('should map hip-hop vibe', () => {
      expect(vibeKeyMap['hip-hop']).toBe('musicVibes.hipHop')
    })

    it('should map pop vibe', () => {
      expect(vibeKeyMap['pop']).toBe('musicVibes.pop')
    })

    it('should map rock vibe', () => {
      expect(vibeKeyMap['rock']).toBe('musicVibes.rock')
    })

    it('should map chill vibe', () => {
      expect(vibeKeyMap['chill']).toBe('musicVibes.chill')
    })
  })
})
