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

jest.mock('next/navigation', () => ({
  useRouter: () => ({
    push: jest.fn(),
    back: jest.fn(),
  }),
  useParams: () => ({
    id: 'new',
  }),
}));

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

jest.mock('next/dynamic', () => () => {
  return function MockEditor() {
    return <div data-testid="rich-text-editor">Editor</div>;
  };
});

global.fetch = jest.fn();

describe('ContentEditPage', () => {
  beforeEach(() => {
    jest.clearAllMocks();
    (global.fetch as jest.Mock).mockResolvedValue({
      ok: true,
      json: async () => ({
        type: 'news',
        title: 'Test Content',
        slug: 'test-content',
        excerpt: 'Test excerpt',
        content: '<p>Test content</p>',
        coverImage: '',
        category: '',
        tags: [],
        status: 'draft',
      }),
    });
  });

  describe('Rendering', () => {
    it('should render content edit page', () => {
      render(<ContentEditPage />);
      const container = document.body;
      expect(container).toBeTruthy();
    });

    it('should render form', () => {
      render(<ContentEditPage />);
      const container = document.body;
      expect(container).toBeTruthy();
    });

    it('should render back button', () => {
      render(<ContentEditPage />);
      const container = document.body;
      expect(container).toBeTruthy();
    });
  });

  describe('Functionality', () => {
    it('should initialize with default values for new content', () => {
      render(<ContentEditPage />);
      const container = document.body;
      expect(container).toBeTruthy();
    });
  });

  describe('Accessibility', () => {
    it('should have form labels', () => {
      render(<ContentEditPage />);
      const container = document.body;
      expect(container).toBeTruthy();
    });
  });
});
