import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';

let locationSearch = '?return_to=encoded-value';

const navigateMock = vi.fn();

vi.mock('react-router-dom', () => ({
  useNavigate: () => navigateMock,
  useLocation: () => ({ search: locationSearch }),
}));

vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    t: (key: string, fallback?: string | Record<string, unknown>) => {
      if (typeof fallback === 'string') {
        return fallback;
      }
      if (fallback && typeof fallback === 'object' && typeof fallback.defaultValue === 'string') {
        return fallback.defaultValue;
      }
      return key;
    },
  }),
}));

vi.mock('@tanstack/react-query', () => ({
  useMutation: () => ({ mutate: vi.fn(), isPending: false }),
}));

vi.mock('../../auth/context', () => ({
  useAuth: () => ({ status: 'unauthenticated', applyToken: vi.fn(), abilities: [] }),
}));

vi.mock('../../lib/returnTo', () => ({
  resolveReturnTarget: () => ({ finalTarget: '/event-admin/mobile/dashboard', encodedFinal: 'encoded-value' }),
}));

vi.mock('../hooks/useInstallPrompt', () => ({
  useInstallPrompt: () => ({
    isInstalled: false,
    isStandalone: false,
    canInstall: false,
    isIos: false,
    promptInstall: vi.fn(),
  }),
}));

vi.mock('../lib/installBanner', () => ({
  getInstallBannerDismissed: () => false,
  setInstallBannerDismissed: vi.fn(),
  shouldShowInstallBanner: () => ({ shouldShow: false }),
}));

vi.mock('../components/MobileInstallBanner', () => ({
  MobileInstallBanner: () => null,
}));

vi.mock('@tamagui/stacks', () => ({
  YStack: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  XStack: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));

vi.mock('@tamagui/text', () => ({
  SizableText: ({ children }: { children: React.ReactNode }) => <span>{children}</span>,
}));

vi.mock('@tamagui/button', () => ({
  Button: ({ children, onPress, onClick, ...rest }: { children: React.ReactNode; onPress?: () => void; onClick?: () => void }) => (
    <button type="button" onClick={onPress ?? onClick} {...rest}>
      {children}
    </button>
  ),
}));

vi.mock('../components/Primitives', () => ({
  MobileCard: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));

vi.mock('../components/FormControls', () => ({
  MobileField: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  MobileInput: (props: React.InputHTMLAttributes<HTMLInputElement>) => <input {...props} />,
}));

vi.mock('../theme', () => ({
  ADMIN_GRADIENTS: { loginBackground: 'linear-gradient(0deg, #000, #111)' },
  useAdminTheme: () => ({
    text: '#111827',
    muted: '#6b7280',
    primary: '#ff5a5f',
    dangerBg: '#fee2e2',
    dangerText: '#991b1b',
    border: '#e5e7eb',
    surface: '#ffffff',
  }),
}));

vi.mock('lucide-react', () => ({
  Loader2: () => <span>loader</span>,
}));

import MobileLoginPage from '../LoginPage';

describe('MobileLoginPage', () => {
  it('renders Google login button', () => {
    locationSearch = '?return_to=encoded-value';

    render(<MobileLoginPage />);

    const googleButton = screen.getByText('Mit Google anmelden');
    expect(googleButton).toBeInTheDocument();
  });

  it('shows oauth error details when present', () => {
    locationSearch = '?error=google_failed&error_description=Login%20failed';

    render(<MobileLoginPage />);

    expect(screen.getByText('Login aktuell nicht moeglich')).toBeInTheDocument();
    expect(screen.getByText('Login failed')).toBeInTheDocument();
  });
});
