import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";

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

import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-audit-task-test-helpers.js";

describe("assistant audit read tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

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