import { vi } from "vitest";

vi.mock("@capakraken/application", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@capakraken/application")>();
  return {
    ...actual,
    approveEstimateVersion: vi.fn(),
    cloneEstimate: vi.fn(),
    commitDispoImportBatch: vi.fn(),
    countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }),
    createEstimateExport: vi.fn(),
    createEstimatePlanningHandoff: vi.fn(),
    createEstimateRevision: vi.fn(),
    assessDispoImportReadiness: vi.fn(),
    loadResourceDailyAvailabilityContexts: vi.fn().mockResolvedValue(new Map()),
    getDashboardDemand: vi.fn().mockResolvedValue([]),
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardOverview: vi.fn(),
    getDashboardSkillGapSummary: vi.fn().mockResolvedValue({
      roleGaps: [],
      totalOpenPositions: 0,
      skillSupplyTop10: [],
      resourcesByRole: [],
    }),
    getDashboardProjectHealth: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    getDashboardTopValueResources: vi.fn().mockResolvedValue([]),
    getEstimateById: vi.fn(),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
    stageDispoImportBatch: vi.fn(),
    submitEstimateVersion: vi.fn(),
    updateEstimateDraft: vi.fn(),
  };
});

import { executeTool as executeAssistantTool } from "../router/assistant-tools.js";

export { createToolContext } from "./assistant-tools-allocation-planning-test-helpers.js";

export function createAssignment(overrides: Record<string, unknown> = {}) {
  return {
    id: "assignment_1",
    resourceId: "resource_1",
    projectId: "project_1",
    demandRequirementId: null,
    startDate: new Date("2026-06-01T00:00:00.000Z"),
    endDate: new Date("2026-06-05T00:00:00.000Z"),
    hoursPerDay: 6,
    percentage: 75,
    role: "Designer",
    roleId: null,
    dailyCostCents: 42000,
    status: "PROPOSED",
    metadata: {},
    createdAt: new Date("2026-03-29T00:00:00.000Z"),
    updatedAt: new Date("2026-03-29T00:00:00.000Z"),
    resource: {
      id: "resource_1",
      displayName: "Carol Danvers",
      eid: "EMP-001",
      lcrCents: 7000,
    },
    project: { id: "project_1", name: "Project One", shortCode: "PROJ-1" },
    roleEntity: null,
    demandRequirement: null,
    ...overrides,
  };
}

export function createHappyPathTransaction() {
  const assignment = createAssignment();
  const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
  const assignmentUpdate = vi.fn().mockResolvedValue({
    ...assignment,
    status: "CANCELLED",
  });
  const tx = {
    assignment: {
      findUnique: vi.fn().mockResolvedValue(assignment),
      update: assignmentUpdate,
    },
    auditLog: {
      create: auditCreate,
    },
  };
  const transaction = vi
    .fn()
    .mockImplementation(async (callback: (inner: typeof tx) => Promise<unknown>) => callback(tx));

  return { assignment, assignmentUpdate, auditCreate, transaction };
}

export const executeTool = executeAssistantTool;
