"use client";

import useSWR from "swr";
import { fetcher } from "@/lib/fetcher";
import type { DecisionRecord, ContractRecord } from "@/types";

interface Stats {
  totalDecisions: number;
  totalContracts: number;
  completedContracts: number;
  completionRate: number;
}

interface AchievementsResponse {
  stats: Stats;
  decisions: DecisionRecord[];
  contracts: ContractRecord[];
}

export function useAchievements(userId: string | undefined) {
  const { data, isLoading, error } = useSWR<AchievementsResponse>(
    userId ? `/api/user/achievements?userId=${userId}` : null,
    fetcher,
  );

  return {
    stats: data?.stats ?? {
      totalDecisions: 0,
      totalContracts: 0,
      completedContracts: 0,
      completionRate: 0,
    },
    decisions: data?.decisions ?? [],
    contracts: data?.contracts ?? [],
    isLoading,
    error,
  };
}
