import { beforeEach, describe, expect, it, vi } from "vitest";

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

describe("assistant metro city mutation tools - errors", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns a stable error when creating a metro city for a missing country", async () => {
    const ctx = createToolContext({
      country: {
        findUnique: vi.fn().mockResolvedValue(null),
      },
    });

    const result = await executeTool(
      "create_metro_city",
      JSON.stringify({ countryId: "country_missing", name: "Munich" }),
      ctx,
    );

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

  it("returns a stable error when updating a missing metro city", async () => {
    const ctx = createToolContext({
      metroCity: {
        findUnique: vi.fn().mockResolvedValue(null),
      },
    });

    const result = await executeTool(
      "update_metro_city",
      JSON.stringify({ id: "city_missing", data: { name: "Hamburg-Mitte" } }),
      ctx,
    );

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

  it("returns a stable error when deleting a metro city that is still assigned", async () => {
    const ctx = createToolContext({
      metroCity: {
        findUnique: vi.fn().mockResolvedValue({
          id: "city_ham",
          name: "Hamburg",
          _count: { resources: 3 },
        }),
      },
    });

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

    expect(JSON.parse(result.content)).toEqual({
      error: "Metro city cannot be deleted while it is still assigned to resources.",
    });
  });
});
