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 {
  createHappyPathDb,
  createToolContext,
  executeTool,
} from "./assistant-tools-vacation-mutation-test-helpers.js";

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

  it("approves and rejects vacation through the real vacation router path", async () => {
    const db = createHappyPathDb();
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

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

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

  it("allows self-service vacation cancellation through the real vacation router path", async () => {
    const db = createHappyPathDb();
    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" },
    });
  });
});
