/* eslint-disable import-x/order */
import { render, screen, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';

// Mock next-intl with Vietnamese messages
const viMessages = await import('@/messages/vi.json');
vi.mock('next-intl', () => ({
  useTranslations: (namespace?: string) => {
    const messages = viMessages.default ?? viMessages;
    const ns = namespace
      ? (messages[namespace as keyof typeof messages] as Record<string, unknown> | undefined)
      : (messages as unknown as Record<string, unknown>);
    return (key: string, params?: Record<string, unknown>) => {
      if (!ns) return key;
      const parts = key.split('.');
      let val: unknown = ns;
      for (const p of parts) {
        val = (val as Record<string, unknown>)?.[p];
      }
      if (typeof val === 'string' && params) {
        return val.replace(/\{(\w+)\}/g, (_, k: string) => String(params[k] ?? `{${k}}`));
      }
      return typeof val === 'string' ? val : key;
    };
  },
  useLocale: () => 'vi',
  NextIntlClientProvider: ({ children }: { children: React.ReactNode }) => children,
}));

vi.mock('next/link', () => ({
  default: ({ children, href, ...props }: { children: React.ReactNode; href: string; [key: string]: unknown }) => (
    <a href={href} {...props}>{children}</a>
  ),
}));

vi.mock('next/image', () => ({
  default: (props: Record<string, unknown>) => <img {...props} />,
}));

vi.mock('@/i18n/navigation', () => ({
  Link: ({ children, href, ...props }: { children: React.ReactNode; href: string; [key: string]: unknown }) => (
    <a href={href} {...props}>{children}</a>
  ),
  useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
  usePathname: () => '/',
  redirect: vi.fn(),
}));

vi.mock('@/lib/listings-api', () => ({
  listingsApi: {
    search: vi.fn().mockResolvedValue({ data: [], total: 0 }),
  },
}));

vi.mock('@/components/search/property-card', () => ({
  PropertyCard: ({ listing }: { listing: { id: string } }) => <div data-testid={`listing-${listing.id}`}>Listing</div>,
}));

import LandingPage from '../page';

describe('LandingPage', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('renders hero section with search form', async () => {
    render(<LandingPage />);

    await waitFor(() => {
      expect(screen.getByRole('search')).toBeInTheDocument();
    });
  });

  it('renders property type badges', async () => {
    render(<LandingPage />);

    await waitFor(() => {
      // Property type badges from Vietnamese messages
      expect(screen.getAllByRole('link').length).toBeGreaterThan(0);
    });
  });

  it('renders districts section', async () => {
    render(<LandingPage />);

    await waitFor(() => {
      expect(screen.getByText('Quận 1')).toBeInTheDocument();
      expect(screen.getByText('Quận 7')).toBeInTheDocument();
    });
  });

  it('renders stats section', async () => {
    render(<LandingPage />);

    await waitFor(() => {
      expect(screen.getByText('10,000+')).toBeInTheDocument();
      expect(screen.getByText('50,000+')).toBeInTheDocument();
    });
  });
});
