/* @vitest-environment jsdom */

import { Settings } from 'lucide-react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, test, vi } from 'vitest';

import { ICP_RECORD_NUMBER, LEGAL_DOCUMENTS } from '../common/legalDocuments';
import {
  ProfileLegalSection,
  ProfileSettingsRow,
  ProfileShortcutButton,
  ProfileStatCard,
} from './PlatformProfilePrimitives';

function TestIcon({ className }: { className?: string }) {
  return <span className={className}>I</span>;
}

describe('PlatformProfilePrimitives', () => {
  test('ProfileStatCard reports its dashboard card key on click', async () => {
    const user = userEvent.setup();
    const onClick = vi.fn();

    render(
      <ProfileStatCard
        cardKey="wallet"
        label="泥点余额"
        value="88"
        icon={TestIcon}
        onClick={onClick}
      />,
    );

    await user.click(screen.getByRole('button', { name: /泥点余额\s*88/u }));
    expect(onClick).toHaveBeenCalledWith('wallet');
  });

  test('ProfileShortcutButton keeps shortcut label and sub label visible', () => {
    render(
      <ProfileShortcutButton
        label="玩家社区"
        subLabel="交流心得"
        icon={TestIcon}
        onClick={vi.fn()}
      />,
    );

    const button = screen.getByRole('button', { name: /玩家社区/u });
    expect(button.className).toContain('platform-profile-shortcut-button');
    expect(screen.getByText('交流心得')).toBeTruthy();
  });

  test('ProfileSettingsRow and ProfileLegalSection keep their click affordances', async () => {
    const user = userEvent.setup();
    const onSettingsClick = vi.fn();
    const onOpenDocument = vi.fn();
    const firstLegalDocument = LEGAL_DOCUMENTS[0];

    if (!firstLegalDocument) {
      throw new Error('expected legal documents fixtures');
    }

    render(
      <>
        <ProfileSettingsRow
          label="通用设置"
          icon={Settings}
          onClick={onSettingsClick}
        />
        <ProfileLegalSection onOpenDocument={onOpenDocument} />
      </>,
    );

    const settingsButton = screen.getByRole('button', { name: /通用设置/u });
    expect(settingsButton.className).toContain('platform-navigable-list-item');

    await user.click(settingsButton);
    expect(onSettingsClick).toHaveBeenCalledTimes(1);

    await user.click(
      screen.getByRole('button', { name: firstLegalDocument.title }),
    );
    expect(onOpenDocument).toHaveBeenCalledWith(firstLegalDocument.id);
    expect(screen.getByText(ICP_RECORD_NUMBER)).toBeTruthy();
  });
});
