import { useState, useEffect, useCallback } from 'react';
import { getPatientRisk } from '../../api/copilot';
import type { RiskScore } from '../../api/copilot';

export function useCopilotRisk(patientId: string | undefined) {
  const [data, setData] = useState<RiskScore | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const fetch = useCallback(async () => {
    if (!patientId) return;
    setLoading(true);
    setError(null);
    try {
      const res = await getPatientRisk(patientId);
      setData(res ?? null);
    } catch (err) {
      setError(err instanceof Error ? err.message : '加载风险评分失败');
    } finally {
      setLoading(false);
    }
  }, [patientId]);

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

  return { data, loading, error, refresh: fetch };
}
