import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { DomainTagInput } from "./DomainTagInput";

describe("DomainTagInput", () => {
  it("shows a clear duplicate tenant warning and adds the domain after confirmation", async () => {
    const user = userEvent.setup();
    const onChange = vi.fn();
    const onConfirmedConflictsChange = vi.fn();

    render(
      <DomainTagInput
        value={[]}
        onChange={onChange}
        tenants={[
          {
            id: "tenant-1",
            name: "한맥가족",
            slug: "hanmac-family",
            type: "COMPANY",
            description: "",
            status: "active",
            domains: ["samaneng.com"],
            memberCount: 0,
            createdAt: "",
            updatedAt: "",
          },
        ]}
        currentTenantId="tenant-2"
        confirmedConflicts={[]}
        onConfirmedConflictsChange={onConfirmedConflictsChange}
        placeholder="example.com"
      />,
    );

    await user.type(
      screen.getByPlaceholderText("example.com"),
      "samaneng.com ",
    );

    expect(
      await screen.findByText(
        "samaneng.com 도메인은 한맥가족 테넌트에 이미 설정되어 있습니다. 그래도 현재 테넌트에도 추가하시겠습니까?",
      ),
    ).toBeInTheDocument();

    await user.click(screen.getByRole("button", { name: "계속 진행" }));

    expect(onChange).toHaveBeenCalledWith(["samaneng.com"]);
    expect(onConfirmedConflictsChange).toHaveBeenCalledWith(["samaneng.com"]);
  });
});
