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

describe("assistant resource detail tool", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("routes resource detail reads through the resource router path", async () => {
    const db = {
      resource: {
        findUnique: vi.fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce({
            id: "res_1",
            eid: "EMP-001",
            displayName: "Bruce Banner",
            chapter: "Delivery",
            isActive: true,
          })
          .mockResolvedValueOnce({
            id: "res_1",
            eid: "EMP-001",
            displayName: "Bruce Banner",
            email: "bruce@example.com",
            chapter: "Delivery",
            fte: 1,
            lcrCents: 8_500,
            ucrCents: 10_500,
            chargeabilityTarget: 80,
            isActive: true,
            availability: { monday: 8 },
            skills: [{ name: "Houdini", level: 5 }],
            postalCode: "80331",
            federalState: "BY",
            areaRole: { name: "Pipeline TD", color: "#112233" },
            country: { code: "DE", name: "Germany", dailyWorkingHours: 8 },
            metroCity: { name: "Munich" },
            managementLevelGroup: { name: "Senior", targetPercentage: 75 },
            orgUnit: { name: "Operations", level: 5 },
            _count: { assignments: 4, vacations: 2 },
          }),
        findFirst: vi.fn(),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const detailResult = await executeTool(
      "get_resource",
      JSON.stringify({ identifier: "EMP-001" }),
      ctx,
    );

    expect(db.resource.findUnique).toHaveBeenNthCalledWith(1, {
      where: { id: "EMP-001" },
      select: {
        id: true,
        eid: true,
        displayName: true,
        chapter: true,
        isActive: true,
      },
    });
    expect(db.resource.findUnique).toHaveBeenNthCalledWith(2, {
      where: { eid: "EMP-001" },
      select: {
        id: true,
        eid: true,
        displayName: true,
        chapter: true,
        isActive: true,
      },
    });
    expect(db.resource.findUnique).toHaveBeenNthCalledWith(3, {
      where: { id: "res_1" },
      select: {
        id: true,
        eid: true,
        displayName: true,
        email: true,
        chapter: true,
        fte: true,
        lcrCents: true,
        ucrCents: true,
        chargeabilityTarget: true,
        isActive: true,
        availability: true,
        skills: true,
        postalCode: true,
        federalState: true,
        areaRole: { select: { name: true, color: true } },
        country: { select: { code: true, name: true, dailyWorkingHours: true } },
        metroCity: { select: { name: true } },
        managementLevelGroup: { select: { name: true, targetPercentage: true } },
        orgUnit: { select: { name: true, level: true } },
        _count: { select: { assignments: true, vacations: true } },
      },
    });
    expect(JSON.parse(detailResult.content)).toEqual({
      id: "res_1",
      eid: "EMP-001",
      name: "Bruce Banner",
      email: "bruce@example.com",
      chapter: "Delivery",
      role: "Pipeline TD",
      country: "Germany",
      countryCode: "DE",
      countryHours: 8,
      metroCity: "Munich",
      fte: 1,
      lcr: "85,00 EUR",
      ucr: "105,00 EUR",
      chargeabilityTarget: "80%",
      managementLevel: "Senior",
      orgUnit: "Operations",
      postalCode: "80331",
      federalState: "BY",
      active: true,
      totalAssignments: 4,
      totalVacations: 2,
      skillCount: 1,
      topSkills: ["Houdini (5)"],
    });
  });
});
