/**
 * React Query Data Hooks
 * Replaces manual state management with React Query for better caching and performance
 */

import { useQuery } from '@tanstack/react-query'
import { dataService } from '../data/dataService'
import type { Workout, Trainer, Collection, Program } from '../types'

// Query Keys
export const queryKeys = {
  workouts: 'workouts',
  workout: (id: string) => ['workouts', id],
  workoutsByCategory: (category: string) => ['workouts', 'category', category],
  workoutsByTrainer: (trainerId: string) => ['workouts', 'trainer', trainerId],
  featuredWorkouts: ['workouts', 'featured'],
  trainers: 'trainers',
  trainer: (id: string) => ['trainers', id],
  collections: 'collections',
  collection: (id: string) => ['collections', id],
  programs: 'programs',
} as const

/**
 * Hook to fetch all workouts
 */
export function useWorkouts() {
  return useQuery({
    queryKey: [queryKeys.workouts],
    queryFn: () => dataService.getAllWorkouts(),
    staleTime: 1000 * 60 * 5, // 5 minutes
  })
}

/**
 * Hook to fetch single workout
 */
export function useWorkout(id: string | undefined) {
  return useQuery({
    queryKey: queryKeys.workout(id || ''),
    queryFn: () => dataService.getWorkoutById(id!),
    enabled: !!id,
    staleTime: 1000 * 60 * 5,
  })
}

/**
 * Hook to fetch workouts by category
 */
export function useWorkoutsByCategory(category: string) {
  return useQuery({
    queryKey: queryKeys.workoutsByCategory(category),
    queryFn: () => dataService.getWorkoutsByCategory(category),
    enabled: !!category,
    staleTime: 1000 * 60 * 5,
  })
}

/**
 * Hook to fetch workouts by trainer
 */
export function useWorkoutsByTrainer(trainerId: string) {
  return useQuery({
    queryKey: queryKeys.workoutsByTrainer(trainerId),
    queryFn: () => dataService.getWorkoutsByTrainer(trainerId),
    enabled: !!trainerId,
    staleTime: 1000 * 60 * 5,
  })
}

/**
 * Hook to fetch featured workouts
 */
export function useFeaturedWorkouts() {
  return useQuery({
    queryKey: queryKeys.featuredWorkouts,
    queryFn: () => dataService.getFeaturedWorkouts(),
    staleTime: 1000 * 60 * 5,
  })
}

/**
 * Hook to fetch popular workouts (uses recent workouts as proxy)
 */
export function usePopularWorkouts(count: number = 8) {
  return useQuery({
    queryKey: ['workouts', 'popular', count],
    queryFn: async () => {
      const allWorkouts = await dataService.getAllWorkouts()
      return allWorkouts.slice(0, count)
    },
    staleTime: 1000 * 60 * 5,
  })
}

/**
 * Hook to fetch all trainers
 */
export function useTrainers() {
  return useQuery({
    queryKey: [queryKeys.trainers],
    queryFn: () => dataService.getAllTrainers(),
    staleTime: 1000 * 60 * 5,
  })
}

/**
 * Hook to fetch single trainer
 */
export function useTrainer(id: string | undefined) {
  return useQuery({
    queryKey: queryKeys.trainer(id || ''),
    queryFn: () => dataService.getTrainerById(id!),
    enabled: !!id,
    staleTime: 1000 * 60 * 5,
  })
}

/**
 * Hook to fetch all collections
 */
export function useCollections() {
  return useQuery({
    queryKey: [queryKeys.collections],
    queryFn: () => dataService.getAllCollections(),
    staleTime: 1000 * 60 * 5,
  })
}

/**
 * Hook to fetch single collection
 */
export function useCollection(id: string | undefined) {
  return useQuery({
    queryKey: queryKeys.collection(id || ''),
    queryFn: () => dataService.getCollectionById(id!),
    enabled: !!id,
    staleTime: 1000 * 60 * 5,
  })
}

/**
 * Hook to fetch all programs
 */
export function usePrograms() {
  return useQuery({
    queryKey: [queryKeys.programs],
    queryFn: () => dataService.getAllPrograms(),
    staleTime: 1000 * 60 * 5,
  })
}