/**
 * Custom hooks for accessing cached data from the store
 * These hooks provide a clean interface for components to fetch and use data
 */

import { useEffect } from "react";
import useDataStore from "../stores/dataStore";

/**
 * Hook to get all songs with caching
 * @returns {Object} { songs, loading, error, refetch }
 */
export function useSongs() {
  const songs = useDataStore((state) => state.songs);
  const loading = useDataStore((state) => state.songsLoading);
  const error = useDataStore((state) => state.songsError);
  const fetchSongs = useDataStore((state) => state.fetchSongs);

  useEffect(() => {
    fetchSongs();
  }, [fetchSongs]);

  return {
    songs,
    loading,
    error,
    refetch: () => fetchSongs(true),
  };
}

/**
 * Hook to get a single song by ID
 * @param {number|string} id - Song ID
 * @returns {Object} { song, loading, error }
 */
export function useSong(id) {
  const songDetails = useDataStore((state) => state.songDetails);
  const loading = useDataStore((state) => state.songDetailsLoading);
  const error = useDataStore((state) => state.songDetailsError);
  const fetchSongDetail = useDataStore((state) => state.fetchSongDetail);

  useEffect(() => {
    if (id) {
      fetchSongDetail(id);
    }
  }, [id, fetchSongDetail]);

  return {
    song: songDetails[id] || null,
    loading,
    error,
  };
}

/**
 * Hook to get all worship lists with caching
 * @returns {Object} { lists, loading, error, refetch }
 */
export function useLists() {
  const lists = useDataStore((state) => state.lists);
  const loading = useDataStore((state) => state.listsLoading);
  const error = useDataStore((state) => state.listsError);
  const fetchLists = useDataStore((state) => state.fetchLists);

  useEffect(() => {
    fetchLists();
  }, [fetchLists]);

  return {
    lists,
    loading,
    error,
    refetch: () => fetchLists(true),
  };
}

/**
 * Hook to get all profiles with caching
 * @returns {Object} { profiles, loading, error, refetch }
 */
export function useProfiles() {
  const profiles = useDataStore((state) => state.profiles);
  const loading = useDataStore((state) => state.profilesLoading);
  const error = useDataStore((state) => state.profilesError);
  const fetchProfiles = useDataStore((state) => state.fetchProfiles);

  useEffect(() => {
    fetchProfiles();
  }, [fetchProfiles]);

  return {
    profiles,
    loading,
    error,
    refetch: () => fetchProfiles(true),
  };
}

/**
 * Hook to get dashboard stats with caching
 * @returns {Object} { stats, loading, error, refetch }
 */
export function useStats() {
  const stats = useDataStore((state) => state.stats);
  const loading = useDataStore((state) => state.statsLoading);
  const error = useDataStore((state) => state.statsError);
  const fetchStats = useDataStore((state) => state.fetchStats);

  useEffect(() => {
    fetchStats();
  }, [fetchStats]);

  return {
    stats,
    loading,
    error,
    refetch: () => fetchStats(true),
  };
}

/**
 * Hook to access invalidation functions
 * Use these after mutations to refresh data
 * @returns {Object} { invalidateSongs, invalidateLists, invalidateProfiles, invalidateStats, invalidateAll }
 */
export function useInvalidate() {
  const invalidateSongs = useDataStore((state) => state.invalidateSongs);
  const invalidateLists = useDataStore((state) => state.invalidateLists);
  const invalidateProfiles = useDataStore((state) => state.invalidateProfiles);
  const invalidateStats = useDataStore((state) => state.invalidateStats);
  const invalidateAll = useDataStore((state) => state.invalidateAll);

  return {
    invalidateSongs,
    invalidateLists,
    invalidateProfiles,
    invalidateStats,
    invalidateAll,
  };
}

// Export the store itself for direct access when needed
export { useDataStore };
