import { beforeEach, describe, expect, it, vi } from "vitest";

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

vi.mock("../lib/audit.js", () => ({
  createAuditEntry: vi.fn().mockResolvedValue(undefined),
}));

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

describe("assistant team vacation overlap tool", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("routes team vacation overlap through the vacation router path", async () => {
    const db = {
      resource: {
        findUnique: vi.fn().mockResolvedValue({
          id: "res_1",
          displayName: "Bruce Banner",
          eid: "BB-1",
          chapter: "CGI",
          lcrCents: 0,
          isActive: true,
          countryId: null,
          federalState: null,
          metroCityId: null,
          areaRole: null,
          country: null,
          metroCity: null,
        }),
        findFirst: vi.fn(),
        findMany: vi.fn(),
      },
      vacation: {
        findMany: vi.fn().mockResolvedValue([
          {
            type: "ANNUAL",
            status: "APPROVED",
            startDate: new Date("2026-08-10T00:00:00.000Z"),
            endDate: new Date("2026-08-12T00:00:00.000Z"),
            resource: { displayName: "Clark Kent" },
          },
        ]),
      },
    };
    const ctx = createToolContext(db);

    const result = await executeTool(
      "get_team_vacation_overlap",
      JSON.stringify({
        resourceId: "res_1",
        startDate: "2026-08-10",
        endDate: "2026-08-12",
      }),
      ctx,
    );

    expect(db.vacation.findMany).toHaveBeenCalledWith({
      where: {
        resource: { chapter: "CGI" },
        resourceId: { not: "res_1" },
        status: { in: ["APPROVED", "PENDING"] },
        startDate: { lte: new Date("2026-08-12T00:00:00.000Z") },
        endDate: { gte: new Date("2026-08-10T00:00:00.000Z") },
      },
      include: {
        resource: { select: expect.any(Object) },
      },
      orderBy: { startDate: "asc" },
      take: 20,
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        resource: "Bruce Banner",
        chapter: "CGI",
        overlapCount: 1,
        overlappingVacations: [
          expect.objectContaining({
            resource: "Clark Kent",
            status: "APPROVED",
          }),
        ],
      }),
    );
  });
});
