/**
 * AggregatedDataGrid Component Tests
 */

import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import { AggregatedDataGrid } from "./AggregatedDataGrid";
import { TaskStatus, TaskPriority } from "@mosaic/shared";
import type { FederatedTask, FederatedEvent } from "./types";

const mockTasks: FederatedTask[] = [
  {
    task: {
      id: "task-1",
      title: "Task from Work",
      description: "Work task",
      status: TaskStatus.IN_PROGRESS,
      priority: TaskPriority.HIGH,
      dueDate: new Date("2026-02-05"),
      creatorId: "user-1",
      assigneeId: "user-1",
      workspaceId: "workspace-1",
      projectId: null,
      parentId: null,
      sortOrder: 0,
      metadata: {},
      completedAt: null,
      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",
    },
  },
  {
    task: {
      id: "task-2",
      title: "Task from Home",
      description: "Home task",
      status: TaskStatus.NOT_STARTED,
      priority: TaskPriority.MEDIUM,
      dueDate: new Date("2026-02-06"),
      creatorId: "user-1",
      assigneeId: "user-1",
      workspaceId: "workspace-2",
      projectId: null,
      parentId: null,
      sortOrder: 0,
      metadata: {},
      completedAt: null,
      createdAt: new Date("2026-02-03"),
      updatedAt: new Date("2026-02-03"),
    },
    provenance: {
      instanceId: "instance-home-001",
      instanceName: "Home Instance",
      instanceUrl: "https://mosaic.home.example.com",
      timestamp: "2026-02-03T14:00:00Z",
    },
  },
];

const mockEvents: FederatedEvent[] = [
  {
    event: {
      id: "event-1",
      title: "Meeting from Work",
      description: "Team standup",
      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("AggregatedDataGrid", () => {
  it("should render tasks", () => {
    render(<AggregatedDataGrid tasks={mockTasks} events={[]} />);

    expect(screen.getByText("Task from Work")).toBeInTheDocument();
    expect(screen.getByText("Task from Home")).toBeInTheDocument();
  });

  it("should render events", () => {
    render(<AggregatedDataGrid tasks={[]} events={mockEvents} />);

    expect(screen.getByText("Meeting from Work")).toBeInTheDocument();
  });

  it("should render both tasks and events", () => {
    render(<AggregatedDataGrid tasks={mockTasks} events={mockEvents} />);

    expect(screen.getByText("Task from Work")).toBeInTheDocument();
    expect(screen.getByText("Meeting from Work")).toBeInTheDocument();
  });

  it("should show loading state", () => {
    render(<AggregatedDataGrid tasks={[]} events={[]} isLoading={true} />);

    expect(screen.getByText("Loading data from instances...")).toBeInTheDocument();
  });

  it("should show empty state when no data", () => {
    render(<AggregatedDataGrid tasks={[]} events={[]} />);

    expect(screen.getByText("No data available from connected instances")).toBeInTheDocument();
  });

  it("should show error message", () => {
    render(<AggregatedDataGrid tasks={[]} events={[]} error="Unable to reach work instance" />);

    expect(screen.getByText("Unable to reach work instance")).toBeInTheDocument();
  });

  it("should render with custom className", () => {
    const { container } = render(
      <AggregatedDataGrid tasks={mockTasks} events={[]} className="custom-class" />
    );

    expect(container.querySelector(".custom-class")).toBeInTheDocument();
  });

  it("should show instance provenance indicators", () => {
    render(<AggregatedDataGrid tasks={mockTasks} events={[]} />);

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

  it("should render with compact mode", () => {
    const { container } = render(
      <AggregatedDataGrid tasks={mockTasks} events={[]} compact={true} />
    );

    // Check that cards have compact padding
    const compactCards = container.querySelectorAll(".p-3");
    expect(compactCards.length).toBeGreaterThan(0);
  });
});
