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 country list tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("lists countries with schedule rules, active state, and metro cities", async () => {
    const findMany = vi.fn().mockResolvedValue([
      {
        id: "country_de",
        code: "DE",
        name: "Deutschland",
        dailyWorkingHours: 8,
        scheduleRules: null,
        isActive: true,
        metroCities: [{ id: "city_muc", name: "Munich" }],
      },
      {
        id: "country_es",
        code: "ES",
        name: "Spain",
        dailyWorkingHours: 8,
        scheduleRules: null,
        isActive: true,
        metroCities: [{ id: "city_mad", name: "Madrid" }],
      },
    ]);
    const ctx = createToolContext({
      country: {
        findMany,
      },
    });

    const result = await executeTool(
      "list_countries",
      JSON.stringify({ search: "deu" }),
      ctx,
    );

    const parsed = JSON.parse(result.content) as {
      count: number;
      countries: Array<{
        code: string;
        isActive: boolean;
        metroCities: Array<{ id: string; name: string }>;
        cities: string[];
      }>;
    };

    expect(findMany).toHaveBeenCalledWith({
      where: { isActive: true },
      include: { metroCities: { orderBy: { name: "asc" } } },
      orderBy: { name: "asc" },
    });
    expect(parsed.count).toBe(1);
    expect(parsed.countries[0]).toMatchObject({
      code: "DE",
      isActive: true,
      cities: ["Munich"],
      metroCities: [{ id: "city_muc", name: "Munich" }],
    });
  });
});
