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,
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
  };
});

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

describe("assistant audit log list tool", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("lists audit entries through the real audit router path", async () => {
    const ctx = createToolContext(
      {
        auditLog: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "audit_1",
              entityType: "Project",
              entityId: "project_1",
              entityName: "Gelddruckmaschine",
              action: "UPDATE",
              userId: "user_1",
              source: "ui",
              summary: "Updated project dates",
              createdAt: new Date("2026-03-28T10:00:00.000Z"),
              user: {
                id: "user_1",
                name: "Larissa",
                email: "larissa@example.com",
              },
            },
          ]),
        },
      },
      { userRole: SystemRole.CONTROLLER },
    );

    const result = await executeTool(
      "list_audit_log_entries",
      JSON.stringify({
        entityType: "Project",
        search: "Gelddruckmaschine",
        limit: 10,
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      filters: {
        entityType: "Project",
        entityId: null,
        userId: null,
        action: null,
        source: null,
        startDate: null,
        endDate: null,
        search: "Gelddruckmaschine",
      },
      itemCount: 1,
      nextCursor: null,
      items: [
        {
          id: "audit_1",
          entityType: "Project",
          entityId: "project_1",
          entityName: "Gelddruckmaschine",
          action: "UPDATE",
          userId: "user_1",
          source: "ui",
          summary: "Updated project dates",
          createdAt: "2026-03-28T10:00:00.000Z",
          user: {
            id: "user_1",
            name: "Larissa",
            email: "larissa@example.com",
          },
        },
      ],
    });
  });
});
