import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";

import { ProductBuyBox, type ProductBuyBoxProps } from "../product-buy-box";

const baseProps: ProductBuyBoxProps = {
  itemId: "item-1",
  title: "Vintage Tee",
  price: 2500,
  originalRetail: 5000,
  status: "active",
  storeCity: "Portland",
  storeState: "OR",
  conditionLabel: "Excellent Used Condition",
  conditionClassName: "bg-forest-100 text-forest-800",
  shippingSummary: "Free shipping over $100.00",
  returnsSummary: "14-day returns",
  addToCartPending: false,
  onAddToCart: vi.fn(),
  onSaveForLater: vi.fn(),
};

function renderBuyBox(overrides: Partial<ProductBuyBoxProps> = {}) {
  const props = {
    ...baseProps,
    onAddToCart: vi.fn(),
    onSaveForLater: vi.fn(),
    ...overrides,
  };

  render(<ProductBuyBox {...props} />);

  return props;
}

describe("ProductBuyBox", () => {
  it("renders the core product, price, condition, availability, and reassurance content", () => {
    renderBuyBox();

    expect(
      screen.getByRole("region", { name: /Vintage Tee purchase options/i }),
    ).not.toHaveClass("md:sticky");
    expect(screen.getByRole("heading", { name: "Vintage Tee" })).toBeInTheDocument();
    expect(screen.getByText("$25.00")).toBeInTheDocument();
    expect(screen.getByText("$50.00")).toBeInTheDocument();
    expect(screen.getByText("Save 50%")).toBeInTheDocument();
    expect(screen.getByText("Excellent Used Condition")).toBeInTheDocument();
    expect(screen.getByText("Only 1 available")).toBeInTheDocument();
    expect(screen.getByText(/Ships from/i)).toBeInTheDocument();
    expect(screen.getByText("Portland, OR")).toBeInTheDocument();
    expect(screen.getByText("Free shipping over $100.00")).toBeInTheDocument();
    expect(screen.getByText("14-day returns")).toBeInTheDocument();
    expect(screen.getByText("Buyer protection")).toBeInTheDocument();
    expect(screen.getByText("Not as described? You're covered.")).toBeInTheDocument();
    expect(screen.getByText("Secure checkout")).toBeInTheDocument();
  });

  it("calls add-to-cart and save handlers with the item id", () => {
    const onAddToCart = vi.fn();
    const onSaveForLater = vi.fn();
    renderBuyBox({ onAddToCart, onSaveForLater });

    fireEvent.click(screen.getByRole("button", { name: "Add to Cart" }));
    fireEvent.click(screen.getByRole("button", { name: "Save for Later" }));

    expect(onAddToCart).toHaveBeenCalledTimes(1);
    expect(onAddToCart).toHaveBeenCalledWith("item-1");
    expect(onSaveForLater).toHaveBeenCalledTimes(1);
    expect(onSaveForLater).toHaveBeenCalledWith("item-1");
  });

  it("disables add-to-cart while pending", () => {
    const onAddToCart = vi.fn();
    renderBuyBox({ addToCartPending: true, onAddToCart });

    const addButton = screen.getByRole("button", { name: "Add to Cart" });

    expect(addButton).toBeDisabled();
    expect(addButton).toHaveAttribute("aria-busy", "true");
    fireEvent.click(addButton);
    expect(onAddToCart).not.toHaveBeenCalled();
  });

  it("does not show one-available messaging or allow purchase for inactive items", () => {
    const onAddToCart = vi.fn();
    renderBuyBox({ status: "sold", onAddToCart });

    expect(screen.queryByText("Only 1 available")).not.toBeInTheDocument();
    expect(screen.getByText("Sold")).toBeInTheDocument();

    const addButton = screen.getByRole("button", { name: "Add to Cart" });
    expect(addButton).toBeDisabled();
    fireEvent.click(addButton);
    expect(onAddToCart).not.toHaveBeenCalled();
  });
});
