/* @vitest-environment jsdom */

import { fireEvent, render, screen } from '@testing-library/react';
import { expect, test, vi } from 'vitest';

import { PlatformPillSwitch } from './PlatformPillSwitch';

test('renders checked pill switch with platform chrome', () => {
  render(
    <PlatformPillSwitch
      label="AI重绘"
      aria-label="AI重绘"
      checked
      onChange={() => {}}
    />,
  );

  const switchInput = screen.getByRole('switch', { name: 'AI重绘' });

  expect(switchInput).toHaveProperty('checked', true);
  expect(screen.getByText('AI重绘').closest('label')?.className).toContain(
    'bg-white/94',
  );
  expect(screen.getByText('AI重绘').closest('label')?.className).toContain(
    'backdrop-blur',
  );
});

test('calls onChange when toggled', () => {
  const onChange = vi.fn();

  render(
    <PlatformPillSwitch
      label="AI重绘"
      aria-label="AI重绘"
      checked={false}
      onChange={onChange}
    />,
  );

  fireEvent.click(screen.getByRole('switch', { name: 'AI重绘' }));

  expect(onChange).toHaveBeenCalledTimes(1);
});

test('keeps disabled switch inert and styled as disabled', () => {
  const onChange = vi.fn();

  render(
    <PlatformPillSwitch
      label="AI重绘"
      aria-label="AI重绘"
      checked={false}
      disabled
      onChange={onChange}
    />,
  );

  const switchInput = screen.getByRole('switch', { name: 'AI重绘' });

  expect(switchInput).toHaveProperty('disabled', true);
  expect(screen.getByText('AI重绘').closest('label')?.className).toContain(
    'cursor-not-allowed',
  );
});

test('keeps local placement classes', () => {
  render(
    <PlatformPillSwitch
      label="AI重绘"
      aria-label="AI重绘"
      checked={false}
      className="absolute bottom-3 left-3"
      onChange={() => {}}
    />,
  );

  expect(screen.getByText('AI重绘').closest('label')?.className).toContain(
    'bottom-3',
  );
});
