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-vacation-entitlement-test-helpers.js";

describe("assistant vacation entitlement mutation tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("sets entitlement through the real entitlement workflow", async () => {
    const db = {
      resource: {
        findUnique: vi.fn().mockResolvedValue({
          id: "res_1",
          eid: "EMP-001",
          displayName: "Alice Example",
          chapter: "Delivery",
        }),
        findFirst: vi.fn().mockResolvedValue(null),
      },
      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 ?? 2027,
          entitledDays: args?.data?.entitledDays ?? 32,
          carryoverDays: 0,
          usedDays: 0,
          pendingDays: 0,
        })),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const result = await executeTool(
      "set_entitlement",
      JSON.stringify({ resourceId: "res_1", year: 2027, entitledDays: 32 }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
      success: true,
      message: "Set entitlement for Alice Example (2027): 32 days",
    }));
    expect(db.vacationEntitlement.create).toHaveBeenCalledWith(expect.objectContaining({
      data: expect.objectContaining({
        resourceId: "res_1",
        year: 2027,
        entitledDays: 32,
      }),
    }));
  });
});
