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

describe("DomainSelector", (): void => {
  const mockDomains: Domain[] = [
    {
      id: "domain-1",
      workspaceId: "workspace-1",
      name: "Work",
      slug: "work",
      description: "Work-related tasks",
      color: "#3B82F6",
      icon: "💼",
      sortOrder: 0,
      metadata: {},
      createdAt: new Date("2026-01-28"),
      updatedAt: new Date("2026-01-28"),
    },
    {
      id: "domain-2",
      workspaceId: "workspace-1",
      name: "Personal",
      slug: "personal",
      description: null,
      color: "#10B981",
      icon: null,
      sortOrder: 1,
      metadata: {},
      createdAt: new Date("2026-01-28"),
      updatedAt: new Date("2026-01-28"),
    },
  ];

  it("should render with default placeholder", (): void => {
    const onChange = vi.fn();
    render(<DomainSelector domains={mockDomains} value={null} onChange={onChange} />);
    expect(screen.getByText("Select a domain")).toBeInTheDocument();
  });

  it("should render with custom placeholder", (): void => {
    const onChange = vi.fn();
    render(
      <DomainSelector
        domains={mockDomains}
        value={null}
        onChange={onChange}
        placeholder="Choose domain"
      />
    );
    expect(screen.getByText("Choose domain")).toBeInTheDocument();
  });

  it("should render all domains as options", (): void => {
    const onChange = vi.fn();
    render(<DomainSelector domains={mockDomains} value={null} onChange={onChange} />);
    expect(screen.getByText("💼 Work")).toBeInTheDocument();
    expect(screen.getByText("Personal")).toBeInTheDocument();
  });

  it("should call onChange when selection changes", async (): Promise<void> => {
    const user = userEvent.setup();
    const onChange = vi.fn();

    render(<DomainSelector domains={mockDomains} value={null} onChange={onChange} />);

    const select = screen.getByRole("combobox");
    await user.selectOptions(select, "domain-1");

    expect(onChange).toHaveBeenCalledWith("domain-1");
  });

  it("should call onChange with null when cleared", async (): Promise<void> => {
    const user = userEvent.setup();
    const onChange = vi.fn();

    render(<DomainSelector domains={mockDomains} value="domain-1" onChange={onChange} />);

    const select = screen.getByRole("combobox");
    await user.selectOptions(select, "");

    expect(onChange).toHaveBeenCalledWith(null);
  });

  it("should show selected value", (): void => {
    const onChange = vi.fn();
    render(<DomainSelector domains={mockDomains} value="domain-1" onChange={onChange} />);

    // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
    const select = screen.getByRole("combobox") as HTMLSelectElement;
    expect(select.value).toBe("domain-1");
  });

  it("should apply custom className", (): void => {
    const onChange = vi.fn();
    render(
      <DomainSelector
        domains={mockDomains}
        value={null}
        onChange={onChange}
        className="custom-class"
      />
    );

    const select = screen.getByRole("combobox");
    expect(select.className).toContain("custom-class");
  });
});
