import { useState, useEffect, useCallback } from 'react';
import { api } from '@/lib/api';

interface UseBlogCategoriesReturn {
  categories: string[];
  loading: boolean;
  refetch: () => Promise<void>;
}

export function useBlogCategories(): UseBlogCategoriesReturn {
  const [categories, setCategories] = useState<string[]>([]);
  const [loading, setLoading] = useState(true);

  const refetch = useCallback(async () => {
    setLoading(true);
    try {
      const res = await api.get<{ categories: string[] }>('/docs/blog/categories');
      setCategories(res.data.categories ?? []);
    } catch {
      setCategories([]);
    } finally {
      setLoading(false);
    }
  }, []);

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

  return { categories, loading, refetch };
}
