import {describe, expect, it, vi} from 'vitest'

import {render, screen, mockMinisSDK, resetAllMocks} from '../../test-utils'

import {AddToCartButton} from './add-to-cart'

// Mock hooks
vi.mock('../../hooks/shop/useShopCartActions', () => ({
  useShopCartActions: () => ({
    addToCart: mockMinisSDK.addToCart,
    buyProduct: mockMinisSDK.buyProduct,
  }),
}))

describe('AddToCartButton', () => {
  const defaultProps = {
    productId: 'gid://shopify/Product/123',
    productVariantId: 'gid://shopify/ProductVariant/456',
  }

  // eslint-disable-next-line jest/require-top-level-describe
  beforeEach(() => {
    resetAllMocks()
  })

  it('renders with default text', () => {
    render(<AddToCartButton {...defaultProps} />)

    expect(screen.getByRole('button')).toBeInTheDocument()
    expect(screen.getByText('Add to cart')).toBeInTheDocument()
  })

  it('renders with required props', () => {
    render(<AddToCartButton {...defaultProps} />)

    const button = screen.getByRole('button')
    expect(button).toBeInTheDocument()
  })

  it('respects disabled prop', () => {
    render(<AddToCartButton {...defaultProps} disabled />)

    const button = screen.getByRole('button')
    expect(button).toBeDisabled()
  })

  it('applies custom className', () => {
    render(<AddToCartButton {...defaultProps} className="custom-class" />)

    const button = screen.getByRole('button')
    expect(button).toHaveClass('custom-class')
  })

  it('renders with different sizes', () => {
    const {rerender} = render(<AddToCartButton {...defaultProps} size="sm" />)

    expect(screen.getByRole('button')).toBeInTheDocument()

    rerender(<AddToCartButton {...defaultProps} size="default" />)
    expect(screen.getByRole('button')).toBeInTheDocument()

    rerender(<AddToCartButton {...defaultProps} size="lg" />)
    expect(screen.getByRole('button')).toBeInTheDocument()
  })

  it('renders with discount codes prop', () => {
    const discountCodes = ['SUMMER20', 'FREESHIP']

    render(<AddToCartButton {...defaultProps} discountCodes={discountCodes} />)

    expect(screen.getByRole('button')).toBeInTheDocument()
  })
})
