import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import SwipeDeck from "./SwipeDeck";
import {
  TEST_RESTAURANT,
  TEST_RESTAURANT_2,
  TEST_RESTAURANT_3,
} from "@/__tests__/helpers/fixtures";

vi.mock("./SwipeableCard", () => ({
  default: ({ restaurant }: { restaurant: { name: string } }) => (
    <div data-testid="swipeable-card">{restaurant.name}</div>
  ),
}));

vi.mock("./ActionButtons", () => ({
  default: ({ disabled }: { disabled: boolean }) => (
    <div data-testid="action-buttons" data-disabled={disabled} />
  ),
}));

vi.mock("./MatchResult", () => ({
  default: ({ restaurant }: { restaurant: { name: string } }) => (
    <div data-testid="match-result">{restaurant.name}</div>
  ),
}));

vi.mock("./SwipeGuide", () => ({
  default: () => <div data-testid="swipe-guide" />,
}));

vi.mock("./UserAvatar", () => ({
  default: () => <span data-testid="user-avatar" />,
}));

const restaurants = [TEST_RESTAURANT, TEST_RESTAURANT_2, TEST_RESTAURANT_3];

const defaultProps = {
  restaurants,
  roomId: "ROOM01",
  userId: "user-1",
  initialIndex: 0,
  matchedRestaurantId: null,
  matchType: null as const,
  matchLikes: 0,
  runnerUps: [],
  likeCounts: {},
  swipeCounts: {},
  userCount: 2,
  userProfiles: {},
  onReset: vi.fn().mockResolvedValue(undefined),
  onNarrow: vi.fn().mockResolvedValue(undefined),
};

describe("SwipeDeck", () => {
  it("renders restaurant cards at initial index", () => {
    render(<SwipeDeck {...defaultProps} />);
    const cards = screen.getAllByTestId("swipeable-card");
    expect(cards.length).toBeGreaterThan(0);
  });

  it("renders action buttons", () => {
    render(<SwipeDeck {...defaultProps} />);
    expect(screen.getByTestId("action-buttons")).toBeInTheDocument();
  });

  it("shows swipe guide at index 0", () => {
    render(<SwipeDeck {...defaultProps} />);
    expect(screen.getByTestId("swipe-guide")).toBeInTheDocument();
  });

  it("does not show swipe guide when initialIndex > 0", () => {
    render(<SwipeDeck {...defaultProps} initialIndex={1} />);
    expect(screen.queryByTestId("swipe-guide")).not.toBeInTheDocument();
  });

  it("shows match result when matchedRestaurantId is set", () => {
    render(
      <SwipeDeck
        {...defaultProps}
        matchedRestaurantId={TEST_RESTAURANT.id}
        matchType="unanimous"
        matchLikes={2}
      />,
    );
    expect(screen.getByTestId("match-result")).toBeInTheDocument();
  });

  it("disables action buttons when match exists", () => {
    render(
      <SwipeDeck
        {...defaultProps}
        matchedRestaurantId={TEST_RESTAURANT.id}
        matchType="unanimous"
      />,
    );
    const actions = screen.getByTestId("action-buttons");
    expect(actions.getAttribute("data-disabled")).toBe("true");
  });
});
