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 summary tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("routes entitlement summary through the entitlement year summary workflow", async () => {
    const db = {
      systemSettings: {
        findUnique: vi.fn().mockResolvedValue({ vacationDefaultDays: 28 }),
      },
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_1",
            displayName: "Alice Example",
            eid: "EMP-001",
            lcrCents: 0,
            chapter: "Delivery",
            federalState: "BY",
            country: { code: "DE", name: "Germany" },
            metroCity: { name: "Muenchen" },
          },
          {
            id: "res_2",
            displayName: "Bob Example",
            eid: "EMP-002",
            lcrCents: 0,
            chapter: "CGI",
            federalState: "HH",
            country: { code: "DE", name: "Germany" },
            metroCity: { name: "Hamburg" },
          },
        ]),
      },
      vacationEntitlement: {
        findUnique: vi.fn().mockResolvedValue(null),
        create: vi.fn().mockImplementation(async (args?: any) => ({
          id: `ent_${args?.data?.resourceId ?? "unknown"}_${args?.data?.year ?? "unknown"}`,
          resourceId: args?.data?.resourceId ?? "res_1",
          year: args?.data?.year ?? 2026,
          entitledDays: args?.data?.entitledDays ?? 28,
          carryoverDays: args?.data?.carryoverDays ?? 0,
          usedDays: args?.data?.usedDays ?? 0,
          pendingDays: args?.data?.pendingDays ?? 0,
        })),
        update: vi.fn().mockImplementation(async (args?: any) => ({
          id: args?.where?.id ?? "ent_unknown",
          resourceId: args?.where?.id?.includes("res_2") ? "res_2" : "res_1",
          year: 2026,
          entitledDays: 28,
          carryoverDays: 0,
          usedDays: args?.data?.usedDays ?? 0,
          pendingDays: args?.data?.pendingDays ?? 0,
        })),
      },
      vacation: {
        findMany: vi.fn().mockResolvedValue([]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.ADMIN });

    const result = await executeTool(
      "get_entitlement_summary",
      JSON.stringify({ year: 2026, resourceName: "alice" }),
      ctx,
    );

    expect(db.resource.findMany).toHaveBeenCalledWith({
      where: { isActive: true },
      select: {
        id: true,
        displayName: true,
        eid: true,
        lcrCents: true,
        chapter: true,
        federalState: true,
        country: { select: { code: true, name: true } },
        metroCity: { select: { name: true } },
      },
      orderBy: [{ chapter: "asc" }, { displayName: "asc" }],
    });
    expect(JSON.parse(result.content)).toEqual([
      {
        resource: "Alice Example",
        eid: "EMP-001",
        chapter: "Delivery",
        countryCode: "DE",
        countryName: "Germany",
        federalState: "BY",
        metroCityName: "Muenchen",
        year: 2026,
        entitled: 28,
        carryover: 0,
        used: 0,
        pending: 0,
        remaining: 28,
      },
    ]);
  });
});
