import { useEffect } from "react";
import { useGLTF } from "@react-three/drei";
import { REPAIR_CASE_MODEL_PATH } from "@/data/gameplay/repairCaseConfig";
import { REPAIR_MISSIONS } from "@/data/gameplay/repairMissions";
import type { RepairMissionId } from "@/types/gameplay/repairMission";

function getPreloadPaths(mission: RepairMissionId): string[] {
  const config = REPAIR_MISSIONS[mission];
  return [
    ...new Set([
      REPAIR_CASE_MODEL_PATH,
      config.modelPath,
      ...config.brokenParts.flatMap((p) => p.modelPath ?? []),
      ...config.replacementParts.flatMap((p) => p.modelPath ?? []),
    ]),
  ];
}

interface RepairGamePreloaderProps {
  mission: RepairMissionId;
}

/**
 * Fires useGLTF.preload() for every asset used by a repair mission.
 * Renders nothing — pure background loading.
 */
export function RepairGamePreloader({
  mission,
}: RepairGamePreloaderProps): null {
  useEffect(() => {
    for (const path of getPreloadPaths(mission)) {
      useGLTF.preload(path);
    }
  }, [mission]);

  return null;
}
