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

class IntersectionObserverMock {
  observe() {}
  unobserve() {}
  disconnect() {}
}

Object.defineProperty(globalThis, 'IntersectionObserver', {
  writable: true,
  value: IntersectionObserverMock,
});

vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    t: (key: string, fallback?: string | { defaultValue?: unknown }) => {
      if (typeof fallback === 'string') {
        return fallback;
      }
      if (fallback && typeof fallback === 'object' && 'defaultValue' in fallback) {
        return fallback.defaultValue;
      }
      return key;
    },
  }),
}));

vi.mock('@inertiajs/react', () => ({
  Head: () => null,
  Link: ({ children, href }: { children: React.ReactNode; href: string }) => <a href={href}>{children}</a>,
  useForm: () => ({
    data: { name: '', email: '', message: '', nickname: '' },
    setData: vi.fn(),
    post: vi.fn(),
    processing: false,
    errors: {},
    reset: vi.fn(),
  }),
  usePage: () => ({ props: { flash: {} } }),
}));

vi.mock('@/layouts/mainWebsite', () => ({
  default: ({ children, title }: { children: React.ReactNode; title: string }) => (
    <div>
      <h1>{title}</h1>
      {children}
    </div>
  ),
}));

vi.mock('@/hooks/useLocalizedRoutes', () => ({
  useLocalizedRoutes: () => ({
    localizedPath: (path: string) => path,
  }),
}));

vi.mock('@/hooks/useAnalytics', () => ({
  useAnalytics: () => ({ trackEvent: vi.fn() }),
}));

vi.mock('@/hooks/useCtaExperiment', () => ({
  useCtaExperiment: () => ({ variant: 'default', trackClick: vi.fn() }),
}));

import Home from '../Home';

describe('Home', () => {
  it('renders the hero headline', () => {
    render(
      <Home
        packages={[
          { id: 1, name: 'Starter', description: 'Desc', price: 29 },
          { id: 2, name: 'Standard', description: 'Desc', price: 59 },
        ]}
      />
    );

    expect(screen.getByText('home.hero_title')).toBeInTheDocument();
  });
});
