/**
 * FederatedEventCard Component Tests
 */

import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import { FederatedEventCard } from "./FederatedEventCard";
import type { FederatedEvent } from "./types";

const mockEvent: FederatedEvent = {
  event: {
    id: "event-1",
    title: "Team standup",
    description: "Daily sync meeting",
    startTime: new Date("2026-02-05T10:00:00"),
    endTime: new Date("2026-02-05T10:30:00"),
    allDay: false,
    location: "Zoom",
    recurrence: null,
    creatorId: "user-1",
    workspaceId: "workspace-1",
    projectId: null,
    metadata: {},
    createdAt: new Date("2026-02-03"),
    updatedAt: new Date("2026-02-03"),
  },
  provenance: {
    instanceId: "instance-work-001",
    instanceName: "Work Instance",
    instanceUrl: "https://mosaic.work.example.com",
    timestamp: "2026-02-03T14:00:00Z",
  },
};

describe("FederatedEventCard", () => {
  it("should render event title", () => {
    render(<FederatedEventCard federatedEvent={mockEvent} />);

    expect(screen.getByText("Team standup")).toBeInTheDocument();
  });

  it("should render event description", () => {
    render(<FederatedEventCard federatedEvent={mockEvent} />);

    expect(screen.getByText("Daily sync meeting")).toBeInTheDocument();
  });

  it("should render provenance indicator", () => {
    render(<FederatedEventCard federatedEvent={mockEvent} />);

    expect(screen.getByText("Work Instance")).toBeInTheDocument();
  });

  it("should render event time", () => {
    render(<FederatedEventCard federatedEvent={mockEvent} />);

    // Check for time components
    expect(screen.getByText(/10:00/)).toBeInTheDocument();
  });

  it("should render event location", () => {
    render(<FederatedEventCard federatedEvent={mockEvent} />);

    expect(screen.getByText("Zoom")).toBeInTheDocument();
  });

  it("should render with compact mode", () => {
    const { container } = render(<FederatedEventCard federatedEvent={mockEvent} compact={true} />);

    const card = container.querySelector(".p-3");
    expect(card).toBeInTheDocument();
  });

  it("should handle event without description", () => {
    const eventNoDesc: FederatedEvent = {
      ...mockEvent,
      event: {
        ...mockEvent.event,
        description: null,
      },
    };

    render(<FederatedEventCard federatedEvent={eventNoDesc} />);

    expect(screen.getByText("Team standup")).toBeInTheDocument();
    expect(screen.queryByText("Daily sync meeting")).not.toBeInTheDocument();
  });

  it("should handle event without location", () => {
    const eventNoLocation: FederatedEvent = {
      ...mockEvent,
      event: {
        ...mockEvent.event,
        location: null,
      },
    };

    render(<FederatedEventCard federatedEvent={eventNoLocation} />);

    expect(screen.getByText("Team standup")).toBeInTheDocument();
    expect(screen.queryByText("Zoom")).not.toBeInTheDocument();
  });

  it("should render all-day event", () => {
    const allDayEvent: FederatedEvent = {
      ...mockEvent,
      event: {
        ...mockEvent.event,
        allDay: true,
      },
    };

    render(<FederatedEventCard federatedEvent={allDayEvent} />);

    expect(screen.getByText("All day")).toBeInTheDocument();
  });

  it("should handle onClick callback", () => {
    let clicked = false;
    const handleClick = (): void => {
      clicked = true;
    };

    const { container } = render(
      <FederatedEventCard federatedEvent={mockEvent} onClick={handleClick} />
    );

    const card = container.querySelector(".cursor-pointer");
    expect(card).toBeInTheDocument();

    if (card instanceof HTMLElement) {
      card.click();
    }
    expect(clicked).toBe(true);
  });
});
