import { useAuth } from '@clerk/nextjs';
import { useQuery } from '@tanstack/react-query';

export const usePresetsData = () => {
  const { getToken } = useAuth();
  return useQuery({
    queryKey: ['presets', 'chat'],
    queryFn: async () => {
      // OK to use fetch here since we're using orpheus Modal API
      try {
        const token = await getToken();
        const env =
          process.env.NEXT_PUBLIC_ORPHEUS_ENV ??
          (process.env.NEXT_PUBLIC_NODE_ENV === 'production' ? 'prod' : 'dev');
        const MODAL_SRV = `https://suno-ai--orpheus-${env}-web.modal.run`;
        const response = await fetch(`${MODAL_SRV}/content`, {
          method: 'GET',
          headers: {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${token}`,
          },
        });
        if (!response.ok) {
          throw new Error(
            `Failed to fetch presets: ${response.status} ${response.statusText}`
          );
        }
        return response.json();
      } catch (e) {
        console.log('Error', e);
        throw e;
      }
    },
    staleTime: 1000 * 60 * 5, // 5 minutes
  });
};
