import { beforeEach, describe, expect, it, vi } from "vitest";
import { countPlanningEntries } from "@capakraken/application";
import { SystemRole } from "@capakraken/shared";

vi.mock("@capakraken/application", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@capakraken/application")>();
  return {
    ...actual,
    countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }),
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
  };
});

import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-master-data-read-test-helpers.js";

describe("assistant master data effort and experience read tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(countPlanningEntries).mockResolvedValue({ countsByRoleId: new Map() });
  });

  it("routes effort rule and experience multiplier reads through their backing routers", async () => {
    const db = {
      effortRuleSet: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "ers_default",
            name: "Default Effort",
            isDefault: true,
            rules: [
              {
                id: "eff_1",
                description: "Animation per shot",
                scopeType: "SHOT",
                discipline: "Animation",
                chapter: "3D",
                unitMode: "per_item",
                hoursPerUnit: 12,
                sortOrder: 0,
              },
            ],
          },
        ]),
      },
      experienceMultiplierSet: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "ems_default",
            name: "Default Multipliers",
            isDefault: true,
            rules: [
              {
                id: "exp_1",
                description: "Senior DE uplift",
                chapter: "3D",
                location: "DE",
                level: "Senior",
                costMultiplier: 1.2,
                billMultiplier: 1.15,
                shoringRatio: 0.4,
                additionalEffortRatio: 0.1,
                sortOrder: 0,
              },
            ],
          },
        ]),
      },
    };
    const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });

    const effortRulesResult = await executeTool("list_effort_rules", "{}", ctx);
    const experienceMultipliersResult = await executeTool("list_experience_multipliers", "{}", ctx);

    expect(db.effortRuleSet.findMany).toHaveBeenCalledWith({
      include: {
        rules: { orderBy: { sortOrder: "asc" } },
      },
      orderBy: [{ isDefault: "desc" }, { name: "asc" }],
    });
    expect(db.experienceMultiplierSet.findMany).toHaveBeenCalledWith({
      include: {
        rules: { orderBy: { sortOrder: "asc" } },
      },
      orderBy: [{ isDefault: "desc" }, { name: "asc" }],
    });

    expect(JSON.parse(effortRulesResult.content)).toEqual([
      {
        id: "eff_1",
        description: "Animation per shot",
        scopeType: "SHOT",
        discipline: "Animation",
        chapter: "3D",
        unitMode: "per_item",
        hoursPerUnit: 12,
        sortOrder: 0,
        ruleSet: { name: "Default Effort", isDefault: true },
      },
    ]);
    expect(JSON.parse(experienceMultipliersResult.content)).toEqual([
      {
        id: "exp_1",
        description: "Senior DE uplift",
        chapter: "3D",
        location: "DE",
        level: "Senior",
        costMultiplier: 1.2,
        billMultiplier: 1.15,
        shoringRatio: 0.4,
        additionalEffortRatio: 0.1,
        sortOrder: 0,
        multiplierSet: { name: "Default Multipliers", isDefault: true },
      },
    ]);
  });
});
