import { useState, useEffect } from "react";
import { get } from "./api";

export interface DashboardStats {
  bot: { name: string; avatarUrl: string | null; status: string | null };
  guilds: { count: number };
  users: { active: number; total: number };
  commands: { total: number; active: number; disabled: number };
  ping: { avg: number };
  economy: {
    totalWealth: string;
    avgLevel: number;
    topStreak: number;
    totalItems?: number;
  };
  recentEvents: Array<{
    type: "success" | "error" | "info" | "warn";
    message: string;
    timestamp: string;
    icon?: string;
  }>;
  activeLootdrops?: Array<{
    rewardAmount: number;
    currency: string;
    createdAt: string;
    expiresAt: string | null;
  }>;
  leaderboards?: {
    topLevels: Array<{ username: string; level: number }>;
    topWealth: Array<{ username: string; balance: string }>;
    topNetWorth: Array<{ username: string; netWorth: string }>;
  };
  uptime: number;
  lastCommandTimestamp: number | null;
  maintenanceMode: boolean;
}

export function useDashboard() {
  const [data, setData] = useState<DashboardStats | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    get<DashboardStats>("/api/stats")
      .then(setData)
      .catch((e) => setError(e.error ?? "Failed to load stats"))
      .finally(() => setLoading(false));
  }, []);

  return { data, loading, error };
}
