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

vi.mock('next/dynamic', () => ({
  default: () => {
    const Mock = () => <div data-testid="chart-placeholder">Chart</div>;
    Mock.displayName = 'MockChart';
    return Mock;
  },
}));

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

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

const mockMarketReport = {
  districts: [
    { district: 'Quan 1', totalListings: 100, avgPriceM2: 120000000, medianPrice: '15000000000', daysOnMarket: 45, yoyChange: 5.2, inventoryLevel: 50 },
    { district: 'Quan 7', totalListings: 80, avgPriceM2: 60000000, medianPrice: '8000000000', daysOnMarket: 30, yoyChange: -2.1, inventoryLevel: 30 },
  ],
};

const mockHeatmap = {
  dataPoints: [
    { district: 'Quan 1', avgPriceM2: 120000000, totalListings: 100, lat: 10.77, lng: 106.7 },
    { district: 'Quan 7', avgPriceM2: 60000000, totalListings: 80, lat: 10.73, lng: 106.72 },
  ],
};

const mockListings = {
  data: [
    {
      id: '1',
      status: 'ACTIVE',
      transactionType: 'SALE',
      priceVND: '5000000000',
      viewCount: 10,
      saveCount: 2,
      inquiryCount: 3,
      publishedAt: '2024-01-01',
      createdAt: '2024-01-01',
      pricePerM2: null,
      rentPriceMonthly: null,
      commissionPct: null,
      property: {
        id: 'p1',
        propertyType: 'APARTMENT',
        title: 'Căn hộ Quận 7',
        description: 'Test',
        address: '123 Nguyễn Hữu Thọ',
        ward: 'Tân Hưng',
        district: 'Quận 7',
        city: 'Hồ Chí Minh',
        areaM2: 75,
        bedrooms: 2,
        bathrooms: 2,
        floors: null,
        direction: null,
        yearBuilt: null,
        legalStatus: null,
        amenities: null,
        projectName: null,
        media: [],
      },
      seller: { id: 's1', fullName: 'Nguyen Van A', phone: '0912345678' },
      agent: null,
    },
  ],
  total: 1,
  page: 1,
  limit: 6,
  totalPages: 1,
};

vi.mock('@/lib/hooks/use-analytics', () => ({
  useMarketReport: vi.fn(() => ({ data: mockMarketReport, isLoading: false })),
  useHeatmap: vi.fn(() => ({ data: mockHeatmap, isLoading: false })),
}));

vi.mock('@/lib/hooks/use-listings', () => ({
  useListingsSearch: vi.fn(() => ({ data: mockListings, isLoading: false })),
}));

vi.mock('@/components/listings/listing-status-badge', () => ({
  ListingStatusBadge: ({ status }: { status: string }) => <span data-testid="status-badge">{status}</span>,
}));

import DashboardPage from '../page';

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

  it('renders the dashboard heading', () => {
    render(<DashboardPage />);
    expect(screen.getByText('Bảng điều khiển')).toBeInTheDocument();
  });

  it('renders stat cards with data', () => {
    render(<DashboardPage />);
    expect(screen.getByText('Tin đăng của tôi')).toBeInTheDocument();
    expect(screen.getByText('Lượt xem')).toBeInTheDocument();
    expect(screen.getByText('Liên hệ')).toBeInTheDocument();
    expect(screen.getByText('Giá TB thị trường')).toBeInTheDocument();
  });

  it('renders recent listings section', () => {
    render(<DashboardPage />);
    expect(screen.getByText('Tin đăng gần đây')).toBeInTheDocument();
    expect(screen.getByText(/căn hộ quận 7/i)).toBeInTheDocument();
  });

  it('renders "Đăng tin mới" button', () => {
    render(<DashboardPage />);
    expect(screen.getByRole('link', { name: /đăng tin mới/i })).toBeInTheDocument();
  });

  it('renders market summary card', () => {
    render(<DashboardPage />);
    expect(screen.getByText(/thị trường ho chi minh/i)).toBeInTheDocument();
  });
});
