import { vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import type { ToolContext } from "../router/assistant-tools.js";

export function createToolContext(
  db: Record<string, unknown>,
  userRole: SystemRole = SystemRole.USER,
): ToolContext {
  const dbWithTransaction = "$transaction" in db
    ? db
    : {
        ...db,
        $transaction: async <T>(callback: (tx: ToolContext["db"]) => Promise<T>) =>
          callback(db as ToolContext["db"]),
      };

  return {
    db: dbWithTransaction as ToolContext["db"],
    userId: "user_1",
    userRole,
    permissions: new Set(),
    session: {
      user: { email: "user@example.com", name: "Assistant User", image: null },
      expires: "2026-03-29T00:00:00.000Z",
    },
    dbUser: {
      id: "user_1",
      systemRole: userRole,
      permissionOverrides: null,
    },
    roleDefaults: null,
  };
}

export function withUserLookup(db: Record<string, unknown>, userId = "user_1") {
  return {
    user: {
      findUnique: vi.fn().mockResolvedValue({ id: userId }),
    },
    ...db,
  };
}
