import { beforeEach, describe, expect, it, vi } from "vitest";
import { PermissionKey } from "@capakraken/shared";
import {
  createToolContext,
  executeTool,
} from "./assistant-tools-resource-test-helpers.js";

describe("assistant resource read tools - computation graph", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns a filtered resource computation graph through the assistant", async () => {
    const resourceRecord = {
      id: "resource_augsburg",
      displayName: "Bruce Banner",
      eid: "bruce.banner",
      fte: 1,
      lcrCents: 5_000,
      chargeabilityTarget: 80,
      countryId: "country_de",
      federalState: "BY",
      metroCityId: "city_augsburg",
      availability: {
        monday: 8,
        tuesday: 8,
        wednesday: 8,
        thursday: 8,
        friday: 8,
        saturday: 0,
        sunday: 0,
      },
      country: {
        id: "country_de",
        code: "DE",
        name: "Deutschland",
        dailyWorkingHours: 8,
        scheduleRules: null,
      },
      metroCity: { id: "city_augsburg", name: "Augsburg" },
      managementLevelGroup: {
        id: "mlg_1",
        name: "Senior",
        targetPercentage: 0.8,
      },
    };

    const ctx = createToolContext(
      {
        resource: {
          findUnique: vi.fn().mockResolvedValue(resourceRecord),
          findFirst: vi.fn(),
          findUniqueOrThrow: vi.fn().mockResolvedValue(resourceRecord),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        vacation: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        holidayCalendar: {
          findMany: vi.fn().mockResolvedValue([]),
        },
        calculationRule: {
          findMany: vi.fn().mockResolvedValue([]),
        },
      },
      {
        permissions: [PermissionKey.VIEW_COSTS, PermissionKey.USE_ASSISTANT_ADVANCED_TOOLS],
      },
    );

    const result = await executeTool(
      "get_resource_computation_graph",
      JSON.stringify({
        resourceId: "resource_augsburg",
        month: "2026-08",
        domain: "SAH",
      }),
      ctx,
    );

    const parsed = JSON.parse(result.content) as {
      resource: { id: string; displayName: string };
      requestedDomain: string;
      totalNodeCount: number;
      selectedNodeCount: number;
      nodes: Array<{ id: string; domain: string }>;
      meta: {
        countryCode: string | null;
        federalState: string | null;
        metroCityName: string | null;
        resolvedHolidays: Array<{ name: string; scope: string }>;
      };
    };

    expect(parsed.resource).toEqual({
      id: "resource_augsburg",
      eid: "bruce.banner",
      displayName: "Bruce Banner",
    });
    expect(parsed.requestedDomain).toBe("SAH");
    expect(parsed.totalNodeCount).toBeGreaterThan(parsed.selectedNodeCount);
    expect(parsed.selectedNodeCount).toBeGreaterThan(0);
    expect(parsed.nodes.every((node) => node.domain === "SAH")).toBe(true);
    expect(parsed.meta).toMatchObject({
      countryCode: "DE",
      federalState: "BY",
      metroCityName: "Augsburg",
    });
    expect(parsed.meta.resolvedHolidays).toEqual(expect.arrayContaining([
      expect.objectContaining({
        name: "Augsburger Friedensfest",
        scope: "CITY",
      }),
    ]));
  });
});
