import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react';
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
import TermsRoute from './page';

const mockUseMarriageProfileQuery = vi.fn();
const mockReplace = vi.fn();

vi.mock('@/hooks/marriage/use-profile-main', () => ({
  useMarriageProfileQuery: () => mockUseMarriageProfileQuery(),
}));

vi.mock('next/navigation', () => ({
  useRouter: () => ({
    replace: mockReplace,
  }),
}));

vi.mock('@/translations/provider', () => ({
  useI18n: () => ({
    locale: 'en',
    dictionary: {
      "Failed to load profile. Please try again.": "Failed to load profile. Please try again.",
      "Retry": "Retry",
    },
  }),
}));

vi.mock('@/translations/config', () => ({
  localizePath: (path: string) => path,
}));

vi.mock('@/components/Componentes/slider-page', () => ({
  default: () => <div data-testid="slider-page">SliderPage</div>,
}));

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

  afterEach(() => {
    cleanup();
  });

  it('renders loading state when isLoading is true', () => {
    mockUseMarriageProfileQuery.mockReturnValue({ isLoading: true });
    render(<TermsRoute />);
    expect(screen.queryByTestId('slider-page')).not.toBeInTheDocument();
    // The spinner should be visible, maybe just check for a div. We can check slider is absent.
  });

  it('renders Error/Retry when isError is true', () => {
    const mockRefetch = vi.fn();
    mockUseMarriageProfileQuery.mockReturnValue({ isError: true, refetch: mockRefetch });
    render(<TermsRoute />);
    
    expect(screen.getByText('Failed to load profile. Please try again.')).toBeInTheDocument();
    const retryBtn = screen.getByText('Retry');
    fireEvent.click(retryBtn);
    expect(mockRefetch).toHaveBeenCalled();
    expect(screen.queryByTestId('slider-page')).not.toBeInTheDocument();
  });

  it('pending_onboarding => SliderPage renders', () => {
    mockUseMarriageProfileQuery.mockReturnValue({
      isLoading: false,
      data: { status: 'pending_onboarding' }
    });
    render(<TermsRoute />);
    
    expect(screen.getByTestId('slider-page')).toBeInTheDocument();
    expect(mockReplace).not.toHaveBeenCalled();
  });

  it('pending_info => redirects to Questions and SliderPage is NOT rendered', async () => {
    mockUseMarriageProfileQuery.mockReturnValue({
      isLoading: false,
      data: { status: 'pending_info' }
    });
    render(<TermsRoute />);
    
    expect(screen.queryByTestId('slider-page')).not.toBeInTheDocument();
    await waitFor(() => {
      expect(mockReplace).toHaveBeenCalled(); // Should redirect to /questions-list or /questions-list/personal_info
    });
  });

  it('waiting => redirects to Finding/Waiting Page and SliderPage is NOT rendered', async () => {
    mockUseMarriageProfileQuery.mockReturnValue({
      isLoading: false,
      data: { status: 'waiting' }
    });
    render(<TermsRoute />);
    
    expect(screen.queryByTestId('slider-page')).not.toBeInTheDocument();
    await waitFor(() => {
      expect(mockReplace).toHaveBeenCalledWith('/finding-match');
    });
  });
});
