import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { EmbedConfigModal } from "./EmbedConfigModal";

describe("EmbedConfigModal", () => {
  const mockBuildPublicLink = vi.fn((type) => `https://example.com/${type}`);
  const mockOnClose = vi.fn();

  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("renders when open", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    expect(screen.getByText("Configure integration")).toBeInTheDocument();
  });

  it("does not render when closed", () => {
    render(
      <EmbedConfigModal
        isOpen={false}
        onClose={mockOnClose}
        buildPublicLink={mockBuildPublicLink}
      />
    );
    expect(screen.queryByText("Configure integration")).not.toBeInTheDocument();
  });

  it("displays title", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    expect(screen.getByText("Configure integration")).toBeInTheDocument();
  });

  it("displays configuration labels", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    expect(screen.getByText("Integration type")).toBeInTheDocument();
    expect(screen.getByText("Button color")).toBeInTheDocument();
  });

  it("displays preview link section", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    expect(screen.getByText("Preview link")).toBeInTheDocument();
  });

  it("displays embed code section", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    expect(screen.getByText("Embed code")).toBeInTheDocument();
  });

  it("displays close button", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    expect(screen.getAllByText("Close")[0]).toBeInTheDocument();
  });

  it("displays preview button", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    expect(screen.getByText("Preview")).toBeInTheDocument();
  });

  it("displays copy functionality", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    // Verify the embed code section exists
    expect(screen.getByText("Embed code")).toBeInTheDocument();
  });

  it("calls onClose when close button is clicked", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    fireEvent.click(screen.getAllByText("Close")[0]);
    expect(mockOnClose).toHaveBeenCalled();
  });

  it("calls buildPublicLink to generate URL", () => {
    render(
      <EmbedConfigModal isOpen={true} onClose={mockOnClose} buildPublicLink={mockBuildPublicLink} />
    );
    // buildPublicLink should be called to generate the embed URL
    expect(mockBuildPublicLink).toHaveBeenCalled();
  });
});
