import { useState, useEffect } from 'react';
import { apiFetch } from '../utils/api';

type Channel = { id: string; name: string; type: string; parentId?: string };
type Role = { id: string; name: string; color: string };
type Category = { id: string; name: string };

export function useGuildResources(guildId?: string) {
  const [channels, setChannels] = useState<Channel[]>([]);
  const [roles, setRoles] = useState<Role[]>([]);
  const [categories, setCategories] = useState<Category[]>([]);

  useEffect(() => {
    if (!guildId) return;
    apiFetch<{ channels: Channel[]; roles: Role[]; categories: Category[] }>(
      `/guild/resources?guildId=${encodeURIComponent(guildId)}`
    ).then((res) => {
      setChannels(res.channels || []);
      setRoles(res.roles || []);
      setCategories(res.categories || []);
    }).catch(() => {});
  }, [guildId]);

  return { channels, roles, categories };
}
