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

vi.mock("@capakraken/application", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@capakraken/application")>();
  return {
    ...actual,
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
  };
});

vi.mock("../lib/logger.js", () => ({
  logger: {
    error: vi.fn(),
    warn: vi.fn(),
    info: vi.fn(),
    debug: vi.fn(),
  },
}));

vi.mock("../lib/email.js", () => ({
  testSmtpConnection: vi.fn(),
}));

vi.mock("../gemini-client.js", () => ({
  generateGeminiImage: vi.fn(),
  parseGeminiError: vi.fn((err: unknown) => (err instanceof Error ? err.message : String(err))),
}));

import { testSmtpConnection as testSmtpConnectionMock } from "../lib/email.js";
import { generateGeminiImage as generateGeminiImageMock } from "../gemini-client.js";
import { executeTool as executeAssistantTool, type ToolContext } from "../router/assistant-tools.js";

export const executeTool = executeAssistantTool;
export const testSmtpConnection = testSmtpConnectionMock;
export const generateGeminiImage = generateGeminiImageMock;

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