/**
 * FederatedTaskCard Component Tests
 */

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

const mockTask: FederatedTask = {
  task: {
    id: "task-1",
    title: "Review pull request",
    description: "Review and provide feedback on frontend PR",
    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",
  },
};

describe("FederatedTaskCard", () => {
  it("should render task title", () => {
    render(<FederatedTaskCard federatedTask={mockTask} />);

    expect(screen.getByText("Review pull request")).toBeInTheDocument();
  });

  it("should render task description", () => {
    render(<FederatedTaskCard federatedTask={mockTask} />);

    expect(screen.getByText("Review and provide feedback on frontend PR")).toBeInTheDocument();
  });

  it("should render provenance indicator", () => {
    render(<FederatedTaskCard federatedTask={mockTask} />);

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

  it("should render status badge", () => {
    render(<FederatedTaskCard federatedTask={mockTask} />);

    expect(screen.getByText("In Progress")).toBeInTheDocument();
  });

  it("should render priority indicator for high priority", () => {
    render(<FederatedTaskCard federatedTask={mockTask} />);

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

  it("should render target date", () => {
    render(<FederatedTaskCard federatedTask={mockTask} />);

    // Check for "Target:" text followed by a date
    expect(screen.getByText(/Target:/)).toBeInTheDocument();
    expect(screen.getByText(/2026/)).toBeInTheDocument();
  });

  it("should render with compact mode", () => {
    const { container } = render(<FederatedTaskCard federatedTask={mockTask} compact={true} />);

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

  it("should render completed task with completed status", () => {
    const completedTask: FederatedTask = {
      ...mockTask,
      task: {
        ...mockTask.task,
        status: TaskStatus.COMPLETED,
        completedAt: new Date("2026-02-04"),
      },
    };

    render(<FederatedTaskCard federatedTask={completedTask} />);

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

  it("should handle task without description", () => {
    const taskNoDesc: FederatedTask = {
      ...mockTask,
      task: {
        ...mockTask.task,
        description: null,
      },
    };

    render(<FederatedTaskCard federatedTask={taskNoDesc} />);

    expect(screen.getByText("Review pull request")).toBeInTheDocument();
    expect(
      screen.queryByText("Review and provide feedback on frontend PR")
    ).not.toBeInTheDocument();
  });

  it("should handle task without target date", () => {
    const taskNoTarget: FederatedTask = {
      ...mockTask,
      task: {
        ...mockTask.task,
        dueDate: null,
      },
    };

    render(<FederatedTaskCard federatedTask={taskNoTarget} />);

    expect(screen.getByText("Review pull request")).toBeInTheDocument();
    expect(screen.queryByText(/Target:/)).not.toBeInTheDocument();
  });

  it("should use PDA-friendly language for status", () => {
    const pausedTask: FederatedTask = {
      ...mockTask,
      task: {
        ...mockTask.task,
        status: TaskStatus.PAUSED,
      },
    };

    render(<FederatedTaskCard federatedTask={pausedTask} />);

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