import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import AdminDashboard from './page';

jest.mock('@/lib/auth', () => ({
  auth: jest.fn().mockResolvedValue({
    user: { name: '测试用户' },
  }),
}));

jest.mock('@/db', () => ({
  db: {
    select: jest.fn().mockReturnValue({
      from: jest.fn().mockReturnValue({
        where: jest.fn().mockReturnValue({
          orderBy: jest.fn().mockReturnValue({
            limit: jest.fn().mockResolvedValue([]),
          }),
        }),
        orderBy: jest.fn().mockReturnValue({
          limit: jest.fn().mockResolvedValue([]),
        }),
      }),
    }),
  },
}));

jest.mock('next/link', () => {
  return ({ children, href }: { children: React.ReactNode; href: string }) => {
    return <a href={href}>{children}</a>;
  };
});

describe('AdminDashboard', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('Rendering', () => {
    it('should render dashboard', async () => {
      const dashboard = await AdminDashboard();
      render(dashboard);
      
      const heading = screen.getByRole('heading', { level: 1 });
      expect(heading).toBeInTheDocument();
      expect(heading).toHaveTextContent('仪表盘');
    });

    it('should render welcome message', async () => {
      const dashboard = await AdminDashboard();
      render(dashboard);
      
      const welcome = screen.getByText(/欢迎回来/i);
      expect(welcome).toBeInTheDocument();
    });

    it('should render stat cards', async () => {
      const dashboard = await AdminDashboard();
      render(dashboard);
      
      const totalContent = screen.getByText('总内容数');
      const published = screen.getByText('已发布');
      const draft = screen.getByText('草稿');
      const users = screen.getByText('用户数');
      
      expect(totalContent).toBeInTheDocument();
      expect(published).toBeInTheDocument();
      expect(draft).toBeInTheDocument();
      expect(users).toBeInTheDocument();
    });

    it('should render recent content section', async () => {
      const dashboard = await AdminDashboard();
      render(dashboard);
      
      const recentContent = screen.getByText('最近内容');
      expect(recentContent).toBeInTheDocument();
    });

    it('should render quick actions section', async () => {
      const dashboard = await AdminDashboard();
      render(dashboard);
      
      const quickActions = screen.getByText('快捷操作');
      expect(quickActions).toBeInTheDocument();
    });
  });

  describe('Navigation', () => {
    it('should have content management link', async () => {
      const dashboard = await AdminDashboard();
      render(dashboard);
      
      const contentLink = screen.getByRole('link', { name: /总内容数/i });
      expect(contentLink).toBeInTheDocument();
      expect(contentLink).toHaveAttribute('href', '/admin/content');
    });

    it('should have users link', async () => {
      const dashboard = await AdminDashboard();
      render(dashboard);
      
      const usersLink = screen.getByRole('link', { name: /用户数/i });
      expect(usersLink).toBeInTheDocument();
      expect(usersLink).toHaveAttribute('href', '/admin/users');
    });
  });
});
