"use client";

import type { AllocationLike, Assignment, DemandRequirement } from "@capakraken/shared";
import { trpc } from "~/lib/trpc/client.js";

/**
 * Fetches full project context when a project is being dragged or the panel opens.
 * Returns the project's resources, their own allocations, and all cross-project allocations.
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ProjectDragContextResult = {
  contextResourceIds: string[];
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  contextAllocations: any[];
  projectAssignments: Assignment<AllocationLike>[];
  projectDemands: DemandRequirement<AllocationLike>[];
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  project: any | null;
};

export function useProjectDragContext(projectId: string | null, enabled = true): ProjectDragContextResult {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const { data } = trpc.timeline.getProjectContext.useQuery(
    { projectId: projectId! },
    { enabled: enabled && !!projectId, staleTime: 10_000 },
  ) as { data: any };

  return {
    contextResourceIds: (data?.resourceIds ?? []) as string[],
    contextAllocations: data?.allResourceAllocations ?? [],
    projectAssignments: (data?.assignments ?? []) as Assignment<AllocationLike>[],
    projectDemands: (data?.demands ?? []) as DemandRequirement<AllocationLike>[],
    project: data?.project ?? null,
  };
}
