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,
    countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }),
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
  };
});

import { countPlanningEntries } from "@capakraken/application";
import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-master-data-mutation-test-helpers.js";

describe("assistant client delete tool - errors", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(countPlanningEntries).mockResolvedValue({ countsByRoleId: new Map() });
  });

  it("returns a stable error when deleting a missing client", async () => {
    const ctx = createToolContext(
      {
        client: {
          findUnique: vi.fn().mockResolvedValue(null),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

    const result = await executeTool(
      "delete_client",
      JSON.stringify({ id: "client_missing" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        error: "Client not found with the given criteria.",
      }),
    );
  });

  it("returns a stable error when deleting a client that still has projects", async () => {
    const clientRecord = {
      id: "client_1",
      name: "Acme Mobility",
      code: "ACM",
      parentId: null,
      isActive: true,
      sortOrder: 3,
      tags: ["auto", "priority"],
      _count: { projects: 2, children: 0 },
    };
    const ctx = createToolContext(
      {
        client: {
          findUnique: vi.fn().mockResolvedValueOnce(clientRecord).mockResolvedValueOnce(clientRecord),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

    const result = await executeTool(
      "delete_client",
      JSON.stringify({ id: "client_1" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        error: "Client cannot be deleted while it still has projects. Deactivate it instead.",
      }),
    );
  });

  it("returns a stable error when deleting a client that still has child clients", async () => {
    const clientRecord = {
      id: "client_1",
      name: "Acme Mobility",
      code: "ACM",
      parentId: null,
      isActive: true,
      sortOrder: 3,
      tags: ["auto", "priority"],
      _count: { projects: 0, children: 2 },
    };
    const ctx = createToolContext(
      {
        client: {
          findUnique: vi.fn().mockResolvedValueOnce(clientRecord).mockResolvedValueOnce(clientRecord),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

    const result = await executeTool(
      "delete_client",
      JSON.stringify({ id: "client_1" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        error:
          "Client cannot be deleted while it still has child clients. Remove or reassign them first.",
      }),
    );
  });
});
