import { PermissionKey, SystemRole } from "@capakraken/shared";
import { vi } from "vitest";

vi.mock("@capakraken/application", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@capakraken/application")>();
  return {
    ...actual,
    getDashboardDemand: vi.fn().mockResolvedValue([]),
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardChargeabilityOverview: vi.fn().mockResolvedValue({
      rows: [],
      top: [],
      watchlist: [],
      month: "2026-03",
    }),
    getDashboardOverview: vi.fn(),
    getDashboardSkillGapSummary: vi.fn().mockResolvedValue({
      roleGaps: [],
      totalOpenPositions: 0,
      skillSupplyTop10: [],
      resourcesByRole: [],
    }),
    getDashboardProjectHealth: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    getDashboardTopValueResources: vi.fn().mockResolvedValue([]),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
  };
});

vi.mock("../lib/cache.js", () => ({
  cacheGet: vi.fn().mockResolvedValue(null),
  cacheSet: vi.fn().mockResolvedValue(undefined),
  cacheInvalidate: vi.fn().mockResolvedValue(undefined),
  invalidateDashboardCache: vi.fn().mockResolvedValue(undefined),
}));

import {
  getDashboardChargeabilityOverview as getDashboardChargeabilityOverviewMock,
  getDashboardDemand as getDashboardDemandMock,
  getDashboardOverview as getDashboardOverviewMock,
  getDashboardProjectHealth as getDashboardProjectHealthMock,
  getDashboardPeakTimes as getDashboardPeakTimesMock,
  getDashboardSkillGapSummary as getDashboardSkillGapSummaryMock,
  getDashboardTopValueResources as getDashboardTopValueResourcesMock,
} from "@capakraken/application";
import { executeTool as executeAssistantTool, type ToolContext } from "../router/assistant-tools.js";

export const executeTool = executeAssistantTool;
export const getDashboardChargeabilityOverview = getDashboardChargeabilityOverviewMock;
export const getDashboardDemand = getDashboardDemandMock;
export const getDashboardOverview = getDashboardOverviewMock;
export const getDashboardProjectHealth = getDashboardProjectHealthMock;
export const getDashboardPeakTimes = getDashboardPeakTimesMock;
export const getDashboardSkillGapSummary = getDashboardSkillGapSummaryMock;
export const getDashboardTopValueResources = getDashboardTopValueResourcesMock;

export function createToolContext(
  db: Record<string, unknown>,
  options?: {
    permissions?: PermissionKey[];
    userRole?: SystemRole;
  },
): ToolContext {
  const userRole = options?.userRole ?? SystemRole.ADMIN;
  return {
    db: db as ToolContext["db"],
    userId: "user_1",
    userRole,
    permissions: new Set(options?.permissions ?? []),
    session: {
      user: { email: "assistant@example.com", name: "Assistant User", image: null },
      expires: "2026-03-29T00:00:00.000Z",
    },
    dbUser: {
      id: "user_1",
      systemRole: userRole,
      permissionOverrides: null,
    },
    roleDefaults: null,
  };
}
