import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { PasswordResetRequestPage } from '@/pages/PasswordResetRequest';
import { PasswordResetConfirmPage } from '@/pages/PasswordResetConfirm';

vi.mock('@/api/hooks', () => ({
  usePasswordResetRequest: () => ({
    mutateAsync: vi.fn().mockResolvedValue({}),
    isPending: false,
  }),
  usePasswordResetConfirm: () => ({
    mutateAsync: vi.fn().mockResolvedValue({}),
    isPending: false,
  }),
}));

describe('PasswordResetRequestPage', () => {
  it('renders form with email input and submit button', () => {
    render(
      <MemoryRouter initialEntries={['/password-reset']}>
        <PasswordResetRequestPage />
      </MemoryRouter>
    );
    expect(screen.getByLabelText(/E-Mail-Adresse/)).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /Link senden/ })).toBeInTheDocument();
  });

  it('renders description text', () => {
    render(
      <MemoryRouter initialEntries={['/password-reset']}>
        <PasswordResetRequestPage />
      </MemoryRouter>
    );
    expect(screen.getByText(/Geben Sie Ihre E-Mail-Adresse ein/)).toBeInTheDocument();
  });

  it('has back to login link', () => {
    render(
      <MemoryRouter initialEntries={['/password-reset']}>
        <PasswordResetRequestPage />
      </MemoryRouter>
    );
    expect(screen.getByText(/Zurück zur Anmeldung/)).toBeInTheDocument();
  });
});

describe('PasswordResetConfirmPage', () => {
  it('renders new password and confirm password inputs', () => {
    render(
      <MemoryRouter initialEntries={['/password-reset/confirm?token=abc123']}>
        <PasswordResetConfirmPage />
      </MemoryRouter>
    );
    expect(screen.getByLabelText(/Neues Passwort/)).toBeInTheDocument();
    expect(screen.getByLabelText(/Passwort bestätigen/)).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /Passwort ändern/ })).toBeInTheDocument();
  });

  it('renders description text', () => {
    render(
      <MemoryRouter initialEntries={['/password-reset/confirm?token=abc123']}>
        <PasswordResetConfirmPage />
      </MemoryRouter>
    );
    expect(screen.getByText(/Geben Sie Ihr neues Passwort ein/)).toBeInTheDocument();
  });

  it('reads token from URL search params', () => {
    render(
      <MemoryRouter initialEntries={['/password-reset/confirm?token=test-token-123']}>
        <PasswordResetConfirmPage />
      </MemoryRouter>
    );
    // Token is used internally for the API call
    expect(screen.getByTestId('reset-confirm-page')).toBeInTheDocument();
  });
});
