/**
 * SEO-007: Auth pages noindex meta tag tests.
 *
 * These tests use a richer Head mock that renders children so we can assert
 * on the noindex and description meta tags added to auth pages.
 */
import { render } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';

import { useForm } from '@inertiajs/react';

// ─── Shared mocks ────────────────────────────────────────────────────────────

const mockPost = vi.fn();
const mockSetData = vi.fn();
const mockReset = vi.fn();

vi.mock('@inertiajs/react', async () => {
  const actual = await vi.importActual('@inertiajs/react');
  return {
    ...actual,
    // Rich Head mock: renders children so meta tags appear in the DOM
    Head: ({ title, children }: { title?: string; children?: React.ReactNode }) => (
      <head data-testid="inertia-head">
        {title && <title>{title}</title>}
        {children}
      </head>
    ),
    Link: ({ children, href }: { children: React.ReactNode; href: string }) => (
      <a href={href}>{children}</a>
    ),
    useForm: vi.fn(() => ({
      data: { email: '', password: '', remember: false },
      setData: mockSetData,
      post: mockPost,
      processing: false,
      errors: {},
      reset: mockReset,
    })),
    usePage: vi.fn(() => ({
      props: { auth: { user: null }, errors: {}, flash: {} },
    })),
  };
});

vi.mock('@/Layouts/AuthLayout', () => ({
  default: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="auth-layout">{children}</div>
  ),
}));

vi.mock('@/Components/legal/LegalContentModal', () => ({
  LegalContentModal: () => null,
}));

vi.mock('@/Components/auth/SocialAuthButtons', () => ({
  SocialAuthButtons: () => null,
}));

vi.mock('@/Components/auth/PasswordStrengthIndicator', () => ({
  PasswordStrengthIndicator: () => null,
}));

// ─── Login ────────────────────────────────────────────────────────────────────

describe('Login page meta tags (SEO-007)', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(useForm).mockReturnValue({
      data: { email: '', password: '', remember: false },
      setData: mockSetData,
      post: mockPost,
      processing: false,
      errors: {},
      reset: mockReset,
    } as unknown as ReturnType<typeof useForm>);
  });

  it('has noindex meta tag', async () => {
    const { default: Login } = await import('./Login');
    const { container } = render(<Login canResetPassword={true} />);

    const robots = container.querySelector('meta[name="robots"]');
    expect(robots).not.toBeNull();
    expect(robots?.getAttribute('content')).toContain('noindex');
  });

  it('has nofollow meta tag', async () => {
    const { default: Login } = await import('./Login');
    const { container } = render(<Login canResetPassword={true} />);

    const robots = container.querySelector('meta[name="robots"]');
    expect(robots?.getAttribute('content')).toContain('nofollow');
  });

  it('has a description meta tag', async () => {
    const { default: Login } = await import('./Login');
    const { container } = render(<Login canResetPassword={true} />);

    const desc = container.querySelector('meta[name="description"]');
    expect(desc).not.toBeNull();
    expect(desc?.getAttribute('content')?.length).toBeGreaterThan(10);
  });
});

// ─── Register ─────────────────────────────────────────────────────────────────

describe('Register page meta tags (SEO-007)', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(useForm).mockReturnValue({
      data: { name: '', email: '', password: '', password_confirmation: '', remember: false },
      setData: mockSetData,
      post: mockPost,
      processing: false,
      errors: {},
      reset: mockReset,
    } as unknown as ReturnType<typeof useForm>);
  });

  it('has noindex meta tag', async () => {
    const { default: Register } = await import('./Register');
    const { container } = render(<Register />);

    const robots = container.querySelector('meta[name="robots"]');
    expect(robots).not.toBeNull();
    expect(robots?.getAttribute('content')).toContain('noindex');
  });

  it('has nofollow meta tag', async () => {
    const { default: Register } = await import('./Register');
    const { container } = render(<Register />);

    const robots = container.querySelector('meta[name="robots"]');
    expect(robots?.getAttribute('content')).toContain('nofollow');
  });

  it('has a description meta tag', async () => {
    const { default: Register } = await import('./Register');
    const { container } = render(<Register />);

    const desc = container.querySelector('meta[name="description"]');
    expect(desc).not.toBeNull();
    expect(desc?.getAttribute('content')?.length).toBeGreaterThan(10);
  });
});

// ─── ForgotPassword ───────────────────────────────────────────────────────────

describe('ForgotPassword page meta tags (SEO-007)', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(useForm).mockReturnValue({
      data: { email: '' },
      setData: mockSetData,
      post: mockPost,
      processing: false,
      errors: {},
    } as unknown as ReturnType<typeof useForm>);
  });

  it('has noindex meta tag', async () => {
    const { default: ForgotPassword } = await import('./ForgotPassword');
    const { container } = render(<ForgotPassword />);

    const robots = container.querySelector('meta[name="robots"]');
    expect(robots).not.toBeNull();
    expect(robots?.getAttribute('content')).toContain('noindex');
  });

  it('has nofollow meta tag', async () => {
    const { default: ForgotPassword } = await import('./ForgotPassword');
    const { container } = render(<ForgotPassword />);

    const robots = container.querySelector('meta[name="robots"]');
    expect(robots?.getAttribute('content')).toContain('nofollow');
  });

  it('has a description meta tag', async () => {
    const { default: ForgotPassword } = await import('./ForgotPassword');
    const { container } = render(<ForgotPassword />);

    const desc = container.querySelector('meta[name="description"]');
    expect(desc).not.toBeNull();
    expect(desc?.getAttribute('content')?.length).toBeGreaterThan(10);
  });

  it('page title contains Forgot password', async () => {
    const { default: ForgotPassword } = await import('./ForgotPassword');
    const { container } = render(<ForgotPassword />);

    const title = container.querySelector('title');
    expect(title?.textContent?.toLowerCase()).toContain('forgot');
  });
});
