import { beforeEach, describe, expect, it, vi } from "vitest";
import { PermissionKey, SystemRole } from "@capakraken/shared";
import { VacationType } from "@capakraken/db";
import { apiRateLimiter } from "../middleware/rate-limit.js";

vi.mock("@capakraken/application", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@capakraken/application")>();
  return {
    ...actual,
    countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }),
    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([]),
  };
});

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),
}));

vi.mock("../ai-client.js", async (importOriginal) => {
  const actual = await importOriginal<typeof import("../ai-client.js")>();
  return {
    ...actual,
    createAiClient: vi.fn(() => ({
      chat: {
        completions: {
          create: vi.fn().mockResolvedValue({
            choices: [
              {
                message: {
                  content: "Project is on track overall, but staffing remains partially open.",
                },
              },
            ],
          }),
        },
      },
    })),
    createDalleClient: vi.fn(() => ({
      images: {
        generate: vi.fn().mockResolvedValue({
          data: [{ b64_json: "ZmFrZQ==" }],
        }),
      },
    })),
    loggedAiCall: vi.fn(async (_provider, _model, _promptLength, fn) => fn()),
  };
});

import {
  countPlanningEntries,
  getDashboardDemand,
  getDashboardOverview,
  getDashboardProjectHealth,
  getDashboardPeakTimes,
  getDashboardSkillGapSummary,
  getDashboardTopValueResources,
  getEstimateById,
  listAssignmentBookings,
} from "@capakraken/application";
import { executeTool, type ToolContext } from "../router/assistant-tools.js";

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,
  };
}

describe("assistant import/export and dispo tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    apiRateLimiter.reset();
    vi.mocked(countPlanningEntries).mockResolvedValue({ countsByRoleId: new Map() });
    vi.mocked(getEstimateById).mockReset();
  });

  it("exports resources CSV through the real import/export router path", async () => {
    const ctx = createToolContext(
      {
        resource: {
          findMany: vi.fn().mockResolvedValue([
            {
              eid: "EMP-001",
              displayName: "Carol Danvers",
              email: "carol@example.com",
              chapter: "Delivery",
              lcrCents: 8000,
              ucrCents: 12000,
              currency: "EUR",
              chargeabilityTarget: 0.8,
              dynamicFields: {},
            },
          ]),
        },
        blueprint: {
          findMany: vi.fn().mockResolvedValue([]),
        },
      },
      { userRole: SystemRole.CONTROLLER },
    );

    const result = await executeTool("export_resources_csv", "{}", ctx);

    expect(JSON.parse(result.content)).toEqual({
      format: "csv",
      lineCount: 2,
      csv: "eid,displayName,email,chapter,lcrCents,ucrCents,currency,chargeabilityTarget\nEMP-001,Carol Danvers,carol@example.com,Delivery,8000,12000,EUR,0.8",
    });
  });

  it("requires importData permission for CSV imports", async () => {
    const ctx = createToolContext(
      {
        auditLog: { create: vi.fn() },
      },
      {
        userRole: SystemRole.MANAGER,
        permissions: [],
      },
    );

    const result = await executeTool(
      "import_csv_data",
      JSON.stringify({
        entityType: "resources",
        rows: [{ eid: "EMP-001", displayName: "Carol Danvers" }],
        dryRun: true,
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        error: expect.stringContaining(PermissionKey.IMPORT_DATA),
      }),
    );
  });

  it("enforces admin access for dispo batch inspection via the backing router", async () => {
    const ctx = createToolContext(
      {
        importBatch: {
          findUnique: vi.fn(),
        },
      },
      { userRole: SystemRole.MANAGER },
    );

    const result = await executeTool(
      "get_dispo_import_batch",
      JSON.stringify({ id: "batch_1" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        error: expect.stringContaining("Admin role required"),
      }),
    );
  });

  it("surfaces protected AI configuration checks to non-admin users", async () => {
    const ctx = createToolContext(
      {
        systemSettings: {
          findUnique: vi.fn().mockResolvedValue({
            aiProvider: "openai",
            azureOpenAiDeployment: "gpt-4o-mini",
            azureOpenAiApiKey: "secret",
          }),
        },
      },
      { userRole: SystemRole.USER },
    );

    const result = await executeTool("get_ai_configured", "{}", ctx);

    expect(JSON.parse(result.content)).toEqual({ configured: true });
  });

  it("masks webhook secrets in assistant responses", async () => {
    const ctx = createToolContext(
      {
        webhook: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "wh_1",
              name: "Primary",
              url: "https://example.com/hook",
              secret: "super-secret",
              events: ["project.updated"],
              isActive: true,
              createdAt: new Date("2026-03-29T00:00:00.000Z"),
            },
          ]),
          findUnique: vi.fn().mockResolvedValue({
            id: "wh_1",
            name: "Primary",
            url: "https://example.com/hook",
            secret: "super-secret",
            events: ["project.updated"],
            isActive: true,
            createdAt: new Date("2026-03-29T00:00:00.000Z"),
          }),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

    const listResult = await executeTool("list_webhooks", "{}", ctx);
    const getResult = await executeTool("get_webhook", JSON.stringify({ id: "wh_1" }), ctx);

    expect(JSON.parse(listResult.content)).toEqual([
      expect.objectContaining({
        id: "wh_1",
        hasSecret: true,
      }),
    ]);
    expect(JSON.parse(listResult.content)[0]).not.toHaveProperty("secret");
    expect(JSON.parse(getResult.content)).toEqual(
      expect.objectContaining({
        id: "wh_1",
        hasSecret: true,
      }),
    );
    expect(JSON.parse(getResult.content)).not.toHaveProperty("secret");
  });

  it("forwards staged dispo resource queries through the real dispo router path", async () => {
    const ctx = createToolContext(
      {
        stagedResource: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "sr_1",
              importBatchId: "batch_1",
              canonicalExternalId: "EMP-001",
              status: "PARSED",
            },
          ]),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

    const result = await executeTool(
      "list_dispo_staged_resources",
      JSON.stringify({ importBatchId: "batch_1", status: "PARSED", limit: 10 }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      items: [
        expect.objectContaining({
          id: "sr_1",
          importBatchId: "batch_1",
          canonicalExternalId: "EMP-001",
          status: "PARSED",
        }),
      ],
    });
  });

  it("lists notifications for the current session user through the real notification router path", async () => {
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
      },
      notification: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "note_1",
            title: "Check staffing",
            body: "Review the new staffing suggestion",
            readAt: null,
            createdAt: new Date("2026-03-29T08:00:00.000Z"),
          },
        ]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool(
      "list_notifications",
      JSON.stringify({ unreadOnly: true, limit: 5 }),
      ctx,
    );

    expect(db.user.findUnique).toHaveBeenCalledWith({
      where: { email: "assistant@example.com" },
      select: { id: true },
    });
    expect(db.notification.findMany).toHaveBeenCalledWith({
      where: {
        userId: "user_1",
        readAt: null,
      },
      orderBy: { createdAt: "desc" },
      take: 5,
    });
    expect(JSON.parse(result.content)).toEqual([
      expect.objectContaining({
        id: "note_1",
        title: "Check staffing",
      }),
    ]);
  });

  it("routes statistics through the dashboard overview path", async () => {
    vi.mocked(getDashboardOverview).mockResolvedValue({
      totalResources: 12,
      activeResources: 10,
      inactiveResources: 2,
      totalProjects: 7,
      activeProjects: 4,
      inactiveProjects: 3,
      totalAllocations: 21,
      activeAllocations: 18,
      cancelledAllocations: 3,
      approvedVacations: 6,
      totalEstimates: 9,
      budgetSummary: {
        totalBudgetCents: 1_234_56,
        totalCostCents: 654_32,
        avgUtilizationPercent: 53,
      },
      budgetBasis: {
        remainingBudgetCents: 58_024,
        budgetedProjects: 5,
        unbudgetedProjects: 2,
        trackedAssignmentCount: 18,
        windowStart: null,
        windowEnd: null,
      },
      projectsByStatus: [
        { status: "ACTIVE", count: 4 },
        { status: "DRAFT", count: 2 },
        { status: "DONE", count: 1 },
      ],
      chapterUtilization: [
        { chapter: "CGI", resourceCount: 5, avgChargeabilityTarget: 78 },
        { chapter: "Compositing", resourceCount: 3, avgChargeabilityTarget: 74 },
        { chapter: "Unassigned", resourceCount: 2, avgChargeabilityTarget: 0 },
      ],
      recentActivity: [],
    });

    const ctx = createToolContext({}, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool("get_statistics", "{}", ctx);

    expect(JSON.parse(result.content)).toEqual({
      activeResources: 10,
      totalProjects: 7,
      activeProjects: 4,
      totalAllocations: 21,
      approvedVacations: 6,
      totalEstimates: 9,
      totalBudget: "1.234,56 EUR",
      projectsByStatus: {
        ACTIVE: 4,
        DRAFT: 2,
        DONE: 1,
      },
      topChapters: [
        { chapter: "CGI", count: 5 },
        { chapter: "Compositing", count: 3 },
        { chapter: "Unassigned", count: 2 },
      ],
    });
  });

  it("marks all unread notifications as read through the notification router", async () => {
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
      },
      notification: {
        updateMany: vi.fn().mockResolvedValue({ count: 3 }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool("mark_notification_read", "{}", ctx);

    expect(db.notification.updateMany).toHaveBeenCalledWith({
      where: { userId: "user_1", readAt: null },
      data: { readAt: expect.any(Date) },
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: "All unread notifications marked as read.",
      }),
    );
  });

  it("routes audit timeline reads through the real audit router detail path", async () => {
    const ctx = createToolContext({
      auditLog: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "audit_1",
            entityType: "project",
            entityId: "project_1",
            entityName: "Apollo",
            action: "updated",
            userId: "user_1",
            source: "ui",
            summary: "Changed budget",
            changes: { budget: [1000, 1200] },
            createdAt: new Date("2026-03-29T12:00:00.000Z"),
            user: {
              id: "user_1",
              name: "Assistant User",
              email: "assistant@example.com",
            },
          },
        ]),
      },
    }, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool(
      "get_audit_log_timeline",
      JSON.stringify({ limit: 10 }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      "2026-03-29": [
        {
          id: "audit_1",
          entityType: "project",
          entityId: "project_1",
          entityName: "Apollo",
          action: "updated",
          userId: "user_1",
          source: "ui",
          summary: "Changed budget",
          createdAt: "2026-03-29T12:00:00.000Z",
          changes: { budget: [1000, 1200] },
          user: {
            id: "user_1",
            name: "Assistant User",
            email: "assistant@example.com",
          },
        },
      ],
    });
  });

  it("returns task lists and counts for the current user through the notification router", async () => {
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
      },
      notification: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "task_1",
            title: "Approve vacation",
            category: "APPROVAL",
            taskStatus: "OPEN",
            priority: "HIGH",
          },
        ]),
        groupBy: vi.fn().mockResolvedValue([
          { taskStatus: "OPEN", _count: 2 },
          { taskStatus: "IN_PROGRESS", _count: 1 },
        ]),
        count: vi.fn().mockResolvedValue(1),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const listResult = await executeTool(
      "list_tasks",
      JSON.stringify({ status: "OPEN", includeAssigned: false, limit: 10 }),
      ctx,
    );
    const countsResult = await executeTool("get_task_counts", "{}", ctx);

    expect(db.notification.findMany).toHaveBeenCalledWith({
      where: {
        userId: "user_1",
        category: { in: ["TASK", "APPROVAL"] },
        taskStatus: "OPEN",
      },
      orderBy: [{ priority: "desc" }, { dueDate: "asc" }, { createdAt: "desc" }],
      take: 10,
    });
    expect(JSON.parse(listResult.content)).toEqual([
      expect.objectContaining({
        id: "task_1",
        title: "Approve vacation",
      }),
    ]);
    expect(JSON.parse(countsResult.content)).toEqual({
      open: 2,
      inProgress: 1,
      done: 0,
      dismissed: 0,
      overdue: 1,
    });
  });

  it("reads task details through the real notification router path", async () => {
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
      },
      notification: {
        findFirst: vi.fn().mockResolvedValue({
          id: "task_1",
          title: "Approve vacation",
          body: "Please approve the request",
          type: "TASK_CREATED",
          priority: "HIGH",
          category: "APPROVAL",
          taskStatus: "OPEN",
          taskAction: "approve_vacation:vac_1",
          dueDate: new Date("2026-04-01T12:00:00.000Z"),
          entityId: "vac_1",
          entityType: "vacation",
          completedAt: null,
          completedBy: null,
          createdAt: new Date("2026-03-29T08:00:00.000Z"),
          userId: "user_1",
          assigneeId: null,
          sender: { id: "sender_1", name: "Scheduler", email: "scheduler@example.com" },
        }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const result = await executeTool(
      "get_task_detail",
      JSON.stringify({ taskId: "task_1" }),
      ctx,
    );

    expect(db.notification.findFirst).toHaveBeenCalledWith({
      where: {
        id: "task_1",
        OR: [{ userId: "user_1" }, { assigneeId: "user_1" }],
        category: { in: ["TASK", "APPROVAL"] },
      },
      select: expect.objectContaining({
        id: true,
        title: true,
        sender: { select: { id: true, name: true, email: true } },
      }),
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        id: "task_1",
        title: "Approve vacation",
        sender: expect.objectContaining({
          id: "sender_1",
          name: "Scheduler",
        }),
      }),
    );
  });

  it("routes task action execution through the real notification router path", async () => {
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
      },
      notification: {
        findFirst: vi.fn().mockResolvedValue({
          id: "task_1",
          userId: "user_1",
          assigneeId: null,
          taskAction: "approve_vacation:vac_1",
          taskStatus: "OPEN",
        }),
        update: vi.fn().mockResolvedValue({
          id: "task_1",
          taskStatus: "DONE",
          completedBy: "user_1",
        }),
      },
      vacation: {
        findUnique: vi.fn().mockResolvedValue({
          id: "vac_1",
          status: "PENDING",
        }),
        update: vi.fn().mockResolvedValue({
          id: "vac_1",
          status: "APPROVED",
        }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const result = await executeTool(
      "execute_task_action",
      JSON.stringify({ taskId: "task_1" }),
      ctx,
    );

    expect(db.notification.findFirst).toHaveBeenCalledWith({
      where: {
        id: "task_1",
        OR: [{ userId: "user_1" }, { assigneeId: "user_1" }],
        category: { in: ["TASK", "APPROVAL"] },
      },
      select: {
        id: true,
        userId: true,
        assigneeId: true,
        taskAction: true,
        taskStatus: true,
      },
    });
    expect(db.vacation.update).toHaveBeenCalledWith({
      where: { id: "vac_1" },
      data: { status: "APPROVED" },
    });
    expect(db.notification.update).toHaveBeenCalledWith({
      where: { id: "task_1" },
      data: expect.objectContaining({
        taskStatus: "DONE",
        completedBy: "user_1",
        completedAt: expect.any(Date),
      }),
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: "Vacation approved",
        task: expect.objectContaining({
          id: "task_1",
          taskStatus: "DONE",
        }),
      }),
    );
  });

  it("creates, lists, updates, and deletes reminders through the notification router", async () => {
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
      },
      notification: {
        create: vi.fn().mockResolvedValue({
          id: "rem_1",
          title: "Submit report",
          category: "REMINDER",
        }),
        findMany: vi.fn().mockResolvedValue([
          {
            id: "rem_1",
            title: "Submit report",
            category: "REMINDER",
          },
        ]),
        findFirst: vi.fn()
          .mockResolvedValueOnce({
            id: "rem_1",
            userId: "user_1",
            category: "REMINDER",
          })
          .mockResolvedValueOnce({
            id: "rem_1",
            userId: "user_1",
            category: "REMINDER",
          }),
        update: vi.fn().mockResolvedValue({
          id: "rem_1",
          title: "Submit updated report",
          category: "REMINDER",
        }),
        delete: vi.fn().mockResolvedValue({ id: "rem_1" }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const createResult = await executeTool(
      "create_reminder",
      JSON.stringify({
        title: "Submit report",
        remindAt: "2026-04-01T09:00:00.000Z",
        recurrence: "weekly",
      }),
      ctx,
    );
    const listResult = await executeTool("list_reminders", JSON.stringify({ limit: 10 }), ctx);
    const updateResult = await executeTool(
      "update_reminder",
      JSON.stringify({
        id: "rem_1",
        title: "Submit updated report",
        recurrence: null,
      }),
      ctx,
    );
    const deleteResult = await executeTool(
      "delete_reminder",
      JSON.stringify({ id: "rem_1" }),
      ctx,
    );

    expect(db.notification.create).toHaveBeenCalledWith({
      data: expect.objectContaining({
        userId: "user_1",
        title: "Submit report",
        category: "REMINDER",
        recurrence: "weekly",
        remindAt: new Date("2026-04-01T09:00:00.000Z"),
        nextRemindAt: new Date("2026-04-01T09:00:00.000Z"),
      }),
    });
    expect(db.notification.findMany).toHaveBeenCalledWith({
      where: { userId: "user_1", category: "REMINDER" },
      orderBy: { nextRemindAt: "asc" },
      take: 10,
    });
    expect(db.notification.update).toHaveBeenCalledWith({
      where: { id: "rem_1" },
      data: {
        title: "Submit updated report",
        recurrence: null,
      },
    });
    expect(db.notification.delete).toHaveBeenCalledWith({ where: { id: "rem_1" } });
    expect(JSON.parse(createResult.content)).toEqual(
      expect.objectContaining({ reminderId: "rem_1", success: true }),
    );
    expect(JSON.parse(listResult.content)).toEqual([
      expect.objectContaining({ id: "rem_1", title: "Submit report" }),
    ]);
    expect(JSON.parse(updateResult.content)).toEqual(
      expect.objectContaining({ reminderId: "rem_1", success: true }),
    );
    expect(JSON.parse(deleteResult.content)).toEqual(
      expect.objectContaining({ id: "rem_1", success: true }),
    );
  });

  it("reads broadcast details through the real notification router and rejects plain users", async () => {
    const db = {
      notificationBroadcast: {
        findUnique: vi.fn().mockResolvedValue({
          id: "broadcast_1",
          title: "Office update",
          body: "New schedule",
          sender: { id: "sender_1", name: "Manager", email: "manager@example.com" },
        }),
      },
    };
    const managerCtx = createToolContext(db, { userRole: SystemRole.MANAGER });
    const userCtx = createToolContext({}, { userRole: SystemRole.USER });

    const successResult = await executeTool(
      "get_broadcast_detail",
      JSON.stringify({ id: "broadcast_1" }),
      managerCtx,
    );
    const deniedResult = await executeTool(
      "get_broadcast_detail",
      JSON.stringify({ id: "broadcast_1" }),
      userCtx,
    );

    expect(db.notificationBroadcast.findUnique).toHaveBeenCalledWith({
      where: { id: "broadcast_1" },
      include: {
        sender: { select: { id: true, name: true, email: true } },
      },
    });
    expect(JSON.parse(successResult.content)).toEqual(
      expect.objectContaining({
        id: "broadcast_1",
        title: "Office update",
      }),
    );
    expect(JSON.parse(deniedResult.content)).toEqual(
      expect.objectContaining({
        error: expect.stringContaining("Manager or Admin role required"),
      }),
    );
  });

  it("lists users only for admins through the real user router", async () => {
    const db = {
      user: {
        findMany: vi.fn().mockResolvedValue([
          { id: "user_1", name: "Alice", email: "alice@example.com" },
          { id: "user_2", name: "Bob", email: "bob@example.com" },
        ]),
      },
    };
    const adminCtx = createToolContext(db, { userRole: SystemRole.ADMIN });
    const managerCtx = createToolContext({}, { userRole: SystemRole.MANAGER });

    const adminResult = await executeTool(
      "list_users",
      JSON.stringify({ limit: 1 }),
      adminCtx,
    );
    const deniedResult = await executeTool("list_users", "{}", managerCtx);

    expect(db.user.findMany).toHaveBeenCalledWith({
      select: {
        id: true,
        name: true,
        email: true,
        systemRole: true,
        createdAt: true,
        lastLoginAt: true,
        lastActiveAt: true,
        permissionOverrides: true,
        totpEnabled: true,
      },
      orderBy: { name: "asc" },
    });
    expect(JSON.parse(adminResult.content)).toEqual([
      expect.objectContaining({ id: "user_1", name: "Alice" }),
    ]);
    expect(JSON.parse(deniedResult.content)).toEqual(
      expect.objectContaining({
        error: expect.stringContaining("Admin role required"),
      }),
    );
  });

  it("reads estimate details through the real estimate router and rejects plain users", async () => {
    vi.mocked(getEstimateById).mockResolvedValue({
      id: "est_1",
      name: "North Cluster Estimate",
      status: "DRAFT",
      versions: [],
    } as Awaited<ReturnType<typeof getEstimateById>>);

    const controllerCtx = createToolContext({}, {
      userRole: SystemRole.CONTROLLER,
      permissions: [PermissionKey.VIEW_COSTS],
    });
    const userCtx = createToolContext({}, { userRole: SystemRole.USER });

    const successResult = await executeTool(
      "get_estimate_detail",
      JSON.stringify({ estimateId: "est_1" }),
      controllerCtx,
    );
    const deniedResult = await executeTool(
      "get_estimate_detail",
      JSON.stringify({ estimateId: "est_1" }),
      userCtx,
    );

    expect(vi.mocked(getEstimateById)).toHaveBeenCalledWith(controllerCtx.db, "est_1");
    expect(JSON.parse(successResult.content)).toEqual(
      expect.objectContaining({
        id: "est_1",
        name: "North Cluster Estimate",
      }),
    );
    expect(JSON.parse(deniedResult.content)).toEqual(
      expect.objectContaining({
        error: expect.stringContaining(PermissionKey.VIEW_COSTS),
      }),
    );
  });

  it("lists estimate versions through the real estimate router and rejects plain users", async () => {
    const db = {
      estimate: {
        findUnique: vi.fn().mockResolvedValue({
          id: "est_1",
          name: "North Cluster Estimate",
          status: "DRAFT",
          latestVersionNumber: 4,
          versions: [
            {
              id: "ver_4",
              versionNumber: 4,
              label: "Rev 4",
              status: "IN_REVIEW",
              notes: "Latest",
              lockedAt: null,
              createdAt: new Date("2026-03-28T00:00:00.000Z"),
              updatedAt: new Date("2026-03-29T00:00:00.000Z"),
              _count: {
                assumptions: 2,
                scopeItems: 3,
                demandLines: 4,
                resourceSnapshots: 1,
                exports: 0,
              },
            },
          ],
        }),
      },
    };
    const controllerCtx = createToolContext(db, { userRole: SystemRole.CONTROLLER });
    const userCtx = createToolContext({}, { userRole: SystemRole.USER });

    const successResult = await executeTool(
      "list_estimate_versions",
      JSON.stringify({ estimateId: "est_1" }),
      controllerCtx,
    );
    const deniedResult = await executeTool(
      "list_estimate_versions",
      JSON.stringify({ estimateId: "est_1" }),
      userCtx,
    );

    expect(db.estimate.findUnique).toHaveBeenCalledWith({
      where: { id: "est_1" },
      select: expect.objectContaining({
        id: true,
        versions: expect.any(Object),
      }),
    });
    expect(JSON.parse(successResult.content)).toEqual(
      expect.objectContaining({
        id: "est_1",
        latestVersionNumber: 4,
        versions: [
          expect.objectContaining({
            id: "ver_4",
            versionNumber: 4,
          }),
        ],
      }),
    );
    expect(JSON.parse(deniedResult.content)).toEqual(
      expect.objectContaining({
        error: expect.stringContaining("Controller access required"),
      }),
    );
  });

  it("reads estimate version snapshots through the real estimate router, requires viewCosts, and rejects plain users", async () => {
    const db = {
      estimate: {
        findUnique: vi.fn().mockResolvedValue({
          id: "est_1",
          name: "North Cluster Estimate",
          status: "APPROVED",
          baseCurrency: "EUR",
          versions: [
            {
              id: "ver_4",
              versionNumber: 4,
              label: "Rev 4",
              status: "APPROVED",
              notes: "Latest",
              lockedAt: new Date("2026-03-29T00:00:00.000Z"),
              createdAt: new Date("2026-03-28T00:00:00.000Z"),
              updatedAt: new Date("2026-03-29T00:00:00.000Z"),
              assumptions: [
                { id: "ass_1", category: "DELIVERY", key: "onsite", label: "Onsite support" },
              ],
              scopeItems: [
                { id: "scope_1", scopeType: "EPIC", sequenceNo: 1, name: "Pipeline" },
              ],
              demandLines: [
                {
                  id: "dl_1",
                  name: "Modeling",
                  chapter: "3D",
                  hours: 40,
                  costTotalCents: 400000,
                  priceTotalCents: 600000,
                  currency: "EUR",
                },
              ],
              resourceSnapshots: [
                {
                  id: "snap_1",
                  displayName: "Alice",
                  chapter: "3D",
                  currency: "EUR",
                  lcrCents: 10000,
                  ucrCents: 15000,
                },
              ],
              exports: [
                {
                  id: "exp_1",
                  format: "XLSX",
                  fileName: "estimate.xlsx",
                  createdAt: new Date("2026-03-29T10:00:00.000Z"),
                },
              ],
            },
          ],
        }),
      },
    };
    const controllerCtx = createToolContext(db, {
      userRole: SystemRole.CONTROLLER,
      permissions: [PermissionKey.VIEW_COSTS],
    });
    const controllerWithoutCostsCtx = createToolContext(db, {
      userRole: SystemRole.CONTROLLER,
      permissions: [],
    });
    const userCtx = createToolContext({}, { userRole: SystemRole.USER });

    const successResult = await executeTool(
      "get_estimate_version_snapshot",
      JSON.stringify({ estimateId: "est_1", versionId: "ver_4" }),
      controllerCtx,
    );
    const missingPermissionResult = await executeTool(
      "get_estimate_version_snapshot",
      JSON.stringify({ estimateId: "est_1", versionId: "ver_4" }),
      controllerWithoutCostsCtx,
    );
    const deniedResult = await executeTool(
      "get_estimate_version_snapshot",
      JSON.stringify({ estimateId: "est_1", versionId: "ver_4" }),
      userCtx,
    );

    expect(db.estimate.findUnique).toHaveBeenCalledWith({
      where: { id: "est_1" },
      select: expect.objectContaining({
        id: true,
        versions: expect.objectContaining({
          where: { id: "ver_4" },
        }),
      }),
    });
    expect(JSON.parse(successResult.content)).toEqual(
      expect.objectContaining({
        estimate: expect.objectContaining({
          id: "est_1",
          baseCurrency: "EUR",
        }),
        version: expect.objectContaining({
          id: "ver_4",
          versionNumber: 4,
        }),
        totals: expect.objectContaining({
          hours: 40,
          costTotalCents: 400000,
          priceTotalCents: 600000,
        }),
        chapterBreakdown: [
          expect.objectContaining({
            chapter: "3D",
            lineCount: 1,
          }),
        ],
      }),
    );
    expect(JSON.parse(missingPermissionResult.content)).toEqual(
      expect.objectContaining({
        error: expect.stringContaining(PermissionKey.VIEW_COSTS),
      }),
    );
    expect(JSON.parse(deniedResult.content)).toEqual(
      expect.objectContaining({
        error: expect.stringContaining(PermissionKey.VIEW_COSTS),
      }),
    );
  });

  it("reads countries through the real country router identifier path", async () => {
    const db = {
      country: {
        findUnique: vi.fn().mockResolvedValue(null),
        findFirst: vi.fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce({
            id: "country_de",
            code: "DE",
            name: "Germany",
            dailyWorkingHours: 8,
            scheduleRules: null,
            isActive: true,
            metroCities: [{ id: "city_muc", name: "Munich" }],
            _count: { resources: 12 },
          }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool(
      "get_country",
      JSON.stringify({ identifier: "Germany" }),
      ctx,
    );

    expect(db.country.findUnique).toHaveBeenCalledWith({
      where: { id: "Germany" },
      include: {
        metroCities: { orderBy: { name: "asc" } },
        _count: { select: { resources: true } },
      },
    });
    expect(db.country.findFirst).toHaveBeenNthCalledWith(1, {
      where: { code: { equals: "GERMANY", mode: "insensitive" } },
      include: {
        metroCities: { orderBy: { name: "asc" } },
        _count: { select: { resources: true } },
      },
    });
    expect(db.country.findFirst).toHaveBeenNthCalledWith(2, {
      where: { name: { equals: "Germany", mode: "insensitive" } },
      include: {
        metroCities: { orderBy: { name: "asc" } },
        _count: { select: { resources: true } },
      },
    });
    expect(JSON.parse(result.content)).toEqual({
      id: "country_de",
      code: "DE",
      name: "Germany",
      dailyWorkingHours: 8,
      scheduleRules: null,
      isActive: true,
      resourceCount: 12,
      metroCities: [{ id: "city_muc", name: "Munich" }],
      cities: ["Munich"],
    });
  });

  it("creates and updates countries through the real country router path", async () => {
    const db = {
      country: {
        findUnique: vi.fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce({ id: "country_de", code: "DE", name: "Germany" }),
        create: vi.fn().mockResolvedValue({
          id: "country_de",
          code: "DE",
          name: "Germany",
          dailyWorkingHours: 8,
          scheduleRules: null,
          isActive: true,
          metroCities: [],
          _count: { resources: 0 },
        }),
        update: vi.fn().mockResolvedValue({
          id: "country_de",
          code: "DE",
          name: "Germany Updated",
          dailyWorkingHours: 7.5,
          scheduleRules: { shortFriday: true },
          isActive: false,
          metroCities: [],
          _count: { resources: 0 },
        }),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({ id: "audit_1" }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const createResult = await executeTool(
      "create_country",
      JSON.stringify({ code: "DE", name: "Germany", dailyWorkingHours: 8 }),
      ctx,
    );
    const updateResult = await executeTool(
      "update_country",
      JSON.stringify({
        id: "country_de",
        data: {
          name: "Germany Updated",
          dailyWorkingHours: 7.5,
          scheduleRules: null,
          isActive: false,
        },
      }),
      ctx,
    );

    expect(db.country.create).toHaveBeenCalledWith({
      data: {
        code: "DE",
        name: "Germany",
        dailyWorkingHours: 8,
      },
      include: { metroCities: true },
    });
    expect(db.country.update).toHaveBeenCalledWith({
      where: { id: "country_de" },
      data: {
        name: "Germany Updated",
        dailyWorkingHours: 7.5,
        scheduleRules: expect.anything(),
        isActive: false,
      },
      include: { metroCities: true },
    });
    expect(JSON.parse(createResult.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: "Created country: Germany",
        country: expect.objectContaining({
          id: "country_de",
          code: "DE",
        }),
      }),
    );
    expect(createResult.action).toEqual({
      type: "invalidate",
      scope: ["country", "resource", "holidayCalendar", "vacation"],
    });
    expect(JSON.parse(updateResult.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: "Updated country: Germany Updated",
        country: expect.objectContaining({
          id: "country_de",
          isActive: false,
        }),
      }),
    );
  });

  it("creates, updates, and deletes metro cities through the real country router path", async () => {
    const metroCityFindUnique = vi.fn()
      .mockResolvedValueOnce({ id: "city_muc", name: "Munich", countryId: "country_de" })
      .mockResolvedValueOnce({ id: "city_muc", name: "Munich", _count: { resources: 0 } });
    const db = {
      country: {
        findUnique: vi.fn().mockResolvedValue({ id: "country_de", name: "Germany" }),
      },
      metroCity: {
        create: vi.fn().mockResolvedValue({ id: "city_muc", name: "Munich", countryId: "country_de" }),
        findUnique: metroCityFindUnique,
        update: vi.fn().mockResolvedValue({ id: "city_muc", name: "Muenchen", countryId: "country_de" }),
        delete: vi.fn().mockResolvedValue({ id: "city_muc" }),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({ id: "audit_1" }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const createResult = await executeTool(
      "create_metro_city",
      JSON.stringify({ countryId: "country_de", name: "Munich" }),
      ctx,
    );
    const updateResult = await executeTool(
      "update_metro_city",
      JSON.stringify({ id: "city_muc", data: { name: "Muenchen" } }),
      ctx,
    );
    const deleteResult = await executeTool(
      "delete_metro_city",
      JSON.stringify({ id: "city_muc" }),
      ctx,
    );

    expect(db.metroCity.create).toHaveBeenCalledWith({
      data: { name: "Munich", countryId: "country_de" },
    });
    expect(db.metroCity.update).toHaveBeenCalledWith({
      where: { id: "city_muc" },
      data: { name: "Muenchen" },
    });
    expect(db.metroCity.delete).toHaveBeenCalledWith({ where: { id: "city_muc" } });
    expect(JSON.parse(createResult.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: "Created metro city: Munich",
        metroCity: expect.objectContaining({ id: "city_muc", name: "Munich" }),
      }),
    );
    expect(JSON.parse(updateResult.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: "Updated metro city: Muenchen",
        metroCity: expect.objectContaining({ id: "city_muc", name: "Muenchen" }),
      }),
    );
    expect(JSON.parse(deleteResult.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: "Deleted metro city: Munich",
      }),
    );
  });

  it("routes role, client, and org unit mutations through their backing routers", async () => {
    const db = {
      role: {
        findUnique: vi.fn().mockImplementation(async (args?: any) => {
          if (args?.where?.name === "Pipeline TD") return null;
          if (args?.where?.name === "Pipeline Lead") return null;
          if (args?.where?.id === "role_pipeline") {
            return {
              id: "role_pipeline",
              name: "Pipeline TD",
              description: "Pipeline craft",
              color: "#123456",
              isActive: true,
              _count: { resourceRoles: 0 },
              resourceRoles: [],
            };
          }
          return null;
        }),
        create: vi.fn().mockResolvedValue({
          id: "role_pipeline",
          name: "Pipeline TD",
          description: "Pipeline craft",
          color: "#123456",
          isActive: true,
          _count: { resourceRoles: 0 },
        }),
        update: vi.fn().mockResolvedValue({
          id: "role_pipeline",
          name: "Pipeline Lead",
          description: "Lead pipeline craft",
          color: "#654321",
          isActive: false,
          _count: { resourceRoles: 0 },
        }),
        delete: vi.fn().mockResolvedValue({ id: "role_pipeline" }),
      },
      client: {
        findUnique: vi.fn().mockImplementation(async (args?: any) => {
          if (args?.where?.id === "client_1") {
            return {
              id: "client_1",
              name: "Acme",
              code: "ACME",
              parentId: null,
              isActive: true,
              sortOrder: 0,
              tags: [],
            };
          }
          if (args?.where?.code === "ACME-NEW") return null;
          return null;
        }),
        create: vi.fn().mockResolvedValue({
          id: "client_1",
          name: "Acme",
          code: "ACME",
          parentId: null,
          isActive: true,
          sortOrder: 2,
          tags: ["key"],
        }),
        update: vi.fn().mockResolvedValue({
          id: "client_1",
          name: "Acme Updated",
          code: "ACME-NEW",
          parentId: null,
          isActive: false,
          sortOrder: 3,
          tags: ["vip"],
        }),
      },
      orgUnit: {
        findUnique: vi.fn().mockImplementation(async (args?: any) => {
          if (args?.where?.id === "ou_1") {
            return {
              id: "ou_1",
              name: "Operations",
              shortName: "OPS",
              level: 5,
              parentId: null,
              sortOrder: 1,
              isActive: true,
            };
          }
          return null;
        }),
        create: vi.fn().mockResolvedValue({
          id: "ou_1",
          name: "Operations",
          shortName: "OPS",
          level: 5,
          parentId: null,
          sortOrder: 1,
          isActive: true,
        }),
        update: vi.fn().mockResolvedValue({
          id: "ou_1",
          name: "Operations EU",
          shortName: null,
          level: 5,
          parentId: null,
          sortOrder: 4,
          isActive: false,
        }),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({ id: "audit_1" }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const createRoleResult = await executeTool(
      "create_role",
      JSON.stringify({ name: "Pipeline TD", description: "Pipeline craft", color: "#123456" }),
      ctx,
    );
    const updateRoleResult = await executeTool(
      "update_role",
      JSON.stringify({ id: "role_pipeline", name: "Pipeline Lead", description: "Lead pipeline craft", color: "#654321", isActive: false }),
      ctx,
    );
    const deleteRoleResult = await executeTool(
      "delete_role",
      JSON.stringify({ id: "role_pipeline" }),
      ctx,
    );
    const createClientResult = await executeTool(
      "create_client",
      JSON.stringify({ name: "Acme", code: "ACME", sortOrder: 2, tags: ["key"] }),
      ctx,
    );
    const updateClientResult = await executeTool(
      "update_client",
      JSON.stringify({ id: "client_1", name: "Acme Updated", code: "ACME-NEW", sortOrder: 3, isActive: false, tags: ["vip"] }),
      ctx,
    );
    const createOrgUnitResult = await executeTool(
      "create_org_unit",
      JSON.stringify({ name: "Operations", shortName: "OPS", level: 5, sortOrder: 1 }),
      ctx,
    );
    const updateOrgUnitResult = await executeTool(
      "update_org_unit",
      JSON.stringify({ id: "ou_1", name: "Operations EU", shortName: null, sortOrder: 4, isActive: false }),
      ctx,
    );

    expect(JSON.parse(createRoleResult.content)).toEqual(expect.objectContaining({
      success: true,
      roleId: "role_pipeline",
      message: "Created role: Pipeline TD",
    }));
    expect(JSON.parse(updateRoleResult.content)).toEqual(expect.objectContaining({
      success: true,
      roleId: "role_pipeline",
      message: "Updated role: Pipeline Lead",
    }));
    expect(JSON.parse(deleteRoleResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Deleted role: Pipeline TD",
    }));
    expect(JSON.parse(createClientResult.content)).toEqual(expect.objectContaining({
      success: true,
      clientId: "client_1",
      message: "Created client: Acme",
    }));
    expect(JSON.parse(updateClientResult.content)).toEqual(expect.objectContaining({
      success: true,
      clientId: "client_1",
      message: "Updated client: Acme Updated",
    }));
    expect(JSON.parse(createOrgUnitResult.content)).toEqual(expect.objectContaining({
      success: true,
      orgUnitId: "ou_1",
      message: "Created org unit: Operations",
    }));
    expect(JSON.parse(updateOrgUnitResult.content)).toEqual(expect.objectContaining({
      success: true,
      orgUnitId: "ou_1",
      message: "Updated org unit: Operations EU",
    }));

    expect(db.role.create).toHaveBeenCalled();
    expect(db.role.update).toHaveBeenCalled();
    expect(db.role.delete).toHaveBeenCalledWith({ where: { id: "role_pipeline" } });
    expect(db.client.create).toHaveBeenCalled();
    expect(db.client.update).toHaveBeenCalled();
    expect(db.orgUnit.create).toHaveBeenCalled();
    expect(db.orgUnit.update).toHaveBeenCalled();
    expect(db.auditLog.create).toHaveBeenCalled();
  });

  it("routes vacation balance, requests, approvals, rejections, and entitlements through the real routers", async () => {
    const vacationFindMany = vi.fn().mockImplementation(async (args?: any) => {
      if (args?.where?.type === VacationType.PUBLIC_HOLIDAY) {
        return [];
      }
      if (args?.where?.type?.in) {
        return [
          {
            startDate: new Date("2026-01-05T00:00:00.000Z"),
            endDate: new Date("2026-01-06T00:00:00.000Z"),
            status: "APPROVED",
            isHalfDay: false,
          },
          {
            startDate: new Date("2026-02-03T00:00:00.000Z"),
            endDate: new Date("2026-02-03T00:00:00.000Z"),
            status: "PENDING",
            isHalfDay: true,
          },
        ];
      }
      if (args?.where?.type === VacationType.SICK) {
        return [
          {
            startDate: new Date("2026-03-10T00:00:00.000Z"),
            endDate: new Date("2026-03-10T00:00:00.000Z"),
            isHalfDay: false,
          },
        ];
      }
      if (args?.where?.resource?.chapter) {
        return [];
      }
      return [];
    });
    const vacationFindUnique = vi.fn().mockImplementation(async (args?: any) => {
      const id = args?.where?.id;
      if (id === "vac_cancelled") {
        return {
          id,
          resourceId: "res_1",
          requestedById: "user_1",
          status: "CANCELLED",
          startDate: new Date("2026-07-01T00:00:00.000Z"),
          endDate: new Date("2026-07-02T00:00:00.000Z"),
          type: VacationType.ANNUAL,
          isHalfDay: false,
          resource: {
            id: "res_1",
            displayName: "Alice Example",
            eid: "EMP-001",
            chapter: "Delivery",
          },
          requestedBy: { id: "user_1", name: "Assistant User", email: "assistant@example.com" },
          approvedBy: null,
        };
      }
      if (id === "vac_pending") {
        return {
          id,
          resourceId: "res_1",
          requestedById: "user_1",
          status: "PENDING",
          startDate: new Date("2026-08-03T00:00:00.000Z"),
          endDate: new Date("2026-08-04T00:00:00.000Z"),
          type: VacationType.ANNUAL,
          isHalfDay: false,
          resource: {
            id: "res_1",
            displayName: "Alice Example",
            eid: "EMP-001",
            chapter: "Delivery",
          },
          requestedBy: { id: "user_1", name: "Assistant User", email: "assistant@example.com" },
          approvedBy: null,
        };
      }
      return null;
    });
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1", systemRole: "MANAGER" }),
      },
      resource: {
        findUnique: vi.fn().mockImplementation(async (args?: any) => {
          if (args?.where?.id === "res_1") {
            return {
              id: "res_1",
              eid: "EMP-001",
              displayName: "Alice Example",
              userId: "user_1",
              chapter: "Delivery",
              federalState: "BY",
              countryId: "country_de",
              metroCityId: null,
              country: { code: "DE", name: "Germany" },
              metroCity: null,
            };
          }
          return null;
        }),
        count: vi.fn().mockResolvedValue(1),
      },
      holidayCalendar: {
        findMany: vi.fn().mockResolvedValue([]),
      },
      systemSettings: {
        findUnique: vi.fn().mockResolvedValue({ vacationDefaultDays: 30 }),
      },
      vacationEntitlement: {
        findUnique: vi.fn().mockResolvedValue(null),
        create: vi.fn().mockImplementation(async (args?: any) => ({
          id: `ent_${args?.data?.year ?? "unknown"}`,
          resourceId: args?.data?.resourceId ?? "res_1",
          year: args?.data?.year ?? 2026,
          entitledDays: args?.data?.entitledDays ?? 30,
          carryoverDays: args?.data?.carryoverDays ?? 0,
          usedDays: args?.data?.usedDays ?? 0,
          pendingDays: args?.data?.pendingDays ?? 0,
        })),
        update: vi.fn().mockImplementation(async (args?: any) => ({
          id: args?.where?.id ?? "ent_2026",
          resourceId: "res_1",
          year: 2026,
          entitledDays: 30,
          carryoverDays: 0,
          usedDays: args?.data?.usedDays ?? 0,
          pendingDays: args?.data?.pendingDays ?? 0,
        })),
      },
      vacation: {
        findUnique: vacationFindUnique,
        findMany: vacationFindMany,
        findFirst: vi.fn().mockResolvedValue(null),
        create: vi.fn().mockResolvedValue({
          id: "vac_created",
          resourceId: "res_1",
          status: "APPROVED",
          type: VacationType.ANNUAL,
          startDate: new Date("2026-07-01T00:00:00.000Z"),
          endDate: new Date("2026-07-02T00:00:00.000Z"),
          isHalfDay: false,
          resource: {
            id: "res_1",
            displayName: "Alice Example",
            eid: "EMP-001",
            chapter: "Delivery",
          },
          requestedBy: { id: "user_1", name: "Assistant User", email: "assistant@example.com" },
          effectiveDays: 2,
        }),
        update: vi.fn().mockImplementation(async (args?: any) => ({
          id: args?.where?.id ?? "vac_unknown",
          resourceId: "res_1",
          startDate: args?.where?.id === "vac_pending"
            ? new Date("2026-08-03T00:00:00.000Z")
            : new Date("2026-07-01T00:00:00.000Z"),
          endDate: args?.where?.id === "vac_pending"
            ? new Date("2026-08-04T00:00:00.000Z")
            : new Date("2026-07-02T00:00:00.000Z"),
          type: VacationType.ANNUAL,
          isHalfDay: false,
          status: args?.data?.status ?? "APPROVED",
          rejectionReason: args?.data?.rejectionReason ?? null,
          approvedById: args?.data?.approvedById ?? null,
          approvedAt: args?.data?.approvedAt ?? null,
        })),
      },
      notification: {
        updateMany: vi.fn().mockResolvedValue({ count: 1 }),
        create: vi.fn().mockResolvedValue({ id: "note_1", userId: "user_1" }),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({ id: "audit_1" }),
      },
      webhook: {
        findMany: vi.fn().mockResolvedValue([]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.MANAGER });

    const balanceResult = await executeTool(
      "get_vacation_balance",
      JSON.stringify({ resourceId: "res_1", year: 2026 }),
      ctx,
    );
    const createResult = await executeTool(
      "create_vacation",
      JSON.stringify({
        resourceId: "res_1",
        type: "ANNUAL",
        startDate: "2026-07-01",
        endDate: "2026-07-02",
      }),
      ctx,
    );
    const approveResult = await executeTool(
      "approve_vacation",
      JSON.stringify({ vacationId: "vac_cancelled" }),
      ctx,
    );
    const rejectResult = await executeTool(
      "reject_vacation",
      JSON.stringify({ vacationId: "vac_pending", reason: "Capacity freeze" }),
      ctx,
    );
    const setEntitlementResult = await executeTool(
      "set_entitlement",
      JSON.stringify({ resourceId: "res_1", year: 2027, entitledDays: 32 }),
      ctx,
    );

    expect(JSON.parse(balanceResult.content)).toEqual({
      resource: "Alice Example",
      eid: "EMP-001",
      year: 2026,
      entitlement: 30,
      carryOver: 0,
      taken: 1,
      pending: 0.5,
      remaining: 28.5,
      sickDays: 1,
    });
    expect(JSON.parse(createResult.content)).toEqual(expect.objectContaining({
      success: true,
      vacationId: "vac_created",
      message: "Created ANNUAL for Alice Example: 2026-07-01 to 2026-07-02 (status: APPROVED, deducted 2 day(s))",
    }));
    expect(JSON.parse(approveResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Approved vacation for Alice Example",
    }));
    expect(JSON.parse(rejectResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Rejected vacation for Alice Example: Capacity freeze",
    }));
    expect(JSON.parse(setEntitlementResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Set entitlement for Alice Example (2027): 32 days",
    }));

    expect(db.vacation.create).toHaveBeenCalledWith(expect.objectContaining({
      data: expect.objectContaining({
        resourceId: "res_1",
        type: VacationType.ANNUAL,
        status: "APPROVED",
        requestedById: "user_1",
        approvedById: "user_1",
      }),
    }));
    expect(db.vacation.update).toHaveBeenCalledWith(expect.objectContaining({
      where: { id: "vac_cancelled" },
      data: expect.objectContaining({ status: "APPROVED" }),
    }));
    expect(db.vacation.update).toHaveBeenCalledWith(expect.objectContaining({
      where: { id: "vac_pending" },
      data: expect.objectContaining({ status: "REJECTED", rejectionReason: "Capacity freeze" }),
    }));
    expect(db.vacationEntitlement.create).toHaveBeenCalledWith(expect.objectContaining({
      data: expect.objectContaining({
        resourceId: "res_1",
        year: 2027,
        entitledDays: 32,
      }),
    }));
  });

  it("routes entitlement summary through the entitlement year summary workflow", async () => {
    const db = {
      systemSettings: {
        findUnique: vi.fn().mockResolvedValue({ vacationDefaultDays: 28 }),
      },
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_1",
            displayName: "Alice Example",
            eid: "EMP-001",
            lcrCents: 0,
            chapter: "Delivery",
          },
          {
            id: "res_2",
            displayName: "Bob Example",
            eid: "EMP-002",
            lcrCents: 0,
            chapter: "CGI",
          },
        ]),
      },
      vacationEntitlement: {
        findUnique: vi.fn().mockResolvedValue(null),
        create: vi.fn().mockImplementation(async (args?: any) => ({
          id: `ent_${args?.data?.resourceId ?? "unknown"}_${args?.data?.year ?? "unknown"}`,
          resourceId: args?.data?.resourceId ?? "res_1",
          year: args?.data?.year ?? 2026,
          entitledDays: args?.data?.entitledDays ?? 28,
          carryoverDays: args?.data?.carryoverDays ?? 0,
          usedDays: args?.data?.usedDays ?? 0,
          pendingDays: args?.data?.pendingDays ?? 0,
        })),
        update: vi.fn().mockImplementation(async (args?: any) => ({
          id: args?.where?.id ?? "ent_unknown",
          resourceId: args?.where?.id?.includes("res_2") ? "res_2" : "res_1",
          year: 2026,
          entitledDays: 28,
          carryoverDays: 0,
          usedDays: args?.data?.usedDays ?? 0,
          pendingDays: args?.data?.pendingDays ?? 0,
        })),
      },
      vacation: {
        findMany: vi.fn().mockResolvedValue([]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.MANAGER });

    const result = await executeTool(
      "get_entitlement_summary",
      JSON.stringify({ year: 2026, resourceName: "alice" }),
      ctx,
    );

    expect(db.resource.findMany).toHaveBeenCalledWith({
      where: { isActive: true },
      select: { id: true, displayName: true, eid: true, lcrCents: true, chapter: true },
      orderBy: [{ chapter: "asc" }, { displayName: "asc" }],
    });
    expect(JSON.parse(result.content)).toEqual([
      {
        resource: "Alice Example",
        eid: "EMP-001",
        chapter: "Delivery",
        year: 2026,
        entitled: 28,
        carryover: 0,
        used: 0,
        pending: 0,
        remaining: 28,
      },
    ]);
  });

  it("allows self-service vacation cancellation through the real vacation router path", async () => {
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1", systemRole: "USER" }),
      },
      vacation: {
        findUnique: vi.fn().mockResolvedValue({
          id: "vac_self",
          resourceId: "res_1",
          requestedById: "user_1",
          status: "APPROVED",
          startDate: new Date("2026-09-07T00:00:00.000Z"),
          endDate: new Date("2026-09-09T00:00:00.000Z"),
          resource: {
            id: "res_1",
            displayName: "Alice Example",
            eid: "EMP-001",
            chapter: "Delivery",
          },
          requestedBy: { id: "user_1", name: "Assistant User", email: "assistant@example.com" },
          approvedBy: null,
        }),
        update: vi.fn().mockResolvedValue({
          id: "vac_self",
          resourceId: "res_1",
          status: "CANCELLED",
        }),
      },
      resource: {
        findUnique: vi.fn().mockResolvedValue({ userId: "user_1" }),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({ id: "audit_1" }),
      },
      systemSettings: {
        findUnique: vi.fn().mockResolvedValue({ anonymizationEnabled: false }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.USER, permissions: [] });

    const result = await executeTool(
      "cancel_vacation",
      JSON.stringify({ vacationId: "vac_self" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Cancelled vacation for Alice Example",
    }));
    expect(db.vacation.update).toHaveBeenCalledWith({
      where: { id: "vac_self" },
      data: { status: "CANCELLED" },
    });
  });

  it("routes comment listing, creation, and resolution through the real comment router path", async () => {
    const commentFindUnique = vi.fn().mockResolvedValue({
      id: "comment_1",
      authorId: "user_1",
    });
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1", systemRole: "USER" }),
      },
      comment: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "comment_1",
            body: "Initial note",
            resolved: false,
            createdAt: new Date("2026-03-29T09:00:00.000Z"),
            author: { id: "user_1", name: "Assistant User", email: "assistant@example.com", image: null },
            replies: [
              {
                id: "comment_reply_1",
                body: "Reply",
                resolved: false,
                createdAt: new Date("2026-03-29T10:00:00.000Z"),
                author: { id: "user_2", name: "Reviewer", email: "reviewer@example.com", image: null },
              },
            ],
          },
        ]),
        findUnique: commentFindUnique,
        create: vi.fn().mockResolvedValue({
          id: "comment_created",
          body: "Please review this estimate.",
          resolved: false,
          createdAt: new Date("2026-03-29T11:00:00.000Z"),
          author: { id: "user_1", name: "Assistant User", email: "assistant@example.com", image: null },
        }),
        update: vi.fn().mockResolvedValue({
          id: "comment_1",
          body: "Initial note",
          resolved: true,
          createdAt: new Date("2026-03-29T09:00:00.000Z"),
          author: { id: "user_1", name: "Assistant User", email: "assistant@example.com", image: null },
        }),
      },
      notification: {
        create: vi.fn(),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({ id: "audit_1" }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const listResult = await executeTool(
      "list_comments",
      JSON.stringify({ entityType: "estimate", entityId: "est_1" }),
      ctx,
    );
    const createResult = await executeTool(
      "create_comment",
      JSON.stringify({
        entityType: "estimate",
        entityId: "est_1",
        body: "Please review this estimate.",
      }),
      ctx,
    );
    const resolveResult = await executeTool(
      "resolve_comment",
      JSON.stringify({ commentId: "comment_1", resolved: true }),
      ctx,
    );

    expect(db.comment.findMany).toHaveBeenCalledWith({
      where: {
        entityType: "estimate",
        entityId: "est_1",
        parentId: null,
      },
      include: {
        author: { select: { id: true, name: true, email: true, image: true } },
        replies: {
          include: {
            author: { select: { id: true, name: true, email: true, image: true } },
          },
          orderBy: { createdAt: "asc" },
        },
      },
      orderBy: { createdAt: "asc" },
    });
    expect(db.user.findUnique).toHaveBeenCalledWith({
      where: { email: "assistant@example.com" },
      select: { id: true },
    });
    expect(db.comment.create).toHaveBeenCalledWith({
      data: {
        entityType: "estimate",
        entityId: "est_1",
        authorId: "user_1",
        body: "Please review this estimate.",
        mentions: [],
      },
      include: {
        author: { select: { id: true, name: true, email: true, image: true } },
      },
    });
    expect(commentFindUnique).toHaveBeenCalledWith({
      where: { id: "comment_1" },
      select: { id: true, authorId: true },
    });
    expect(db.comment.update).toHaveBeenCalledWith({
      where: { id: "comment_1" },
      data: { resolved: true },
      include: {
        author: { select: { id: true, name: true, email: true, image: true } },
      },
    });

    expect(JSON.parse(listResult.content)).toEqual([
      {
        id: "comment_1",
        author: "Assistant User",
        body: "Initial note",
        resolved: false,
        createdAt: "2026-03-29T09:00:00.000Z",
        replyCount: 1,
        replies: [
          {
            id: "comment_reply_1",
            author: "Reviewer",
            body: "Reply",
            resolved: false,
            createdAt: "2026-03-29T10:00:00.000Z",
          },
        ],
      },
    ]);
    expect(JSON.parse(createResult.content)).toEqual({
      id: "comment_created",
      author: "Assistant User",
      body: "Please review this estimate.",
      createdAt: "2026-03-29T11:00:00.000Z",
    });
    expect(createResult.action).toEqual({
      type: "invalidate",
      scope: ["comment"],
    });
    expect(JSON.parse(resolveResult.content)).toEqual({
      id: "comment_1",
      resolved: true,
      author: "Assistant User",
      body: "Initial note",
    });
    expect(resolveResult.action).toEqual({
      type: "invalidate",
      scope: ["comment"],
    });
  });

  it("routes taxonomy and rule read tools through their backing routers", async () => {
    const db = {
      managementLevelGroup: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "mlg_exec",
            name: "Executive",
            targetPercentage: 75,
            levels: [{ id: "ml_partner", name: "Partner" }],
          },
        ]),
      },
      utilizationCategory: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "util_billable",
            code: "BILLABLE",
            name: "Billable",
            description: "Client work",
            isActive: true,
            sortOrder: 1,
          },
        ]),
        findUnique: vi.fn().mockResolvedValue({
          id: "util_billable",
          code: "BILLABLE",
          name: "Billable",
          description: "Client work",
          isActive: true,
          sortOrder: 1,
          _count: { projects: 3 },
        }),
      },
      calculationRule: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "calc_1",
            name: "Rush Fee",
            description: "Adds cost for rush work",
            isActive: true,
            triggerType: "RUSH",
            orderType: null,
            costEffect: "INCREASE",
            costReductionPercent: null,
            chargeabilityEffect: "NONE",
            priority: 90,
            project: { id: "proj_1", name: "Falcon", shortCode: "FAL" },
          },
        ]),
      },
      effortRuleSet: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "ers_default",
            name: "Default Effort",
            isDefault: true,
            rules: [
              {
                id: "eff_1",
                description: "Animation per shot",
                scopeType: "SHOT",
                discipline: "Animation",
                chapter: "3D",
                unitMode: "per_item",
                hoursPerUnit: 12,
                sortOrder: 0,
              },
            ],
          },
        ]),
      },
      experienceMultiplierSet: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "ems_default",
            name: "Default Multipliers",
            isDefault: true,
            rules: [
              {
                id: "exp_1",
                description: "Senior DE uplift",
                chapter: "3D",
                location: "DE",
                level: "Senior",
                costMultiplier: 1.2,
                billMultiplier: 1.15,
                shoringRatio: 0.4,
                additionalEffortRatio: 0.1,
                sortOrder: 0,
              },
            ],
          },
        ]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const managementLevelsResult = await executeTool("list_management_levels", "{}", ctx);
    const utilizationCategoriesResult = await executeTool("list_utilization_categories", "{}", ctx);
    const calculationRulesResult = await executeTool("list_calculation_rules", "{}", ctx);
    const effortRulesResult = await executeTool("list_effort_rules", "{}", ctx);
    const experienceMultipliersResult = await executeTool("list_experience_multipliers", "{}", ctx);

    expect(db.managementLevelGroup.findMany).toHaveBeenCalledWith({
      include: { levels: { orderBy: { name: "asc" } } },
      orderBy: { sortOrder: "asc" },
    });
    expect(db.utilizationCategory.findMany).toHaveBeenCalledWith({
      where: {},
      orderBy: { sortOrder: "asc" },
    });
    expect(db.utilizationCategory.findUnique).toHaveBeenCalledWith({
      where: { id: "util_billable" },
      include: { _count: { select: { projects: true } } },
    });
    expect(db.calculationRule.findMany).toHaveBeenCalledWith({
      orderBy: [{ priority: "desc" }, { name: "asc" }],
      include: {
        project: {
          select: {
            id: true,
            shortCode: true,
            name: true,
            status: true,
            endDate: true,
          },
        },
      },
    });
    expect(db.effortRuleSet.findMany).toHaveBeenCalledWith({
      include: {
        rules: { orderBy: { sortOrder: "asc" } },
      },
      orderBy: [{ isDefault: "desc" }, { name: "asc" }],
    });
    expect(db.experienceMultiplierSet.findMany).toHaveBeenCalledWith({
      include: {
        rules: { orderBy: { sortOrder: "asc" } },
      },
      orderBy: [{ isDefault: "desc" }, { name: "asc" }],
    });

    expect(JSON.parse(managementLevelsResult.content)).toEqual([
      {
        id: "mlg_exec",
        name: "Executive",
        target: "75%",
        levels: [{ id: "ml_partner", name: "Partner" }],
      },
    ]);
    expect(JSON.parse(utilizationCategoriesResult.content)).toEqual([
      {
        id: "util_billable",
        code: "BILLABLE",
        name: "Billable",
        description: "Client work",
        projectCount: 3,
      },
    ]);
    expect(JSON.parse(calculationRulesResult.content)).toEqual([
      expect.objectContaining({
        id: "calc_1",
        name: "Rush Fee",
        project: expect.objectContaining({
          id: "proj_1",
          shortCode: "FAL",
        }),
      }),
    ]);
    expect(JSON.parse(effortRulesResult.content)).toEqual([
      {
        id: "eff_1",
        description: "Animation per shot",
        scopeType: "SHOT",
        discipline: "Animation",
        chapter: "3D",
        unitMode: "per_item",
        hoursPerUnit: 12,
        sortOrder: 0,
        ruleSet: { name: "Default Effort", isDefault: true },
      },
    ]);
    expect(JSON.parse(experienceMultipliersResult.content)).toEqual([
      {
        id: "exp_1",
        description: "Senior DE uplift",
        chapter: "3D",
        location: "DE",
        level: "Senior",
        costMultiplier: 1.2,
        billMultiplier: 1.15,
        shoringRatio: 0.4,
        additionalEffortRatio: 0.1,
        sortOrder: 0,
        multiplierSet: { name: "Default Multipliers", isDefault: true },
      },
    ]);
  });

  it("lists and reads holiday calendars through the real holiday router paths", async () => {
    const db = {
      holidayCalendar: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "cal_de",
            name: "Germany National",
            scopeType: "COUNTRY",
            stateCode: null,
            isActive: true,
            priority: 0,
            country: { id: "country_de", code: "DE", name: "Germany" },
            metroCity: null,
            _count: { entries: 1 },
            entries: [
              {
                id: "entry_1",
                date: new Date("2026-01-01T00:00:00.000Z"),
                name: "New Year",
                isRecurringAnnual: true,
                source: "seed",
              },
            ],
          },
        ]),
        findUnique: vi.fn().mockResolvedValue(null),
        findFirst: vi.fn()
          .mockResolvedValueOnce({
            id: "cal_de",
            name: "Germany National",
            scopeType: "COUNTRY",
            stateCode: null,
            isActive: true,
            priority: 0,
            country: { id: "country_de", code: "DE", name: "Germany" },
            metroCity: null,
            entries: [
              {
                id: "entry_1",
                date: new Date("2026-01-01T00:00:00.000Z"),
                name: "New Year",
                isRecurringAnnual: true,
                source: "seed",
              },
            ],
          }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.MANAGER });

    const listResult = await executeTool(
      "list_holiday_calendars",
      JSON.stringify({ countryCode: "de", scopeType: "COUNTRY" }),
      ctx,
    );
    const getResult = await executeTool(
      "get_holiday_calendar",
      JSON.stringify({ identifier: "Germany National" }),
      ctx,
    );

    expect(db.holidayCalendar.findMany).toHaveBeenCalledWith({
      where: {
        isActive: true,
        country: { code: { equals: "DE", mode: "insensitive" } },
        scopeType: "COUNTRY",
      },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        _count: { select: { entries: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
      orderBy: [
        { country: { name: "asc" } },
        { scopeType: "asc" },
        { priority: "desc" },
        { name: "asc" },
      ],
    });
    expect(db.holidayCalendar.findUnique).toHaveBeenCalledWith({
      where: { id: "Germany National" },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
    });
    expect(db.holidayCalendar.findFirst).toHaveBeenCalledWith({
      where: { name: { equals: "Germany National", mode: "insensitive" } },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
    });
    expect(JSON.parse(listResult.content)).toEqual({
      count: 1,
      calendars: [
        expect.objectContaining({
          id: "cal_de",
          name: "Germany National",
          entryCount: 1,
        }),
      ],
    });
    expect(JSON.parse(getResult.content)).toEqual(
      expect.objectContaining({
        id: "cal_de",
        name: "Germany National",
      }),
    );
  });

  it("previews resolved holiday calendars through the real holiday router path", async () => {
    const holidayCallerDb = {
      country: {
        findUnique: vi.fn().mockResolvedValue({ id: "country_de", code: "DE", name: "Germany" }),
      },
      metroCity: {
        findUnique: vi.fn().mockResolvedValue({ id: "city_muc", name: "Munich", countryId: "country_de" }),
      },
      holidayCalendar: {
        findMany: vi.fn().mockResolvedValue([]),
      },
    };
    const ctx = createToolContext(holidayCallerDb, { userRole: SystemRole.USER });

    const result = await executeTool(
      "preview_resolved_holiday_calendar",
      JSON.stringify({
        countryId: "country_de",
        stateCode: "BY",
        metroCityId: "city_muc",
        year: 2026,
      }),
      ctx,
    );

    expect(holidayCallerDb.country.findUnique).toHaveBeenCalledWith({
      where: { id: "country_de" },
      select: { id: true, code: true, name: true },
    });
    expect(holidayCallerDb.metroCity.findUnique).toHaveBeenCalledWith({
      where: { id: "city_muc" },
      select: { id: true, name: true, countryId: true },
    });
    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      count: expect.any(Number),
      locationContext: {
        countryId: "country_de",
        countryCode: "DE",
        stateCode: "BY",
        metroCityId: "city_muc",
        metroCity: "Munich",
        year: 2026,
      },
      summary: expect.objectContaining({
        byCalendar: expect.any(Array),
        byScope: expect.any(Array),
        bySourceType: expect.arrayContaining([
          expect.objectContaining({ sourceType: "BUILTIN" }),
        ]),
      }),
      holidays: expect.arrayContaining([
        expect.objectContaining({
          date: "2026-01-01",
          name: "Neujahr",
          scope: "COUNTRY",
          sourceType: "BUILTIN",
        }),
        expect.objectContaining({
          date: "2026-01-06",
          name: "Heilige Drei Könige",
          scope: "STATE",
          sourceType: "BUILTIN",
        }),
      ]),
    }));
  });

  it("routes role, client, and org unit reads through their backing routers", async () => {
    const db = {
      role: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "role_anim",
            name: "Animation",
            color: "#112233",
            _count: { resourceRoles: 2 },
          },
        ]),
      },
      client: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "client_1",
            name: "Acme Mobility",
            code: "ACM",
            parentId: null,
            isActive: true,
            sortOrder: 1,
            tags: [],
            createdAt: new Date("2026-01-01T00:00:00.000Z"),
            updatedAt: new Date("2026-01-02T00:00:00.000Z"),
            _count: { children: 0, projects: 4 },
          },
        ]),
      },
      orgUnit: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "ou_delivery",
            name: "Delivery",
            shortName: "DEL",
            level: 5,
            parentId: null,
            sortOrder: 1,
            isActive: true,
            createdAt: new Date("2026-01-01T00:00:00.000Z"),
            updatedAt: new Date("2026-01-02T00:00:00.000Z"),
          },
        ]),
        findUnique: vi.fn().mockResolvedValue({
          id: "ou_delivery",
          name: "Delivery",
          shortName: "DEL",
          level: 5,
          parentId: null,
          sortOrder: 1,
          isActive: true,
          createdAt: new Date("2026-01-01T00:00:00.000Z"),
          updatedAt: new Date("2026-01-02T00:00:00.000Z"),
          parent: null,
          children: [],
          _count: { resources: 7 },
        }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const rolesResult = await executeTool("list_roles", "{}", ctx);
    const clientsResult = await executeTool(
      "list_clients",
      JSON.stringify({ query: "ACM", limit: 5 }),
      ctx,
    );
    const orgUnitsResult = await executeTool(
      "list_org_units",
      JSON.stringify({ level: 5 }),
      ctx,
    );

    expect(db.role.findMany).toHaveBeenCalledWith({
      where: {},
      include: {
        _count: {
          select: { resourceRoles: true },
        },
      },
      orderBy: { name: "asc" },
    });
    expect(db.client.findMany).toHaveBeenCalledWith({
      where: {
        isActive: true,
        OR: [
          { name: { contains: "ACM", mode: "insensitive" } },
          { code: { contains: "ACM", mode: "insensitive" } },
        ],
      },
      include: { _count: { select: { children: true, projects: true } } },
      orderBy: [{ sortOrder: "asc" }, { name: "asc" }],
    });
    expect(db.orgUnit.findMany).toHaveBeenCalledWith({
      where: {
        level: 5,
        isActive: true,
      },
      orderBy: [{ level: "asc" }, { sortOrder: "asc" }, { name: "asc" }],
    });
    expect(db.orgUnit.findUnique).toHaveBeenCalledWith({
      where: { id: "ou_delivery" },
      include: {
        parent: true,
        children: { orderBy: { sortOrder: "asc" } },
        _count: { select: { resources: true } },
      },
    });

    expect(JSON.parse(rolesResult.content)).toEqual([
      {
        id: "role_anim",
        name: "Animation",
        color: "#112233",
      },
    ]);
    expect(JSON.parse(clientsResult.content)).toEqual([
      {
        id: "client_1",
        name: "Acme Mobility",
        code: "ACM",
        projectCount: 4,
      },
    ]);
    expect(JSON.parse(orgUnitsResult.content)).toEqual([
      {
        id: "ou_delivery",
        name: "Delivery",
        shortName: "DEL",
        level: 5,
        parent: null,
        resourceCount: 7,
      },
    ]);
  });

  it("routes blueprint and rate card reads through their backing routers", async () => {
    const db = {
      blueprint: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "bp_project",
            name: "Project Default",
            _count: { projects: 3 },
          },
        ]),
        findUnique: vi.fn().mockResolvedValue({
          id: "bp_project",
          name: "Project Default",
          fieldDefs: [{ key: "market", type: "text" }],
          rolePresets: [{ role: "Consulting", share: 0.5 }],
        }),
      },
      rateCard: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "rc_2026",
            name: "Standard 2026",
            effectiveFrom: new Date("2026-01-01T00:00:00.000Z"),
            effectiveTo: null,
            _count: { lines: 12 },
            client: null,
          },
        ]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const blueprintsResult = await executeTool("list_blueprints", "{}", ctx);
    const blueprintResult = await executeTool(
      "get_blueprint",
      JSON.stringify({ identifier: "bp_project" }),
      ctx,
    );
    const rateCardsResult = await executeTool(
      "list_rate_cards",
      JSON.stringify({ query: "Standard", limit: 10 }),
      ctx,
    );

    expect(db.blueprint.findMany).toHaveBeenCalledWith({
      select: {
        id: true,
        name: true,
        _count: { select: { projects: true } },
      },
      orderBy: { name: "asc" },
    });
    expect(db.blueprint.findUnique).toHaveBeenCalledWith({
      where: { id: "bp_project" },
    });
    expect(db.rateCard.findMany).toHaveBeenCalledWith({
      where: {
        isActive: true,
        name: { contains: "Standard", mode: "insensitive" },
      },
      include: {
        _count: { select: { lines: true } },
        client: { select: { id: true, name: true, code: true } },
      },
      orderBy: [{ isActive: "desc" }, { effectiveFrom: "desc" }, { name: "asc" }],
    });

    expect(JSON.parse(blueprintsResult.content)).toEqual([
      {
        id: "bp_project",
        name: "Project Default",
        projectCount: 3,
      },
    ]);
    expect(JSON.parse(blueprintResult.content)).toEqual(
      expect.objectContaining({
        id: "bp_project",
        name: "Project Default",
        fieldDefs: [{ key: "market", type: "text" }],
        rolePresets: [{ role: "Consulting", share: 0.5 }],
      }),
    );
    expect(JSON.parse(rateCardsResult.content)).toEqual([
      {
        id: "rc_2026",
        name: "Standard 2026",
        effectiveFrom: "2026-01-01",
        effectiveTo: null,
        lineCount: 12,
      },
    ]);
  });

  it("routes project health reads through the dashboard router path", async () => {
    vi.mocked(getDashboardProjectHealth).mockResolvedValue([
      {
        id: "project_1",
        projectName: "Apollo",
        shortCode: "APL",
        status: "ACTIVE",
        clientId: "client_1",
        clientName: "Acme Mobility",
        budgetHealth: 74,
        staffingHealth: 88,
        timelineHealth: 63,
        compositeScore: 75,
      },
    ]);

    const ctx = createToolContext({}, { userRole: SystemRole.CONTROLLER });
    const result = await executeTool("get_project_health", "{}", ctx);

    expect(getDashboardProjectHealth).toHaveBeenCalledTimes(1);
    expect(JSON.parse(result.content)).toEqual({
      projects: [
        {
          projectId: "project_1",
          projectName: "Apollo",
          shortCode: "APL",
          status: "ACTIVE",
          overall: 75,
          budget: 74,
          staffing: 88,
          timeline: 63,
          rating: "at_risk",
        },
      ],
      summary: {
        healthy: 0,
        atRisk: 1,
        critical: 0,
      },
    });
  });

  it("routes dashboard detail reads through dashboard router callers", async () => {
    vi.mocked(getDashboardOverview).mockResolvedValue({
      totalResources: 12,
      activeResources: 10,
      inactiveResources: 2,
      totalProjects: 4,
      activeProjects: 3,
      inactiveProjects: 1,
      totalAllocations: 8,
      activeAllocations: 7,
      cancelledAllocations: 1,
      approvedVacations: 2,
      totalEstimates: 5,
      budgetSummary: {
        totalBudgetCents: 500_000,
        totalCostCents: 240_000,
        avgUtilizationPercent: 48,
      },
      budgetBasis: {
        remainingBudgetCents: 260_000,
        budgetedProjects: 3,
        unbudgetedProjects: 1,
        trackedAssignmentCount: 8,
        windowStart: new Date("2026-01-01T00:00:00.000Z"),
        windowEnd: new Date("2026-06-30T00:00:00.000Z"),
      },
      recentActivity: [],
      projectsByStatus: [],
      chapterUtilization: [
        {
          chapter: "Delivery",
          resourceCount: 4,
          avgChargeabilityTarget: 78,
        },
      ],
    });
    vi.mocked(getDashboardPeakTimes).mockResolvedValue([
      {
        period: "2026-03",
        groups: [],
        totalHours: 320.4,
        capacityHours: 400.2,
        utilizationPct: 80,
        derivation: {
          periodStart: "2026-03-01",
          periodEnd: "2026-03-31",
          resourceCount: 4,
          groupCount: 1,
          bookedHours: 320.4,
          capacityHours: 400.2,
          remainingCapacityHours: 79.8,
          overbookedHours: 0,
          utilizationPct: 80,
        },
      },
    ]);
    vi.mocked(getDashboardTopValueResources).mockResolvedValue([
      {
        id: "res_1",
        eid: "pparker",
        displayName: "Peter Parker",
        chapter: "Delivery",
        valueScore: 91,
        lcrCents: 9_500,
      },
    ]);
    vi.mocked(getDashboardDemand).mockResolvedValue([
      {
        id: "project_1",
        name: "Gelddruckmaschine",
        shortCode: "GDM",
        allocatedHours: 120,
        requiredFTEs: 4,
        resourceCount: 2,
        derivation: {
          periodStart: "2026-01-01",
          periodEnd: "2026-06-30",
          periodWorkingHoursBase: 1040,
          requiredHours: 2080,
          requiredFTEs: 4,
          fillPct: 50,
          demandSource: "DEMAND_REQUIREMENTS",
          calendarLocations: [
            {
              countryCode: "DE",
              federalState: "BY",
              metroCityName: "Augsburg",
              resourceCount: 2,
              allocatedHours: 120,
            },
          ],
        },
      },
    ]);

    const ctx = createToolContext(
      {
        systemSettings: {
          findUnique: vi.fn().mockResolvedValue(null),
        },
      },
      { userRole: SystemRole.CONTROLLER },
    );

    const result = await executeTool("get_dashboard_detail", JSON.stringify({ section: "all" }), ctx);

    expect(getDashboardOverview).toHaveBeenCalledTimes(1);
    expect(getDashboardPeakTimes).toHaveBeenCalledWith(
      ctx.db,
      expect.objectContaining({
        startDate: new Date("2026-01-01T00:00:00.000Z"),
        endDate: new Date("2026-06-30T00:00:00.000Z"),
        granularity: "month",
        groupBy: "project",
      }),
    );
    expect(getDashboardTopValueResources).toHaveBeenCalledWith(
      ctx.db,
      expect.objectContaining({
        limit: 10,
        userRole: SystemRole.CONTROLLER,
      }),
    );
    expect(getDashboardDemand).toHaveBeenCalledWith(
      ctx.db,
      expect.objectContaining({
        startDate: new Date("2026-01-01T00:00:00.000Z"),
        endDate: new Date("2026-06-30T00:00:00.000Z"),
        groupBy: "project",
      }),
    );
    expect(JSON.parse(result.content)).toEqual({
      peakTimes: [
        {
          month: "2026-03",
          totalHours: 320.4,
          totalHoursPerDay: 320.4,
          capacityHours: 400.2,
          utilizationPct: 80,
        },
      ],
      topResources: [
        {
          name: "Peter Parker",
          eid: "pparker",
          chapter: "Delivery",
          lcr: "95,00 EUR",
          valueScore: 91,
        },
      ],
      demandPipeline: [
        {
          project: "Gelddruckmaschine (GDM)",
          needed: 2,
          requiredFTEs: 4,
          allocatedResources: 2,
          allocatedHours: 120,
          calendarLocations: [
            {
              countryCode: "DE",
              federalState: "BY",
              metroCityName: "Augsburg",
              resourceCount: 2,
              allocatedHours: 120,
            },
          ],
        },
      ],
      chargeabilityByChapter: [
        {
          chapter: "Delivery",
          headcount: 4,
          avgTarget: "78%",
        },
      ],
    });
  });

  it("routes skill gap reads through the dashboard router path", async () => {
    vi.mocked(getDashboardSkillGapSummary).mockResolvedValue({
      roleGaps: [
        { role: "Pipeline TD", needed: 4, filled: 1, gap: 3, fillRate: 25 },
      ],
      totalOpenPositions: 3,
      skillSupplyTop10: [
        { skill: "houdini", resourceCount: 5 },
      ],
      resourcesByRole: [
        { role: "Pipeline TD", count: 2 },
      ],
    });

    const ctx = createToolContext({}, { userRole: SystemRole.CONTROLLER });
    const result = await executeTool("get_skill_gaps", "{}", ctx);

    expect(getDashboardSkillGapSummary).toHaveBeenCalledTimes(1);
    expect(JSON.parse(result.content)).toEqual({
      roleGaps: [
        { role: "Pipeline TD", needed: 4, filled: 1, gap: 3, fillRate: 25 },
      ],
      totalOpenPositions: 3,
      skillSupplyTop10: [
        { skill: "houdini", resourceCount: 5 },
      ],
      resourcesByRole: [
        { role: "Pipeline TD", count: 2 },
      ],
    });
  });

  it("routes insights summary reads through the insights router path", async () => {
    const db = {
      project: {
        findMany: vi.fn().mockResolvedValue([
          {
            budgetCents: 100_000,
            startDate: new Date("2026-03-01T00:00:00.000Z"),
            endDate: new Date("2026-03-31T00:00:00.000Z"),
            demandRequirements: [
              {
                headcount: 3,
                startDate: new Date("2026-03-20T00:00:00.000Z"),
                endDate: new Date("2026-04-05T00:00:00.000Z"),
                _count: { assignments: 1 },
              },
            ],
            assignments: [
              {
                resourceId: "res_1",
                startDate: new Date("2026-03-01T00:00:00.000Z"),
                endDate: new Date("2026-03-31T00:00:00.000Z"),
                hoursPerDay: 8,
                dailyCostCents: 10_000,
                status: "ACTIVE",
              },
            ],
          },
        ]),
      },
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_1",
            availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
          },
        ]),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([
          {
            resourceId: "res_1",
            hoursPerDay: 2,
          },
        ]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool("get_insights_summary", "{}", ctx);

    expect(db.project.findMany).toHaveBeenCalledWith({
      where: { status: { in: ["ACTIVE", "DRAFT"] } },
      include: {
        demandRequirements: {
          select: {
            headcount: true,
            startDate: true,
            endDate: true,
            _count: { select: { assignments: true } },
          },
        },
        assignments: {
          select: {
            resourceId: true,
            startDate: true,
            endDate: true,
            hoursPerDay: true,
            dailyCostCents: true,
            status: true,
          },
        },
      },
    });
    expect(db.resource.findMany).toHaveBeenCalledWith({
      where: { isActive: true },
      select: { id: true, displayName: true, availability: true },
    });
    expect(db.assignment.findMany).toHaveBeenCalledWith({
      where: {
        status: { in: ["ACTIVE", "CONFIRMED"] },
        startDate: { lte: expect.any(Date) },
        endDate: { gte: expect.any(Date) },
      },
      select: { resourceId: true, hoursPerDay: true },
    });
    expect(JSON.parse(result.content)).toEqual({
      total: 3,
      criticalCount: 2,
      budget: 1,
      staffing: 1,
      timeline: 0,
      utilization: 1,
    });
  });

  it("returns anomaly details and count from the insights-backed detector", async () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date("2026-03-29T12:00:00.000Z"));

    try {
      const ctx = createToolContext({
        project: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "project_1",
              name: "Apollo",
              budgetCents: 100_000,
              startDate: new Date("2026-03-01T00:00:00.000Z"),
              endDate: new Date("2026-03-31T00:00:00.000Z"),
              demandRequirements: [
                {
                  id: "demand_1",
                  headcount: 3,
                  startDate: new Date("2026-03-20T00:00:00.000Z"),
                  endDate: new Date("2026-04-05T00:00:00.000Z"),
                  status: "CONFIRMED",
                  _count: { assignments: 1 },
                },
              ],
              assignments: [
                {
                  id: "assignment_1",
                  resourceId: "res_1",
                  startDate: new Date("2026-03-01T00:00:00.000Z"),
                  endDate: new Date("2026-04-05T00:00:00.000Z"),
                  hoursPerDay: 12,
                  dailyCostCents: 10_000,
                  status: "ACTIVE",
                },
              ],
            },
          ]),
        },
        resource: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "res_1",
              displayName: "Peter Parker",
              availability: {
                monday: 8,
                tuesday: 8,
                wednesday: 8,
                thursday: 8,
                friday: 8,
              },
            },
          ]),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([
            {
              resourceId: "res_1",
              hoursPerDay: 12,
            },
          ]),
        },
      });

      const result = await executeTool("detect_anomalies", "{}", ctx);
      const parsed = JSON.parse(result.content) as {
        count: number;
        anomalies: Array<{ type: string; severity: string; entityName: string }>;
      };

      expect(parsed.count).toBe(4);
      expect(parsed.anomalies).toEqual([
        expect.objectContaining({
          type: "budget",
          severity: "critical",
          entityName: "Apollo",
        }),
        expect.objectContaining({
          type: "staffing",
          severity: "critical",
          entityName: "Apollo",
        }),
        expect.objectContaining({
          type: "utilization",
          severity: "critical",
          entityName: "Peter Parker",
        }),
        expect.objectContaining({
          type: "timeline",
          severity: "warning",
          entityName: "Apollo",
        }),
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it("routes project search and detail reads through the project router path", async () => {
    const db = {
      project: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "project_1",
            shortCode: "GDM",
            name: "Gelddruckmaschine",
            status: "ACTIVE",
            budgetCents: 500_000,
            winProbability: 100,
            startDate: new Date("2026-01-01T00:00:00.000Z"),
            endDate: new Date("2026-03-31T00:00:00.000Z"),
            client: { name: "Acme Mobility" },
            _count: { assignments: 3, estimates: 1 },
          },
        ]),
        findUnique: vi.fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce({
            id: "project_1",
            shortCode: "GDM",
            name: "Gelddruckmaschine",
            status: "ACTIVE",
            startDate: new Date("2026-01-01T00:00:00.000Z"),
            endDate: new Date("2026-03-31T00:00:00.000Z"),
          })
          .mockResolvedValueOnce({
            id: "project_1",
            shortCode: "GDM",
            name: "Gelddruckmaschine",
            status: "ACTIVE",
            orderType: "CHARGEABLE",
            allocationType: "INT",
            budgetCents: 500_000,
            winProbability: 100,
            startDate: new Date("2026-01-01T00:00:00.000Z"),
            endDate: new Date("2026-03-31T00:00:00.000Z"),
            responsiblePerson: "Bruce Banner",
            client: { name: "Acme Mobility" },
            utilizationCategory: { code: "BILL", name: "Billable" },
            _count: { assignments: 3, estimates: 1 },
          }),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([
          {
            resource: { displayName: "Bruce Banner", eid: "EMP-001" },
            role: "Lead",
            status: "ACTIVE",
            hoursPerDay: 8,
            startDate: new Date("2026-02-01T00:00:00.000Z"),
            endDate: new Date("2026-02-28T00:00:00.000Z"),
          },
        ]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const searchResult = await executeTool(
      "search_projects",
      JSON.stringify({ query: "Gelddruckmaschine", limit: 10 }),
      ctx,
    );
    const detailResult = await executeTool(
      "get_project",
      JSON.stringify({ identifier: "GDM" }),
      ctx,
    );

    expect(db.project.findMany).toHaveBeenCalledWith({
      where: {
        OR: [
          { name: { contains: "Gelddruckmaschine", mode: "insensitive" } },
          { shortCode: { contains: "Gelddruckmaschine", mode: "insensitive" } },
        ],
      },
      select: {
        id: true,
        shortCode: true,
        name: true,
        status: true,
        budgetCents: true,
        winProbability: true,
        startDate: true,
        endDate: true,
        client: { select: { name: true } },
        _count: { select: { assignments: true, estimates: true } },
      },
      take: 10,
      orderBy: { name: "asc" },
    });
    expect(db.project.findUnique).toHaveBeenNthCalledWith(1, {
      where: { id: "GDM" },
      select: {
        id: true,
        shortCode: true,
        name: true,
        status: true,
        startDate: true,
        endDate: true,
      },
    });
    expect(db.project.findUnique).toHaveBeenNthCalledWith(2, {
      where: { shortCode: "GDM" },
      select: {
        id: true,
        shortCode: true,
        name: true,
        status: true,
        startDate: true,
        endDate: true,
      },
    });
    expect(db.project.findUnique).toHaveBeenNthCalledWith(3, {
      where: { id: "project_1" },
      select: {
        id: true,
        shortCode: true,
        name: true,
        status: true,
        orderType: true,
        allocationType: true,
        budgetCents: true,
        winProbability: true,
        startDate: true,
        endDate: true,
        responsiblePerson: true,
        client: { select: { name: true } },
        utilizationCategory: { select: { code: true, name: true } },
        _count: { select: { assignments: true, estimates: true } },
      },
    });
    expect(db.assignment.findMany).toHaveBeenCalledWith({
      where: {
        projectId: "project_1",
        status: { not: "CANCELLED" },
      },
      select: {
        resource: { select: { displayName: true, eid: true } },
        role: true,
        status: true,
        hoursPerDay: true,
        startDate: true,
        endDate: true,
      },
      take: 10,
      orderBy: { startDate: "desc" },
    });

    expect(JSON.parse(searchResult.content)).toEqual([
      {
        id: "project_1",
        code: "GDM",
        name: "Gelddruckmaschine",
        status: "ACTIVE",
        budget: "5.000,00 EUR",
        winProbability: "100%",
        start: "2026-01-01",
        end: "2026-03-31",
        client: "Acme Mobility",
        assignmentCount: 3,
        estimateCount: 1,
      },
    ]);
    expect(JSON.parse(detailResult.content)).toEqual({
      id: "project_1",
      code: "GDM",
      name: "Gelddruckmaschine",
      status: "ACTIVE",
      orderType: "CHARGEABLE",
      allocationType: "INT",
      budget: "5.000,00 EUR",
      budgetCents: 500000,
      winProbability: "100%",
      start: "2026-01-01",
      end: "2026-03-31",
      responsible: "Bruce Banner",
      client: "Acme Mobility",
      category: "Billable",
      assignmentCount: 3,
      estimateCount: 1,
      topAllocations: [
        {
          resource: "Bruce Banner",
          eid: "EMP-001",
          role: "Lead",
          status: "ACTIVE",
          hoursPerDay: 8,
          start: "2026-02-01",
          end: "2026-02-28",
        },
      ],
    });
  });

  it("routes resource search and detail reads through the resource router path", async () => {
    const db = {
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_1",
            eid: "EMP-001",
            displayName: "Bruce Banner",
            chapter: "Delivery",
            fte: 1,
            lcrCents: 8_500,
            chargeabilityTarget: 80,
            isActive: true,
            areaRole: { name: "Pipeline TD" },
            country: { code: "DE", name: "Germany", dailyWorkingHours: 8 },
            metroCity: { name: "Munich" },
            orgUnit: { name: "Operations", level: 5 },
            email: "bruce@example.com",
            ucrCents: 10_500,
            availability: { monday: 8 },
            skills: [{ name: "Houdini", level: 5 }],
            postalCode: "80331",
            federalState: "BY",
            managementLevelGroup: { name: "Senior", targetPercentage: 75 },
            _count: { assignments: 4, vacations: 2 },
          },
        ]),
        findUnique: vi.fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce({
            id: "res_1",
            eid: "EMP-001",
            displayName: "Bruce Banner",
            chapter: "Delivery",
            isActive: true,
          })
          .mockResolvedValueOnce({
            id: "res_1",
            eid: "EMP-001",
            displayName: "Bruce Banner",
            email: "bruce@example.com",
            chapter: "Delivery",
            fte: 1,
            lcrCents: 8_500,
            ucrCents: 10_500,
            chargeabilityTarget: 80,
            isActive: true,
            availability: { monday: 8 },
            skills: [{ name: "Houdini", level: 5 }],
            postalCode: "80331",
            federalState: "BY",
            areaRole: { name: "Pipeline TD", color: "#112233" },
            country: { code: "DE", name: "Germany", dailyWorkingHours: 8 },
            metroCity: { name: "Munich" },
            managementLevelGroup: { name: "Senior", targetPercentage: 75 },
            orgUnit: { name: "Operations", level: 5 },
            _count: { assignments: 4, vacations: 2 },
          }),
        findFirst: vi.fn(),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const searchResult = await executeTool(
      "search_resources",
      JSON.stringify({ query: "Bruce", country: "DE", roleName: "Pipeline", limit: 5 }),
      ctx,
    );
    const detailResult = await executeTool(
      "get_resource",
      JSON.stringify({ identifier: "EMP-001" }),
      ctx,
    );

    expect(db.resource.findMany).toHaveBeenCalledWith({
      where: {
        isActive: true,
        OR: [
          { displayName: { contains: "Bruce", mode: "insensitive" } },
          { eid: { contains: "Bruce", mode: "insensitive" } },
          { chapter: { contains: "Bruce", mode: "insensitive" } },
        ],
        country: {
          OR: [
            { code: { equals: "DE", mode: "insensitive" } },
            { name: { contains: "DE", mode: "insensitive" } },
          ],
        },
        areaRole: { name: { contains: "Pipeline", mode: "insensitive" } },
      },
      select: {
        id: true,
        eid: true,
        displayName: true,
        chapter: true,
        fte: true,
        lcrCents: true,
        chargeabilityTarget: true,
        isActive: true,
        areaRole: { select: { name: true } },
        country: { select: { code: true, name: true } },
        metroCity: { select: { name: true } },
        orgUnit: { select: { name: true } },
      },
      take: 5,
      orderBy: { displayName: "asc" },
    });
    expect(db.resource.findUnique).toHaveBeenNthCalledWith(1, {
      where: { id: "EMP-001" },
      select: {
        id: true,
        eid: true,
        displayName: true,
        chapter: true,
        isActive: true,
      },
    });
    expect(db.resource.findUnique).toHaveBeenNthCalledWith(2, {
      where: { eid: "EMP-001" },
      select: {
        id: true,
        eid: true,
        displayName: true,
        chapter: true,
        isActive: true,
      },
    });
    expect(db.resource.findUnique).toHaveBeenNthCalledWith(3, {
      where: { id: "res_1" },
      select: {
        id: true,
        eid: true,
        displayName: true,
        email: true,
        chapter: true,
        fte: true,
        lcrCents: true,
        ucrCents: true,
        chargeabilityTarget: true,
        isActive: true,
        availability: true,
        skills: true,
        postalCode: true,
        federalState: true,
        areaRole: { select: { name: true, color: true } },
        country: { select: { code: true, name: true, dailyWorkingHours: true } },
        metroCity: { select: { name: true } },
        managementLevelGroup: { select: { name: true, targetPercentage: true } },
        orgUnit: { select: { name: true, level: true } },
        _count: { select: { assignments: true, vacations: true } },
      },
    });

    expect(JSON.parse(searchResult.content)).toEqual([
      {
        id: "res_1",
        eid: "EMP-001",
        name: "Bruce Banner",
        chapter: "Delivery",
        role: "Pipeline TD",
        country: "Germany",
        countryCode: "DE",
        metroCity: "Munich",
        orgUnit: "Operations",
        fte: 1,
        lcr: "85,00 EUR",
        chargeabilityTarget: "80%",
        active: true,
      },
    ]);
    expect(JSON.parse(detailResult.content)).toEqual({
      id: "res_1",
      eid: "EMP-001",
      name: "Bruce Banner",
      email: "bruce@example.com",
      chapter: "Delivery",
      role: "Pipeline TD",
      country: "Germany",
      countryCode: "DE",
      countryHours: 8,
      metroCity: "Munich",
      fte: 1,
      lcr: "85,00 EUR",
      ucr: "105,00 EUR",
      chargeabilityTarget: "80%",
      managementLevel: "Senior",
      orgUnit: "Operations",
      postalCode: "80331",
      federalState: "BY",
      active: true,
      totalAssignments: 4,
      totalVacations: 2,
      skillCount: 1,
      topSkills: ["Houdini (5)"],
    });
  });

  it("routes rate lookup reads through the rate-card router path", async () => {
    const db = {
      rateCard: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "rc_client",
            name: "Client 2026",
            client: { id: "client_1", name: "Acme Mobility" },
            lines: [
              {
                id: "line_best",
                chapter: "Delivery",
                seniority: "Senior",
                costRateCents: 12_000,
                billRateCents: 18_000,
                role: { id: "role_1", name: "Pipeline TD" },
              },
            ],
          },
          {
            id: "rc_default",
            name: "Default 2026",
            client: null,
            lines: [
              {
                id: "line_alt",
                chapter: "Delivery",
                seniority: "Senior",
                costRateCents: 11_000,
                billRateCents: 17_000,
                role: { id: "role_1", name: "Pipeline TD" },
              },
            ],
          },
        ]),
      },
      role: {
        findFirst: vi.fn().mockResolvedValue({ id: "role_1" }),
      },
    };
    const ctx = createToolContext(db, {
      userRole: SystemRole.CONTROLLER,
      permissions: [PermissionKey.VIEW_COSTS],
    });

    const result = await executeTool(
      "lookup_rate",
      JSON.stringify({
        clientId: "client_1",
        chapter: "Delivery",
        roleName: "Pipeline",
        seniority: "Senior",
      }),
      ctx,
    );

    expect(db.rateCard.findMany).toHaveBeenCalledWith({
      where: {
        isActive: true,
        OR: [
          { clientId: "client_1" },
          { clientId: null },
        ],
      },
      include: {
        lines: {
          select: {
            id: true,
            chapter: true,
            seniority: true,
            costRateCents: true,
            billRateCents: true,
            role: { select: { id: true, name: true } },
          },
        },
        client: { select: { id: true, name: true } },
      },
      orderBy: [{ effectiveFrom: "desc" }],
    });
    expect(db.role.findFirst).toHaveBeenCalledWith({
      where: { name: { contains: "Pipeline", mode: "insensitive" } },
      select: { id: true },
    });
    expect(JSON.parse(result.content)).toEqual({
      bestMatch: {
        rateCardName: "Client 2026",
        clientId: "client_1",
        clientName: "Acme Mobility",
        lineId: "line_best",
        chapter: "Delivery",
        seniority: "Senior",
        roleName: "Pipeline TD",
        costRateCents: 12000,
        billRateCents: 18000,
        score: 10,
        costRate: "120,00 EUR",
        billRate: "180,00 EUR",
      },
      alternatives: [
        {
          rateCardName: "Default 2026",
          clientId: null,
          clientName: null,
          lineId: "line_alt",
          chapter: "Delivery",
          seniority: "Senior",
          roleName: "Pipeline TD",
          costRateCents: 11000,
          billRateCents: 17000,
          score: 7,
          costRate: "110,00 EUR",
          billRate: "170,00 EUR",
        },
      ],
      totalCandidates: 2,
    });
  });

  it("routes report reads through the report router path", async () => {
    const db = {
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_1",
            displayName: "Bruce Banner",
            chapter: "Delivery",
            country: { name: "Germany" },
          },
        ]),
        count: vi.fn().mockResolvedValue(1),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool(
      "run_report",
      JSON.stringify({
        entity: "resource",
        columns: ["displayName", "chapter", "country.name"],
        filters: [{ field: "displayName", op: "contains", value: "Bruce" }],
        limit: 25,
      }),
      ctx,
    );

    expect(db.resource.findMany).toHaveBeenCalledWith({
      select: {
        id: true,
        displayName: true,
        chapter: true,
        country: { select: { name: true } },
      },
      where: {
        displayName: { contains: "Bruce", mode: "insensitive" },
      },
      take: 25,
      skip: 0,
    });
    expect(db.resource.count).toHaveBeenCalledWith({
      where: {
        displayName: { contains: "Bruce", mode: "insensitive" },
      },
    });
    expect(JSON.parse(result.content)).toEqual({
      rows: [
        {
          id: "res_1",
          displayName: "Bruce Banner",
          chapter: "Delivery",
          "country.name": "Germany",
        },
      ],
      rowCount: 1,
      columns: ["id", "displayName", "chapter", "country.name"],
    });
  });

  it("routes budget status reads through the timeline and project router paths", async () => {
    vi.mocked(listAssignmentBookings).mockResolvedValue([
      {
        projectId: "project_1",
        status: "CONFIRMED",
        dailyCostCents: 10_000,
        hoursPerDay: 8,
        startDate: new Date("2026-04-01T00:00:00.000Z"),
        endDate: new Date("2026-04-02T00:00:00.000Z"),
        project: {
          id: "project_1",
          status: "ACTIVE",
        },
      },
      {
        projectId: "project_1",
        status: "PROPOSED",
        dailyCostCents: 5_000,
        hoursPerDay: 8,
        startDate: new Date("2026-04-03T00:00:00.000Z"),
        endDate: new Date("2026-04-03T00:00:00.000Z"),
        project: {
          id: "project_1",
          status: "ACTIVE",
        },
      },
    ] as Awaited<ReturnType<typeof listAssignmentBookings>>);

    const db = {
      project: {
        findUnique: vi
          .fn()
          .mockResolvedValueOnce({
            id: "project_1",
            name: "Gelddruckmaschine",
            shortCode: "GDM",
            status: "ACTIVE",
            responsiblePerson: "Larissa",
          })
          .mockResolvedValueOnce({
            id: "project_1",
            name: "Gelddruckmaschine",
            shortCode: "GDM",
            budgetCents: 100_000,
            winProbability: 80,
            startDate: new Date("2026-04-01T00:00:00.000Z"),
            endDate: new Date("2026-04-30T00:00:00.000Z"),
          }),
        findFirst: vi.fn(),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool(
      "get_budget_status",
      JSON.stringify({ projectId: "project_1" }),
      ctx,
    );

    expect(vi.mocked(listAssignmentBookings)).toHaveBeenCalledWith(db, {
      projectIds: ["project_1"],
    });
    expect(JSON.parse(result.content)).toEqual({
      project: "Gelddruckmaschine",
      code: "GDM",
      budget: "1.000,00 EUR",
      confirmed: "200,00 EUR",
      proposed: "50,00 EUR",
      allocated: "250,00 EUR",
      remaining: "750,00 EUR",
      utilization: "25.0%",
      winWeighted: "200,00 EUR",
    });
  });

  it("routes scenario simulation through the scenario router path", async () => {
    const db = {
      project: {
        findUnique: vi.fn().mockResolvedValue({
          id: "project_1",
          name: "Gelddruckmaschine",
          budgetCents: 200_000,
          orderType: "CHARGEABLE",
          startDate: new Date("2026-04-01T00:00:00.000Z"),
          endDate: new Date("2026-04-30T00:00:00.000Z"),
        }),
      },
      assignment: {
        findMany: vi.fn()
          .mockResolvedValueOnce([
            {
              id: "assign_1",
              resourceId: "res_1",
              projectId: "project_1",
              hoursPerDay: 4,
              startDate: new Date("2026-04-01T00:00:00.000Z"),
              endDate: new Date("2026-04-03T00:00:00.000Z"),
              status: "ACTIVE",
              roleId: null,
              role: "Pipeline TD",
              resource: {
                id: "res_1",
                displayName: "Bruce Banner",
                eid: "EMP-001",
                lcrCents: 10_000,
                availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
                chargeabilityTarget: 80,
                skills: [{ skill: "Houdini" }],
                countryId: null,
                federalState: null,
                metroCityId: null,
                country: null,
                metroCity: null,
              },
              roleEntity: null,
            },
          ])
          .mockResolvedValueOnce([
            {
              id: "assign_1",
              resourceId: "res_1",
              projectId: "project_1",
              hoursPerDay: 4,
              startDate: new Date("2026-04-01T00:00:00.000Z"),
              endDate: new Date("2026-04-03T00:00:00.000Z"),
            },
          ]),
      },
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_1",
            displayName: "Bruce Banner",
            eid: "EMP-001",
            lcrCents: 10_000,
            availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
            chargeabilityTarget: 80,
            skills: [{ skill: "Houdini" }],
            countryId: null,
            federalState: null,
            metroCityId: null,
            country: null,
            metroCity: null,
          },
        ]),
      },
      role: {
        findMany: vi.fn(),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool(
      "simulate_scenario",
      JSON.stringify({
        projectId: "project_1",
        changes: [
          {
            assignmentId: "assign_1",
            resourceId: "res_1",
            startDate: "2026-04-01",
            endDate: "2026-04-03",
            hoursPerDay: 6,
          },
        ],
      }),
      ctx,
    );

    expect(db.assignment.findMany).toHaveBeenNthCalledWith(1, {
      where: { projectId: "project_1", status: { not: "CANCELLED" } },
      include: {
        resource: {
          select: {
            id: true,
            displayName: true,
            eid: true,
            lcrCents: true,
            availability: true,
            chargeabilityTarget: true,
            skills: true,
            countryId: true,
            federalState: true,
            metroCityId: true,
            country: { select: { code: true } },
            metroCity: { select: { name: true } },
          },
        },
      },
    });
    expect(db.assignment.findMany).toHaveBeenNthCalledWith(2, {
      where: {
        resourceId: { in: ["res_1"] },
        status: { not: "CANCELLED" },
      },
      select: {
        id: true,
        resourceId: true,
        projectId: true,
        hoursPerDay: true,
        startDate: true,
        endDate: true,
      },
    });
    expect(db.role.findMany).not.toHaveBeenCalled();
    expect(JSON.parse(result.content)).toEqual({
      baseline: {
        totalCostCents: 120000,
        totalHours: 12,
        headcount: 1,
        skillCount: 1,
        totalCost: "1.200,00 EUR",
      },
      scenario: {
        totalCostCents: 180000,
        totalHours: 18,
        headcount: 1,
        skillCount: 1,
        totalCost: "1.800,00 EUR",
      },
      delta: {
        costCents: 60000,
        hours: 6,
        headcount: 0,
        skillCoveragePct: 100,
        cost: "600,00 EUR",
      },
      resourceImpacts: [
        {
          resourceId: "res_1",
          resourceName: "Bruce Banner",
          chargeabilityTarget: 80,
          currentUtilization: 6.8,
          scenarioUtilization: 10.2,
          utilizationDelta: 3.4,
          isOverallocated: false,
        },
      ],
      warnings: [],
      budgetCents: 200000,
    });
  });

  it("routes allocation listing through the allocation router path", async () => {
    const db = {
      systemSettings: {
        findUnique: vi.fn().mockResolvedValue({
          anonymizationEnabled: false,
          anonymizationDomain: null,
          anonymizationSeed: null,
          anonymizationMode: null,
          anonymizationAliases: null,
        }),
      },
      demandRequirement: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "demand_1",
            projectId: "project_1",
            startDate: new Date("2026-04-01T00:00:00.000Z"),
            endDate: new Date("2026-04-30T00:00:00.000Z"),
            hoursPerDay: 8,
            percentage: 100,
            role: "TD",
            roleId: null,
            headcount: 1,
            status: "OPEN",
            metadata: null,
            createdAt: new Date("2026-03-20T00:00:00.000Z"),
            updatedAt: new Date("2026-03-20T00:00:00.000Z"),
            project: { id: "project_1", name: "Gelddruckmaschine", shortCode: "GDM", status: "ACTIVE", endDate: new Date("2026-04-30T00:00:00.000Z") },
            roleEntity: null,
            assignments: [],
          },
        ]),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "assign_1",
            demandRequirementId: null,
            resourceId: "res_1",
            projectId: "project_1",
            startDate: new Date("2026-04-01T00:00:00.000Z"),
            endDate: new Date("2026-04-10T00:00:00.000Z"),
            hoursPerDay: 6,
            percentage: 75,
            role: "Pipeline TD",
            roleId: null,
            dailyCostCents: 12_000,
            status: "ACTIVE",
            metadata: null,
            createdAt: new Date("2026-03-21T00:00:00.000Z"),
            updatedAt: new Date("2026-03-21T00:00:00.000Z"),
            resource: { id: "res_1", displayName: "Bruce Banner", eid: "EMP-001", lcrCents: 8_500 },
            project: { id: "project_1", name: "Gelddruckmaschine", shortCode: "GDM", status: "ACTIVE", endDate: new Date("2026-04-30T00:00:00.000Z") },
            roleEntity: null,
            demandRequirement: null,
          },
        ]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool(
      "list_allocations",
      JSON.stringify({
        projectId: "project_1",
        resourceName: "Bruce",
        projectCode: "GDM",
        status: "ACTIVE",
        limit: 10,
      }),
      ctx,
    );

    expect(db.demandRequirement.findMany).toHaveBeenCalledWith({
      where: {
        projectId: "project_1",
        status: "ACTIVE",
      },
      include: {
        project: {
          select: {
            id: true,
            name: true,
            shortCode: true,
            status: true,
            endDate: true,
          },
        },
        roleEntity: {
          select: { id: true, name: true, color: true },
        },
        assignments: {
          include: {
            resource: {
              select: {
                id: true,
                displayName: true,
                eid: true,
                lcrCents: true,
                chapter: true,
              },
            },
            project: {
              select: {
                id: true,
                name: true,
                shortCode: true,
                status: true,
                endDate: true,
              },
            },
            roleEntity: {
              select: { id: true, name: true, color: true },
            },
          },
        },
      },
      orderBy: { startDate: "asc" },
    });
    expect(db.assignment.findMany).toHaveBeenCalledWith({
      where: {
        projectId: "project_1",
        status: "ACTIVE",
      },
      include: {
        resource: {
          select: {
            id: true,
            displayName: true,
            eid: true,
            lcrCents: true,
            chapter: true,
          },
        },
        project: {
          select: {
            id: true,
            name: true,
            shortCode: true,
            status: true,
            endDate: true,
          },
        },
        roleEntity: {
          select: { id: true, name: true, color: true },
        },
        demandRequirement: {
          select: {
            id: true,
            projectId: true,
            startDate: true,
            endDate: true,
            hoursPerDay: true,
            percentage: true,
            role: true,
            roleId: true,
            headcount: true,
            status: true,
          },
        },
      },
      orderBy: { startDate: "asc" },
    });
    expect(JSON.parse(result.content)).toEqual([
      {
        id: "assign_1",
        resource: "Bruce Banner",
        resourceEid: "EMP-001",
        project: "Gelddruckmaschine",
        projectCode: "GDM",
        role: "Pipeline TD",
        status: "ACTIVE",
        hoursPerDay: 6,
        dailyCost: "120,00 EUR",
        start: "2026-04-01",
        end: "2026-04-10",
      },
    ]);
  });

  it("routes upcoming vacation reads through the vacation router path and post-filters by chapter", async () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date("2026-03-01T00:00:00.000Z"));

    try {
      const db = {
        vacation: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "vac_1",
              resourceId: "res_1",
              status: "APPROVED",
              type: VacationType.ANNUAL,
              startDate: new Date("2026-03-05T00:00:00.000Z"),
              endDate: new Date("2026-03-06T00:00:00.000Z"),
              isHalfDay: false,
              halfDayPart: null,
              resource: {
                id: "res_1",
                displayName: "Bruce Banner",
                eid: "EMP-001",
                lcrCents: 8_000,
                chapter: "Delivery",
              },
              requestedBy: null,
              approvedBy: null,
            },
            {
              id: "vac_2",
              resourceId: "res_2",
              status: "APPROVED",
              type: VacationType.ANNUAL,
              startDate: new Date("2026-03-07T00:00:00.000Z"),
              endDate: new Date("2026-03-07T00:00:00.000Z"),
              isHalfDay: true,
              halfDayPart: "MORNING",
              resource: {
                id: "res_2",
                displayName: "Tony Stark",
                eid: "EMP-002",
                lcrCents: 9_000,
                chapter: "Finance",
              },
              requestedBy: null,
              approvedBy: null,
            },
          ]),
        },
      };
      const ctx = createToolContext(db, { userRole: SystemRole.MANAGER });

      const result = await executeTool(
        "list_vacations_upcoming",
        JSON.stringify({ chapter: "Delivery", daysAhead: 14, limit: 10 }),
        ctx,
      );

      expect(db.vacation.findMany).toHaveBeenCalledWith({
        where: {
          status: "APPROVED",
          endDate: { gte: new Date("2026-03-01T00:00:00.000Z") },
          startDate: { lte: new Date("2026-03-15T00:00:00.000Z") },
        },
        include: {
          resource: {
            select: {
              id: true,
              displayName: true,
              eid: true,
              lcrCents: true,
              chapter: true,
            },
          },
          requestedBy: { select: { id: true, name: true, email: true } },
          approvedBy: { select: { id: true, name: true, email: true } },
        },
        orderBy: { startDate: "asc" },
        take: 10,
      });
      expect(JSON.parse(result.content)).toEqual([
        {
          resource: "Bruce Banner",
          eid: "EMP-001",
          chapter: "Delivery",
          type: VacationType.ANNUAL,
          start: "2026-03-05",
          end: "2026-03-06",
          isHalfDay: false,
          halfDayPart: null,
        },
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it("routes skill searches through the resource router path", async () => {
    const db = {
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_1",
            eid: "EMP-001",
            displayName: "Bruce Banner",
            chapter: "Delivery",
            skills: [{ skill: "Houdini FX", proficiency: 4, category: "FX" }],
          },
          {
            id: "res_2",
            eid: "EMP-002",
            displayName: "Tony Stark",
            chapter: "Tech",
            skills: [{ skill: "Nuke", proficiency: 5, category: "Comp" }],
          },
        ]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool(
      "search_by_skill",
      JSON.stringify({ skill: "houdini" }),
      ctx,
    );

    expect(db.resource.findMany).toHaveBeenCalledWith({
      where: { isActive: true },
      select: {
        id: true,
        eid: true,
        displayName: true,
        chapter: true,
        skills: true,
      },
    });
    expect(JSON.parse(result.content)).toEqual([
      {
        id: "res_1",
        eid: "EMP-001",
        name: "Bruce Banner",
        matchedSkill: "Houdini FX",
        level: 4,
        chapter: "Delivery",
      },
    ]);
  });

  it("routes availability checks through the allocation and vacation router paths", async () => {
    const db = {
      resource: {
        findUnique: vi.fn().mockResolvedValue({
          id: "res_1",
          eid: "EMP-001",
          displayName: "Bruce Banner",
          fte: 1,
          availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
          countryId: null,
          federalState: null,
          metroCityId: null,
          country: null,
          metroCity: null,
        }),
        findFirst: vi.fn(),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "assign_1",
            startDate: new Date("2026-04-01T00:00:00.000Z"),
            endDate: new Date("2026-04-01T00:00:00.000Z"),
            hoursPerDay: 4,
            status: "CONFIRMED",
            project: { name: "Gelddruckmaschine", shortCode: "GDM" },
          },
        ]),
      },
      vacation: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "vac_1",
            resourceId: "res_1",
            status: "APPROVED",
            type: VacationType.ANNUAL,
            startDate: new Date("2026-04-02T00:00:00.000Z"),
            endDate: new Date("2026-04-02T00:00:00.000Z"),
            isHalfDay: true,
            halfDayPart: "AFTERNOON",
            resource: {
              id: "res_1",
              displayName: "Bruce Banner",
              eid: "EMP-001",
              lcrCents: 8_000,
            },
            requestedBy: null,
            approvedBy: null,
          },
        ]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const result = await executeTool(
      "check_resource_availability",
      JSON.stringify({ resourceId: "res_1", startDate: "2026-04-01", endDate: "2026-04-02" }),
      ctx,
    );

    expect(db.assignment.findMany).toHaveBeenCalledWith({
      where: {
        resourceId: "res_1",
        status: { not: "CANCELLED" },
        startDate: { lte: new Date("2026-04-02T00:00:00.000Z") },
        endDate: { gte: new Date("2026-04-01T00:00:00.000Z") },
      },
      select: {
        id: true,
        startDate: true,
        endDate: true,
        hoursPerDay: true,
        status: true,
        project: { select: { name: true, shortCode: true } },
      },
      orderBy: { startDate: "asc" },
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        resource: "Bruce Banner",
        workingDays: 2,
        periodAvailableHours: 16,
        periodBookedHours: 4,
        periodRemainingHours: 12,
        availableHoursPerDay: 6,
        isFullyAvailable: false,
        existingAllocations: [
          {
            project: "Gelddruckmaschine (GDM)",
            hoursPerDay: 4,
            status: "CONFIRMED",
            start: "2026-04-01",
            end: "2026-04-01",
          },
        ],
        vacations: [
          {
            type: VacationType.ANNUAL,
            start: "2026-04-02",
            end: "2026-04-02",
            isHalfDay: true,
          },
        ],
      }),
    );
  });

  it("routes rate resolution through the rate-card router path", async () => {
    const db = {
      resource: {
        findUnique: vi.fn().mockResolvedValue({
          id: "res_1",
          eid: "EMP-001",
          displayName: "Bruce Banner",
          chapter: "Delivery",
          lcrCents: 8_000,
          areaRole: { name: "Pipeline TD" },
          managementLevel: { id: "ml_senior" },
        }),
        findFirst: vi.fn(),
      },
      role: {
        findFirst: vi.fn().mockResolvedValue({ id: "role_1" }),
      },
      rateCard: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "rc_2026",
            name: "Standard 2026",
            client: null,
            lines: [
              {
                id: "line_1",
                chapter: "Delivery",
                seniority: "Senior",
                costRateCents: 12_000,
                billRateCents: 18_000,
                role: { id: "role_1", name: "Pipeline TD" },
                managementLevelId: "ml_senior",
              },
            ],
          },
        ]),
        findUnique: vi.fn(),
      },
    };
    const ctx = createToolContext(db, {
      userRole: SystemRole.CONTROLLER,
      permissions: [PermissionKey.VIEW_COSTS],
    });

    const result = await executeTool(
      "resolve_rate",
      JSON.stringify({ resourceId: "res_1" }),
      ctx,
    );

    expect(db.resource.findUnique).toHaveBeenNthCalledWith(1, {
      where: { id: "res_1" },
      select: {
        id: true,
        eid: true,
        displayName: true,
        chapter: true,
        isActive: true,
      },
    });
    expect(db.resource.findUnique).toHaveBeenNthCalledWith(2, {
      where: { id: "res_1" },
      select: {
        id: true,
        displayName: true,
        chapter: true,
        areaRole: { select: { name: true } },
      },
    });
    expect(JSON.parse(result.content)).toEqual({
      rateCard: "Standard 2026",
      resource: "Bruce Banner",
      rate: "120,00 EUR",
      rateCents: 12000,
      matchedBy: "role: Pipeline TD",
    });
  });

  it("creates, updates, and deletes holiday calendars and entries through the real holiday router path", async () => {
    const db = {
      country: {
        findUnique: vi.fn().mockResolvedValue({ id: "country_de", name: "Germany" }),
      },
      metroCity: {
        findUnique: vi.fn().mockResolvedValue({ id: "city_muc", countryId: "country_de" }),
      },
      holidayCalendar: {
        findFirst: vi.fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce(null),
        findUnique: vi.fn()
          .mockResolvedValueOnce({
            id: "cal_de",
            name: "Germany National",
            scopeType: "COUNTRY",
            countryId: "country_de",
            stateCode: null,
            metroCityId: null,
          })
          .mockResolvedValueOnce({
            id: "cal_de",
            name: "Germany National",
          })
          .mockResolvedValueOnce({
            id: "cal_de",
            name: "Germany National Updated",
            entries: [],
          }),
        create: vi.fn().mockResolvedValue({
          id: "cal_de",
          name: "Germany National",
          scopeType: "COUNTRY",
          stateCode: null,
          isActive: true,
          priority: 0,
          country: { id: "country_de", code: "DE", name: "Germany" },
          metroCity: null,
          entries: [],
        }),
        update: vi.fn().mockResolvedValue({
          id: "cal_de",
          name: "Germany National Updated",
          scopeType: "COUNTRY",
          stateCode: null,
          isActive: false,
          priority: 1,
          country: { id: "country_de", code: "DE", name: "Germany" },
          metroCity: null,
          entries: [],
        }),
        delete: vi.fn().mockResolvedValue({ id: "cal_de" }),
      },
      holidayCalendarEntry: {
        findFirst: vi.fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce(null),
        findUnique: vi.fn()
          .mockResolvedValueOnce({
            id: "entry_1",
            name: "New Year",
            date: new Date("2026-01-01T00:00:00.000Z"),
            holidayCalendarId: "cal_de",
          })
          .mockResolvedValueOnce({
            id: "entry_1",
            name: "New Year Observed",
          }),
        create: vi.fn().mockResolvedValue({
          id: "entry_1",
          date: new Date("2026-01-01T00:00:00.000Z"),
          name: "New Year",
          isRecurringAnnual: true,
          source: "seed",
        }),
        update: vi.fn().mockResolvedValue({
          id: "entry_1",
          date: new Date("2026-01-02T00:00:00.000Z"),
          name: "New Year Observed",
          isRecurringAnnual: false,
          source: null,
        }),
        delete: vi.fn().mockResolvedValue({ id: "entry_1" }),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({ id: "audit_1" }),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const createCalendarResult = await executeTool(
      "create_holiday_calendar",
      JSON.stringify({ name: "Germany National", scopeType: "COUNTRY", countryId: "country_de" }),
      ctx,
    );
    const updateCalendarResult = await executeTool(
      "update_holiday_calendar",
      JSON.stringify({ id: "cal_de", data: { name: "Germany National Updated", isActive: false, priority: 1 } }),
      ctx,
    );
    const createEntryResult = await executeTool(
      "create_holiday_calendar_entry",
      JSON.stringify({
        holidayCalendarId: "cal_de",
        date: "2026-01-01",
        name: "New Year",
        isRecurringAnnual: true,
        source: "seed",
      }),
      ctx,
    );
    const updateEntryResult = await executeTool(
      "update_holiday_calendar_entry",
      JSON.stringify({
        id: "entry_1",
        data: { date: "2026-01-02", name: "New Year Observed", isRecurringAnnual: false, source: null },
      }),
      ctx,
    );
    const deleteEntryResult = await executeTool(
      "delete_holiday_calendar_entry",
      JSON.stringify({ id: "entry_1" }),
      ctx,
    );
    const deleteCalendarResult = await executeTool(
      "delete_holiday_calendar",
      JSON.stringify({ id: "cal_de" }),
      ctx,
    );

    expect(db.holidayCalendar.create).toHaveBeenCalledWith({
      data: {
        name: "Germany National",
        scopeType: "COUNTRY",
        countryId: "country_de",
        isActive: true,
        priority: 0,
      },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        entries: true,
      },
    });
    expect(db.holidayCalendar.update).toHaveBeenCalledWith({
      where: { id: "cal_de" },
      data: {
        name: "Germany National Updated",
        isActive: false,
        priority: 1,
      },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
    });
    expect(db.holidayCalendarEntry.create).toHaveBeenCalledWith({
      data: {
        holidayCalendarId: "cal_de",
        date: new Date("2026-01-01T00:00:00.000Z"),
        name: "New Year",
        isRecurringAnnual: true,
        source: "seed",
      },
    });
    expect(db.holidayCalendarEntry.update).toHaveBeenCalledWith({
      where: { id: "entry_1" },
      data: {
        date: new Date("2026-01-02T00:00:00.000Z"),
        name: "New Year Observed",
        isRecurringAnnual: false,
        source: null,
      },
    });
    expect(db.holidayCalendarEntry.delete).toHaveBeenCalledWith({ where: { id: "entry_1" } });
    expect(db.holidayCalendar.delete).toHaveBeenCalledWith({ where: { id: "cal_de" } });
    expect(JSON.parse(createCalendarResult.content)).toEqual(
      expect.objectContaining({ success: true, message: "Created holiday calendar: Germany National" }),
    );
    expect(JSON.parse(updateCalendarResult.content)).toEqual(
      expect.objectContaining({ success: true, message: "Updated holiday calendar: Germany National Updated" }),
    );
    expect(JSON.parse(createEntryResult.content)).toEqual(
      expect.objectContaining({ success: true, message: "Created holiday entry: New Year" }),
    );
    expect(JSON.parse(updateEntryResult.content)).toEqual(
      expect.objectContaining({ success: true, message: "Updated holiday entry: New Year Observed" }),
    );
    expect(JSON.parse(deleteEntryResult.content)).toEqual(
      expect.objectContaining({ success: true, message: "Deleted holiday entry: New Year Observed" }),
    );
    expect(JSON.parse(deleteCalendarResult.content)).toEqual(
      expect.objectContaining({ success: true, message: "Deleted holiday calendar: Germany National Updated" }),
    );
  });

  it("returns the expected assistant payloads for role, client, and org unit mutations", async () => {
    const db = {
      role: {
        findUnique: vi.fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce({ id: "role_1", name: "CG Artist", description: null, color: "#111111", isActive: true })
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce({
            id: "role_1",
            name: "Senior CG Artist",
            description: "Pipeline lead",
            color: "#222222",
            isActive: true,
            _count: { resourceRoles: 0 },
            resourceRoles: [],
          })
          .mockResolvedValueOnce({
            id: "role_1",
            name: "Senior CG Artist",
            description: "Pipeline lead",
            color: "#222222",
            isActive: true,
            _count: { resourceRoles: 0 },
            resourceRoles: [],
          })
          .mockResolvedValueOnce({
            id: "role_1",
            name: "Senior CG Artist",
            description: "Pipeline lead",
            color: "#222222",
            isActive: true,
            _count: { resourceRoles: 0 },
          }),
        create: vi.fn().mockResolvedValue({
          id: "role_1",
          name: "CG Artist",
          description: null,
          color: "#111111",
          isActive: true,
          _count: { resourceRoles: 0 },
        }),
        update: vi.fn().mockResolvedValue({
          id: "role_1",
          name: "Senior CG Artist",
          description: "Pipeline lead",
          color: "#222222",
          isActive: true,
          _count: { resourceRoles: 0 },
        }),
        delete: vi.fn().mockResolvedValue({ id: "role_1" }),
      },
      client: {
        findUnique: vi.fn(async ({
          where,
        }: {
          where: { id?: string; code?: string };
        }) => {
          if (where.id === "client_1") {
            return {
              id: "client_1",
              name: "Acme",
              code: "AC",
              sortOrder: 0,
              isActive: true,
              parentId: null,
              tags: [],
            };
          }

          return null;
        }),
        create: vi.fn().mockResolvedValue({
          id: "client_1",
          name: "Acme",
          code: "AC",
          parentId: null,
          sortOrder: 2,
          tags: ["auto"],
        }),
        update: vi.fn().mockResolvedValue({
          id: "client_1",
          name: "Acme Mobility",
          code: "ACM",
          parentId: null,
          sortOrder: 3,
          tags: ["auto", "priority"],
          isActive: true,
        }),
      },
      orgUnit: {
        findUnique: vi.fn(async ({
          where,
        }: {
          where: { id: string };
        }) => (
          where.id === "ou_1"
            ? {
              id: "ou_1",
              name: "Delivery",
              shortName: "DEL",
              level: 5,
              parentId: null,
              sortOrder: 1,
              isActive: true,
            }
            : null
        )),
        create: vi.fn().mockResolvedValue({
          id: "ou_1",
          name: "Delivery",
          shortName: "DEL",
          level: 5,
          parentId: null,
          sortOrder: 1,
          isActive: true,
        }),
        update: vi.fn().mockResolvedValue({
          id: "ou_1",
          name: "Delivery Europe",
          shortName: "DEU",
          level: 5,
          parentId: null,
          sortOrder: 2,
          isActive: true,
        }),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({ id: "audit_1" }),
      },
      demandRequirement: {
        groupBy: vi.fn().mockResolvedValue([]),
      },
      assignment: {
        groupBy: vi.fn().mockResolvedValue([]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const createRoleResult = await executeTool(
      "create_role",
      JSON.stringify({ name: "CG Artist", color: "#111111" }),
      ctx,
    );
    const updateRoleResult = await executeTool(
      "update_role",
      JSON.stringify({ id: "role_1", name: "Senior CG Artist", description: "Pipeline lead", color: "#222222" }),
      ctx,
    );
    const deleteRoleResult = await executeTool(
      "delete_role",
      JSON.stringify({ id: "role_1" }),
      ctx,
    );
    const createClientResult = await executeTool(
      "create_client",
      JSON.stringify({ name: "Acme", code: "AC", sortOrder: 2, tags: ["auto"] }),
      ctx,
    );
    const updateClientResult = await executeTool(
      "update_client",
      JSON.stringify({ id: "client_1", name: "Acme Mobility", code: "ACM", sortOrder: 3, tags: ["auto", "priority"] }),
      ctx,
    );
    const createOrgUnitResult = await executeTool(
      "create_org_unit",
      JSON.stringify({ name: "Delivery", shortName: "DEL", level: 5, sortOrder: 1 }),
      ctx,
    );
    const updateOrgUnitResult = await executeTool(
      "update_org_unit",
      JSON.stringify({ id: "ou_1", name: "Delivery Europe", shortName: "DEU", sortOrder: 2 }),
      ctx,
    );

    expect(JSON.parse(createRoleResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Created role: CG Artist",
    }));
    expect(JSON.parse(updateRoleResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Updated role: Senior CG Artist",
    }));
    expect(JSON.parse(deleteRoleResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Deleted role: Senior CG Artist",
    }));
    expect(JSON.parse(createClientResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Created client: Acme",
    }));
    expect(JSON.parse(updateClientResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Updated client: Acme Mobility",
    }));
    expect(JSON.parse(createOrgUnitResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Created org unit: Delivery",
    }));
    expect(JSON.parse(updateOrgUnitResult.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Updated org unit: Delivery Europe",
    }));
  });

  it("routes resource creation through the real resource router path and writes an audit log", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const resourceFindFirst = vi.fn().mockResolvedValue(null);
    const resourceCreate = vi.fn().mockResolvedValue({
      id: "res_1",
      eid: "EMP-001",
      displayName: "Carol Danvers",
      email: "carol@example.com",
      resourceRoles: [],
    });
    const ctx = createToolContext(
      {
        resource: {
          findFirst: resourceFindFirst,
          create: resourceCreate,
        },
        role: {
          findUnique: vi.fn().mockResolvedValue({ id: "role_1", name: "Designer", _count: { resourceRoles: 0 } }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        country: {
          findUnique: vi.fn().mockResolvedValue({ id: "country_de", code: "DE", name: "Germany", metroCities: [], _count: { resources: 0 } }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        orgUnit: {
          findUnique: vi.fn().mockResolvedValue({ id: "ou_1", name: "Delivery", shortName: "DEL", _count: { resources: 0 } }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        demandRequirement: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        auditLog: {
          create: auditCreate,
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_RESOURCES],
      },
    );

    const result = await executeTool(
      "create_resource",
      JSON.stringify({
        eid: "EMP-001",
        displayName: "Carol Danvers",
        email: "carol@example.com",
        lcrCents: 8000,
        roleName: "Designer",
        countryCode: "DE",
        orgUnitName: "Delivery",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Created resource: Carol Danvers (EMP-001)",
    }));
    expect(resourceFindFirst).toHaveBeenCalled();
    expect(resourceCreate).toHaveBeenCalledWith(expect.objectContaining({
      data: expect.objectContaining({
        roleId: "role_1",
        countryId: "country_de",
        orgUnitId: "ou_1",
      }),
    }));
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("routes project creation through the real project, blueprint, and client router paths", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const projectCreate = vi.fn().mockResolvedValue({
      id: "project_1",
      shortCode: "PROJ-1",
      name: "Project One",
      status: "DRAFT",
    });
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue({
            displayName: "Peter Parker",
          }),
          findMany: vi.fn().mockResolvedValue([]),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue(null),
          create: projectCreate,
        },
        blueprint: {
          findUnique: vi.fn().mockResolvedValue({ id: "bp_1", name: "Consulting Blueprint", target: "PROJECT", fieldDefs: [] }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        client: {
          findUnique: vi.fn().mockResolvedValue({ id: "client_1", name: "Acme", code: "ACME", _count: { projects: 0, children: 0 } }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        webhook: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        auditLog: {
          create: auditCreate,
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "create_project",
      JSON.stringify({
        shortCode: "PROJ-1",
        name: "Project One",
        orderType: "CHARGEABLE",
        budgetCents: 150000,
        startDate: "2026-05-01",
        endDate: "2026-06-30",
        responsiblePerson: "Peter Parker",
        blueprintName: "Consulting Blueprint",
        clientName: "ACME",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: expect.stringContaining("Created project: Project One (PROJ-1), budget "),
      projectId: "project_1",
    }));
    expect(projectCreate).toHaveBeenCalledWith(expect.objectContaining({
      data: expect.objectContaining({
        shortCode: "PROJ-1",
        blueprintId: "bp_1",
        clientId: "client_1",
        responsiblePerson: "Peter Parker",
      }),
    }));
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("returns a stable assistant error when the blueprint cannot be resolved", async () => {
    const projectCreate = vi.fn();
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue({
            displayName: "Peter Parker",
          }),
          findMany: vi.fn().mockResolvedValue([]),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue(null),
          create: projectCreate,
        },
        blueprint: {
          findUnique: vi.fn().mockResolvedValue(null),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        client: {
          findUnique: vi.fn().mockResolvedValue(null),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        auditLog: {
          create: vi.fn(),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "create_project",
      JSON.stringify({
        shortCode: "PROJ-404",
        name: "Missing Blueprint Project",
        orderType: "CHARGEABLE",
        budgetCents: 150000,
        startDate: "2026-05-01",
        endDate: "2026-06-30",
        responsiblePerson: "Peter Parker",
        blueprintName: "Missing Blueprint",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: 'Blueprint not found: "Missing Blueprint"',
    });
    expect(projectCreate).not.toHaveBeenCalled();
  });

  it("routes resource updates through the real resource router path and writes an audit log", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const resourceFindUnique = vi.fn()
      .mockResolvedValueOnce({
        id: "res_1",
        eid: "EMP-001",
        displayName: "Carol Danvers",
      })
      .mockResolvedValueOnce({
        id: "res_1",
        eid: "EMP-001",
        displayName: "Carol Danvers",
        dynamicFields: {},
        blueprintId: null,
      });
    const resourceUpdate = vi.fn().mockResolvedValue({
      id: "res_1",
      eid: "EMP-001",
      displayName: "Captain Marvel",
      resourceRoles: [],
    });
    const ctx = createToolContext(
      {
        resource: {
          findUnique: resourceFindUnique,
          update: resourceUpdate,
        },
        auditLog: {
          create: auditCreate,
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_RESOURCES],
      },
    );

    const result = await executeTool(
      "update_resource",
      JSON.stringify({
        id: "res_1",
        displayName: "Captain Marvel",
        chapter: "Delivery",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Updated resource Captain Marvel (EMP-001)",
      updatedFields: ["displayName", "chapter"],
    }));
    expect(resourceUpdate).toHaveBeenCalled();
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("returns a stable assistant error when the role cannot be resolved during resource creation", async () => {
    const resourceCreate = vi.fn();
    const ctx = createToolContext(
      {
        resource: {
          findFirst: vi.fn().mockResolvedValue(null),
          create: resourceCreate,
        },
        role: {
          findUnique: vi.fn().mockResolvedValue(null),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        demandRequirement: {
          findMany: vi.fn().mockResolvedValue([]),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_RESOURCES],
      },
    );

    const result = await executeTool(
      "create_resource",
      JSON.stringify({
        eid: "EMP-404",
        displayName: "Role Missing",
        email: "missing@example.com",
        lcrCents: 8000,
        roleName: "Missing Role",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: 'Role not found: "Missing Role"',
    });
    expect(resourceCreate).not.toHaveBeenCalled();
  });

  it("routes project updates through the real project router path and resolves short codes before updating", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const projectFindUnique = vi.fn()
      .mockResolvedValueOnce(null)
      .mockResolvedValueOnce({
        id: "project_1",
        shortCode: "PROJ-1",
        name: "Project One",
        status: "DRAFT",
        responsiblePerson: "Peter Parker",
      })
      .mockResolvedValueOnce({
        id: "project_1",
        shortCode: "PROJ-1",
        name: "Project One",
        status: "DRAFT",
        responsiblePerson: "Peter Parker",
        dynamicFields: {},
        blueprintId: null,
      });
    const projectUpdate = vi.fn().mockResolvedValue({
      id: "project_1",
      shortCode: "PROJ-1",
      name: "Project One Reloaded",
    });
    const ctx = createToolContext(
      {
        project: {
          findUnique: projectFindUnique,
          findFirst: vi.fn().mockResolvedValue(null),
          update: projectUpdate,
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        resource: {
          findFirst: vi.fn().mockResolvedValue({
            displayName: "Peter Parker",
          }),
          findMany: vi.fn().mockResolvedValue([]),
        },
        auditLog: {
          create: auditCreate,
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "update_project",
      JSON.stringify({
        id: "PROJ-1",
        name: "Project One Reloaded",
        responsiblePerson: "Peter Parker",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Updated project Project One Reloaded (PROJ-1)",
      updatedFields: ["name", "responsiblePerson"],
    }));
    expect(projectUpdate).toHaveBeenCalledWith(expect.objectContaining({
      where: { id: "project_1" },
      data: expect.objectContaining({
        name: "Project One Reloaded",
        responsiblePerson: "Peter Parker",
      }),
    }));
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("enforces admin-only project deletion through the real project router path", async () => {
    const ctx = createToolContext(
      {
        project: {
          findUnique: vi.fn().mockResolvedValue({
            id: "project_1",
            shortCode: "PROJ-1",
            name: "Project One",
            status: "ACTIVE",
            responsiblePerson: "Peter Parker",
          }),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
      },
      {
        userRole: SystemRole.MANAGER,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "delete_project",
      JSON.stringify({ projectId: "project_1" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      error: expect.stringContaining("Admin role required"),
    }));
  });

  it("routes project deletion through the real project router path for admins", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const tx = {
      assignment: { deleteMany: vi.fn().mockResolvedValue({ count: 0 }) },
      demandRequirement: { deleteMany: vi.fn().mockResolvedValue({ count: 0 }) },
      calculationRule: { updateMany: vi.fn().mockResolvedValue({ count: 0 }) },
      project: { delete: vi.fn().mockResolvedValue({ id: "project_1" }) },
      auditLog: { create: auditCreate },
    };
    const transaction = vi.fn().mockImplementation(async (callback: (inner: typeof tx) => Promise<unknown>) => callback(tx));
    const projectFindUnique = vi.fn()
      .mockResolvedValueOnce({
        id: "project_1",
        shortCode: "PROJ-1",
        name: "Project One",
        status: "ACTIVE",
        responsiblePerson: "Peter Parker",
      })
      .mockResolvedValueOnce({
        id: "project_1",
        shortCode: "PROJ-1",
        name: "Project One",
      });
    const ctx = createToolContext(
      {
        project: {
          findUnique: projectFindUnique,
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        $transaction: transaction,
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "delete_project",
      JSON.stringify({ projectId: "project_1" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Deleted project: Project One (PROJ-1)",
    }));
    expect(transaction).toHaveBeenCalledTimes(1);
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("routes demand creation through the real allocation router path and writes an audit log", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const notificationCreate = vi.fn().mockResolvedValue({ id: "task_1" });
    const demandCreate = vi.fn().mockResolvedValue({
      id: "demand_1",
      projectId: "project_1",
      roleId: "role_1",
      role: null,
      headcount: 2,
      hoursPerDay: 6,
      percentage: 75,
      budgetCents: 0,
      status: "PROPOSED",
      metadata: {},
      startDate: new Date("2026-05-01T00:00:00.000Z"),
      endDate: new Date("2026-05-15T00:00:00.000Z"),
      project: { id: "project_1", name: "Project One", shortCode: "PROJ-1" },
      roleEntity: { id: "role_1", name: "Designer", color: "#00AAFF" },
    });
    const tx = {
      project: {
        findUnique: vi.fn().mockResolvedValue({
          id: "project_1",
          name: "Project One",
          shortCode: "PROJ-1",
          budgetCents: 0,
        }),
      },
      demandRequirement: { create: demandCreate },
      auditLog: { create: auditCreate },
    };
    const transaction = vi.fn().mockImplementation(async (callback: (inner: typeof tx) => Promise<unknown>) => callback(tx));
    const projectFindFirst = vi.fn().mockResolvedValue({
      id: "project_1",
      name: "Project One",
      shortCode: "PROJ-1",
    });
    const projectFindUnique = vi.fn().mockResolvedValue({
      id: "project_1",
      name: "Project One",
      shortCode: "PROJ-1",
      budgetCents: 0,
    });
    const roleFindFirst = vi.fn().mockResolvedValue({ id: "role_1", name: "Designer" });
    const roleFindUnique = vi.fn().mockResolvedValue({ id: "role_1", name: "Designer" });
    const ctx = createToolContext(
      {
        project: {
          findFirst: projectFindFirst,
          findUnique: projectFindUnique,
        },
        role: {
          findFirst: roleFindFirst,
          findUnique: roleFindUnique,
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        demandRequirement: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        user: {
          findMany: vi.fn().mockResolvedValue([{ id: "manager_1" }]),
        },
        notification: {
          findFirst: vi.fn().mockResolvedValue(null),
          create: notificationCreate,
        },
        $transaction: transaction,
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_ALLOCATIONS],
      },
    );

    const result = await executeTool(
      "create_demand",
      JSON.stringify({
        projectId: "PROJ-1",
        roleName: "Designer",
        headcount: 2,
        hoursPerDay: 6,
        startDate: "2026-05-01",
        endDate: "2026-05-15",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Created demand: Designer × 2 for Project One (PROJ-1), 6h/day, 2026-05-01 to 2026-05-15",
      demandId: "demand_1",
    }));
    expect(demandCreate).toHaveBeenCalledWith(expect.objectContaining({
      data: expect.objectContaining({
        projectId: "project_1",
        roleId: "role_1",
        headcount: 2,
        hoursPerDay: 6,
        percentage: 75,
      }),
    }));
    expect(auditCreate).toHaveBeenCalledTimes(1);
    expect(notificationCreate).toHaveBeenCalledTimes(1);
  });

  it("routes demand filling through the real allocation router path", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const assignmentCreate = vi.fn().mockResolvedValue({
      id: "assignment_1",
      demandRequirementId: "demand_1",
      resourceId: "resource_1",
      projectId: "project_1",
      startDate: new Date("2026-05-01T00:00:00.000Z"),
      endDate: new Date("2026-05-15T00:00:00.000Z"),
      hoursPerDay: 6,
      percentage: 75,
      role: "Designer",
      roleId: "role_1",
      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: { id: "role_1", name: "Designer", color: "#00AAFF" },
      demandRequirement: {
        id: "demand_1",
        projectId: "project_1",
        startDate: new Date("2026-05-01T00:00:00.000Z"),
        endDate: new Date("2026-05-15T00:00:00.000Z"),
        hoursPerDay: 6,
        percentage: 75,
        role: "Designer",
        roleId: "role_1",
        headcount: 1,
        status: "PROPOSED",
      },
    });
    const txDemandFindUnique = vi.fn().mockResolvedValue({
      id: "demand_1",
      projectId: "project_1",
    });
    const txDemandUpdate = vi.fn().mockResolvedValue({
      id: "demand_1",
      projectId: "project_1",
      headcount: 1,
      status: "COMPLETED",
    });
    const tx = {
      project: {
        findUnique: vi.fn().mockResolvedValue({ id: "project_1" }),
      },
      resource: {
        findUnique: vi.fn().mockResolvedValue({
          id: "resource_1",
          lcrCents: 7000,
          availability: {
            monday: 8,
            tuesday: 8,
            wednesday: 8,
            thursday: 8,
            friday: 8,
            saturday: 0,
            sunday: 0,
          },
        }),
      },
      demandRequirement: {
        findUnique: txDemandFindUnique,
        update: txDemandUpdate,
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([]),
        create: assignmentCreate,
      },
      vacation: {
        findMany: vi.fn().mockResolvedValue([]),
      },
      auditLog: {
        create: auditCreate,
      },
    };
    const transaction = vi.fn().mockImplementation(async (callback: (inner: typeof tx) => Promise<unknown>) => callback(tx));
    const demandRecord = {
      id: "demand_1",
      projectId: "project_1",
      startDate: new Date("2026-05-01T00:00:00.000Z"),
      endDate: new Date("2026-05-15T00:00:00.000Z"),
      hoursPerDay: 6,
      percentage: 75,
      role: "Designer",
      roleId: "role_1",
      headcount: 1,
      status: "PROPOSED",
      metadata: {},
      roleEntity: { name: "Designer" },
      project: { id: "project_1", name: "Project One", shortCode: "PROJ-1" },
    };
    const demandFindUnique = vi.fn()
      .mockResolvedValueOnce(demandRecord)
      .mockResolvedValueOnce(demandRecord);
    const resourceFindUnique = vi.fn().mockResolvedValue({
      id: "resource_1",
      displayName: "Carol Danvers",
      lcrCents: 7000,
      availability: {
        monday: 8,
        tuesday: 8,
        wednesday: 8,
        thursday: 8,
        friday: 8,
        saturday: 0,
        sunday: 0,
      },
    });
    const ctx = createToolContext(
      {
        demandRequirement: {
          findUnique: demandFindUnique,
        },
        project: {
          findUnique: vi.fn().mockResolvedValue({
            id: "project_1",
            name: "Project One",
            shortCode: "PROJ-1",
            budgetCents: 100000,
          }),
        },
        user: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        notification: {
          findFirst: vi.fn().mockResolvedValue(null),
          create: vi.fn(),
        },
        resource: {
          findUnique: resourceFindUnique,
          findFirst: vi.fn().mockResolvedValue(null),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        $transaction: transaction,
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_ALLOCATIONS],
      },
    );

    const result = await executeTool(
      "fill_demand",
      JSON.stringify({ demandId: "demand_1", resourceId: "resource_1" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Assigned Carol Danvers to Designer on Project One (PROJ-1)",
      assignmentId: "assignment_1",
    }));
    expect(assignmentCreate).toHaveBeenCalledTimes(1);
    expect(txDemandUpdate).toHaveBeenCalledWith(expect.objectContaining({
      where: { id: "demand_1" },
      data: { status: "COMPLETED" },
    }));
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("returns a stable assistant error when the role cannot be resolved during demand creation", async () => {
    const ctx = createToolContext(
      {
        project: {
          findUnique: vi.fn()
            .mockResolvedValueOnce(null)
            .mockResolvedValueOnce({
              id: "project_1",
              name: "Project One",
              shortCode: "PROJ-1",
              status: "ACTIVE",
              responsiblePerson: "Peter Parker",
            }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        role: {
          findUnique: vi.fn().mockResolvedValue(null),
          findFirst: vi.fn().mockResolvedValue(null),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_ALLOCATIONS],
      },
    );

    const result = await executeTool(
      "create_demand",
      JSON.stringify({
        projectId: "PROJ-1",
        roleName: "Missing Role",
        headcount: 2,
        hoursPerDay: 6,
        startDate: "2026-05-01",
        endDate: "2026-05-15",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "Role not found: Missing Role",
    });
  });

  it("routes allocation creation through the real allocation router path", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const assignmentCreate = vi.fn().mockResolvedValue({
      id: "assignment_1",
      demandRequirementId: null,
      resourceId: "resource_1",
      projectId: "project_1",
      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,
    });
    const tx = {
      project: {
        findUnique: vi.fn().mockResolvedValue({ id: "project_1" }),
      },
      resource: {
        findUnique: vi.fn().mockResolvedValue({
          id: "resource_1",
          lcrCents: 7000,
          availability: {
            monday: 8,
            tuesday: 8,
            wednesday: 8,
            thursday: 8,
            friday: 8,
            saturday: 0,
            sunday: 0,
          },
        }),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([]),
        create: assignmentCreate,
      },
      vacation: {
        findMany: vi.fn().mockResolvedValue([]),
      },
      auditLog: {
        create: auditCreate,
      },
    };
    const transaction = vi.fn().mockImplementation(async (callback: (inner: typeof tx) => Promise<unknown>) => callback(tx));
    const ctx = createToolContext(
      {
        resource: {
          findUnique: vi.fn()
            .mockResolvedValue({
              id: "resource_1",
              displayName: "Carol Danvers",
              eid: "EMP-001",
              lcrCents: 7000,
            }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue({
            id: "project_1",
            name: "Project One",
            shortCode: "PROJ-1",
            status: "ACTIVE",
            responsiblePerson: "Peter Parker",
            budgetCents: 0,
          }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
          findUnique: vi.fn().mockResolvedValue(null),
        },
        $transaction: transaction,
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_ALLOCATIONS],
      },
    );

    const result = await executeTool(
      "create_allocation",
      JSON.stringify({
        resourceId: "resource_1",
        projectId: "project_1",
        startDate: "2026-06-01",
        endDate: "2026-06-05",
        hoursPerDay: 6,
        role: "Designer",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Created allocation: Carol Danvers → Project One (PROJ-1), 6h/day, 2026-06-01 to 2026-06-05",
      allocationId: "assignment_1",
      status: "PROPOSED",
    }));
    expect(ctx.db.resource.findUnique).toHaveBeenCalledWith({
      where: { id: "resource_1" },
      select: {
        id: true,
        displayName: true,
        eid: true,
        chapter: true,
        isActive: true,
      },
    });
    expect(ctx.db.project.findUnique).toHaveBeenCalledWith({
      where: { id: "project_1" },
      select: expect.objectContaining({
        id: true,
        shortCode: true,
        name: true,
      }),
    });
    expect(ctx.db.assignment.findMany).toHaveBeenCalledWith(expect.objectContaining({
      where: {
        resourceId: "resource_1",
        projectId: "project_1",
      },
      orderBy: { startDate: "asc" },
      include: expect.objectContaining({
        resource: {
          select: expect.objectContaining({
            id: true,
            displayName: true,
            eid: true,
            chapter: true,
            lcrCents: true,
          }),
        },
        project: {
          select: expect.objectContaining({
            id: true,
            name: true,
            shortCode: true,
            status: true,
          }),
        },
      }),
    }));
    expect(assignmentCreate).toHaveBeenCalledTimes(1);
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("routes allocation cancellation through the real allocation router path", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const assignmentUpdate = vi.fn().mockResolvedValue({
      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: "CANCELLED",
      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,
    });
    const tx = {
      assignment: {
        findUnique: vi.fn().mockResolvedValue({
          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,
        }),
        update: assignmentUpdate,
      },
      auditLog: {
        create: auditCreate,
      },
    };
    const transaction = vi.fn().mockImplementation(async (callback: (inner: typeof tx) => Promise<unknown>) => callback(tx));
    const ctx = createToolContext(
      {
        assignment: {
          findUnique: vi.fn().mockResolvedValue({
            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,
          }),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue({
            id: "project_1",
            name: "Project One",
            shortCode: "PROJ-1",
            budgetCents: 0,
          }),
        },
        webhook: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        $transaction: transaction,
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_ALLOCATIONS],
      },
    );

    const result = await executeTool(
      "cancel_allocation",
      JSON.stringify({ allocationId: "assignment_1" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Cancelled allocation: Carol Danvers → Project One (PROJ-1), 2026-06-01 to 2026-06-05",
    }));
    expect(ctx.db.assignment.findUnique).toHaveBeenCalledWith({
      where: { id: "assignment_1" },
      include: expect.objectContaining({
        resource: expect.any(Object),
        project: expect.any(Object),
      }),
    });
    expect(assignmentUpdate).toHaveBeenCalledWith(expect.objectContaining({
      where: { id: "assignment_1" },
      data: expect.objectContaining({
        status: "CANCELLED",
      }),
    }));
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("returns a stable assistant error for duplicate allocations", async () => {
    const ctx = createToolContext(
      {
        resource: {
          findUnique: vi.fn()
            .mockResolvedValueOnce({
              id: "resource_1",
              eid: "EMP-001",
              displayName: "Carol Danvers",
            })
            .mockResolvedValueOnce({
              id: "resource_1",
              eid: "EMP-001",
              displayName: "Carol Danvers",
              lcrCents: 7000,
              availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
            }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        project: {
          findUnique: vi.fn()
            .mockResolvedValueOnce(null)
            .mockResolvedValueOnce({
              id: "project_1",
              name: "Project One",
              shortCode: "PROJ-1",
              status: "ACTIVE",
              responsiblePerson: "Peter Parker",
            }),
          findFirst: vi.fn().mockResolvedValue(null),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "assignment_existing",
              resourceId: "resource_1",
              projectId: "project_1",
              startDate: new Date("2026-06-01T00:00:00.000Z"),
              endDate: new Date("2026-06-05T00:00:00.000Z"),
              status: "PROPOSED",
              resource: { id: "resource_1", displayName: "Carol Danvers", eid: "EMP-001" },
              project: { id: "project_1", name: "Project One", shortCode: "PROJ-1" },
            },
          ]),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_ALLOCATIONS],
      },
    );

    const result = await executeTool(
      "create_allocation",
      JSON.stringify({
        resourceId: "resource_1",
        projectId: "project_1",
        startDate: "2026-06-01",
        endDate: "2026-06-05",
        hoursPerDay: 6,
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "Allocation already exists for this resource/project/dates. No new allocation created.",
    });
  });

  it("routes allocation status updates through the real allocation router path", async () => {
    const auditCreate = vi.fn().mockResolvedValue({ id: "audit_1" });
    const assignmentUpdate = vi.fn().mockResolvedValue({
      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: "ACTIVE",
      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,
    });
    const tx = {
      assignment: {
        findUnique: vi.fn().mockResolvedValue({
          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,
        }),
        update: assignmentUpdate,
      },
      auditLog: {
        create: auditCreate,
      },
    };
    const transaction = vi.fn().mockImplementation(async (callback: (inner: typeof tx) => Promise<unknown>) => callback(tx));
    const ctx = createToolContext(
      {
        assignment: {
          findUnique: vi.fn().mockResolvedValue({
            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,
          }),
        },
        project: {
          findUnique: vi.fn().mockResolvedValue({
            id: "project_1",
            name: "Project One",
            shortCode: "PROJ-1",
            budgetCents: 0,
          }),
        },
        webhook: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        $transaction: transaction,
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_ALLOCATIONS],
      },
    );

    const result = await executeTool(
      "update_allocation_status",
      JSON.stringify({ allocationId: "assignment_1", newStatus: "ACTIVE" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Updated allocation status: Carol Danvers → Project One (PROJ-1), 2026-06-01 to 2026-06-05: PROPOSED → ACTIVE",
    }));
    expect(ctx.db.assignment.findUnique).toHaveBeenCalledWith({
      where: { id: "assignment_1" },
      include: expect.objectContaining({
        resource: expect.any(Object),
        project: expect.any(Object),
      }),
    });
    expect(assignmentUpdate).toHaveBeenCalledWith(expect.objectContaining({
      where: { id: "assignment_1" },
      data: expect.objectContaining({
        status: "ACTIVE",
      }),
    }));
    expect(auditCreate).toHaveBeenCalledTimes(1);
  });

  it("routes project cover generation through the real project router path", async () => {
    const projectFindUnique = vi.fn()
      .mockResolvedValueOnce({
        id: "project_1",
        name: "Project One",
        shortCode: "PROJ-1",
        status: "ACTIVE",
        orderType: "CHARGEABLE",
        allocationType: "INT",
        budgetCents: 0,
        winProbability: 100,
        startDate: new Date("2026-05-01T00:00:00.000Z"),
        endDate: new Date("2026-06-30T00:00:00.000Z"),
        responsiblePerson: "Peter Parker",
        client: null,
        utilizationCategory: null,
        _count: { assignments: 0, estimates: 0 },
      })
      .mockResolvedValueOnce({
        id: "project_1",
        name: "Project One",
        client: { name: "Wayne Enterprises" },
      });
    const projectUpdate = vi.fn().mockResolvedValue({
      id: "project_1",
      coverImageUrl: "data:image/png;base64,ZmFrZQ==",
    });
    const ctx = createToolContext(
      {
        project: {
          findUnique: projectFindUnique,
          update: projectUpdate,
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        systemSettings: {
          findUnique: vi.fn().mockResolvedValue({
            id: "singleton",
            imageProvider: "dalle",
            aiProvider: "openai",
            azureOpenAiApiKey: "sk-test",
            azureOpenAiDeployment: "gpt-4o-mini",
          }),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "generate_project_cover",
      JSON.stringify({ projectId: "project_1", prompt: "Blue studio lighting" }),
      ctx,
    );

    expect(projectUpdate).toHaveBeenCalledWith({
      where: { id: "project_1" },
      data: { coverImageUrl: "data:image/png;base64,ZmFrZQ==" },
    });
    expect(projectFindUnique).toHaveBeenCalledWith({
      where: { id: "project_1" },
      select: expect.objectContaining({
        id: true,
        shortCode: true,
        name: true,
        status: true,
        responsiblePerson: true,
        startDate: true,
        endDate: true,
      }),
    });
    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: 'Generated cover art for project "Project One"',
    }));
  });

  it("routes project cover removal through the real project router path", async () => {
    const projectFindUnique = vi.fn()
      .mockResolvedValueOnce({
        id: "project_1",
        name: "Project One",
        shortCode: "PROJ-1",
        status: "ACTIVE",
        orderType: "CHARGEABLE",
        allocationType: "INT",
        budgetCents: 0,
        winProbability: 100,
        startDate: new Date("2026-05-01T00:00:00.000Z"),
        endDate: new Date("2026-06-30T00:00:00.000Z"),
        responsiblePerson: "Peter Parker",
        client: null,
        utilizationCategory: null,
        _count: { assignments: 0, estimates: 0 },
      })
      .mockResolvedValueOnce({
        id: "project_1",
        name: "Project One",
      });
    const projectUpdate = vi.fn().mockResolvedValue({
      id: "project_1",
      coverImageUrl: null,
    });
    const ctx = createToolContext(
      {
        project: {
          findUnique: projectFindUnique,
          update: projectUpdate,
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
      },
      {
        userRole: SystemRole.ADMIN,
        permissions: [PermissionKey.MANAGE_PROJECTS],
      },
    );

    const result = await executeTool(
      "remove_project_cover",
      JSON.stringify({ projectId: "project_1" }),
      ctx,
    );

    expect(projectUpdate).toHaveBeenCalledWith({
      where: { id: "project_1" },
      data: { coverImageUrl: null },
    });
    expect(projectFindUnique).toHaveBeenCalledWith({
      where: { id: "project_1" },
      select: expect.objectContaining({
        id: true,
        shortCode: true,
        name: true,
        status: true,
        responsiblePerson: true,
        startDate: true,
        endDate: true,
      }),
    });
    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: 'Removed cover art from project "Project One"',
    }));
  });

  it("routes project narrative generation through the real insights router path", async () => {
    const projectFindUnique = vi.fn().mockResolvedValue({
      id: "project_1",
      name: "Project One",
      shortCode: "PROJ-1",
      status: "ACTIVE",
      startDate: new Date("2026-03-01T00:00:00.000Z"),
      endDate: new Date("2026-06-30T00:00:00.000Z"),
      budgetCents: 250_000_00,
      dynamicFields: { existingFlag: true },
      demandRequirements: [
        {
          headcount: 2,
          _count: { assignments: 1 },
        },
      ],
      assignments: [
        {
          status: "ACTIVE",
          dailyCostCents: 42_000,
          startDate: new Date("2026-03-10T00:00:00.000Z"),
          endDate: new Date("2026-03-20T00:00:00.000Z"),
          resource: { displayName: "Carol Danvers" },
        },
      ],
    });
    const projectUpdate = vi.fn().mockResolvedValue({
      id: "project_1",
      dynamicFields: {
        existingFlag: true,
        aiNarrative: "Project is on track overall, but staffing remains partially open.",
      },
    });
    const ctx = createToolContext(
      {
        project: {
          findUnique: projectFindUnique,
          update: projectUpdate,
        },
        systemSettings: {
          findUnique: vi.fn().mockResolvedValue({
            id: "singleton",
            aiProvider: "openai",
            azureOpenAiApiKey: "sk-test",
            azureOpenAiDeployment: "gpt-4o-mini",
          }),
        },
      },
      {
        userRole: SystemRole.CONTROLLER,
      },
    );

    const result = await executeTool(
      "generate_project_narrative",
      JSON.stringify({ projectId: "project_1" }),
      ctx,
    );

    expect(projectFindUnique).toHaveBeenCalledWith({
      where: { id: "project_1" },
      include: {
        demandRequirements: {
          select: {
            id: true,
            role: true,
            headcount: true,
            hoursPerDay: true,
            startDate: true,
            endDate: true,
            status: true,
            _count: { select: { assignments: true } },
          },
        },
        assignments: {
          select: {
            id: true,
            role: true,
            hoursPerDay: true,
            startDate: true,
            endDate: true,
            status: true,
            dailyCostCents: true,
            resource: { select: { displayName: true } },
          },
        },
      },
    });
    expect(projectUpdate).toHaveBeenCalledWith({
      where: { id: "project_1" },
      data: {
        dynamicFields: expect.objectContaining({
          existingFlag: true,
          aiNarrative: "Project is on track overall, but staffing remains partially open.",
          aiNarrativeGeneratedAt: expect.any(String),
        }),
      },
    });
    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      narrative: "Project is on track overall, but staffing remains partially open.",
      generatedAt: expect.any(String),
    }));
  });
});
