// @vitest-environment jsdom
/**
 * Component tests for ConsoleHeader — the detail line above the terminal
 * showing connected/connecting/replaying/disconnected status. Verifies the
 * connected state resolves every label through i18n (namespace `detail`,
 * keys under `console.*`) in both `en` and `ja`, and that no raw hardcoded
 * English literal (e.g. "Connected", "uptime") leaks through once the
 * language is switched to `ja`.
 */
import '../../../../test/dom-setup';
import { afterEach, describe, it, expect } from 'vitest';
import { screen } from '@testing-library/react';
import { renderWithProviders } from '../../../../test/render-helpers';
import i18n from '../../../../i18n';
import type { ConsoleSessionSummary } from '../../../../lib/ssh-console-types';
import { ConsoleHeader } from './ConsoleHeader';

afterEach(async () => {
  await i18n.changeLanguage('en');
});

function makeSession(overrides: Partial<ConsoleSessionSummary> = {}): ConsoleSessionSummary {
  return {
    connection_id: 'conn-a',
    connection_label: 'Prod DB',
    started_at: new Date(Date.now() - 65_000).toISOString(),
    last_activity_at: new Date(Date.now() - 5_000).toISOString(),
    status: 'connected',
    can_write: true,
    can_close: true,
    agent_active: false,
    ...overrides,
  };
}

describe('ConsoleHeader', () => {
  it('renders the connected-state labels in ja and no raw English literal leaks through', async () => {
    await i18n.changeLanguage('ja');
    const t = i18n.getFixedT('ja', 'detail');
    const session = makeSession();
    renderWithProviders(
      <ConsoleHeader state={{ kind: 'connected', canWrite: true, cols: 80, rows: 24 }} session={session} />,
    );

    // Japanese "connected" label from console.connected must appear.
    expect(screen.getByText(new RegExp(t('console.connected')))).toBeInTheDocument();

    const container = document.body.textContent ?? '';
    expect(container).not.toMatch(/●\s*Connected\b/);
    expect(container.toLowerCase()).not.toContain('uptime');
    expect(container.toLowerCase()).not.toContain('idle');
  });

  it('shows the read-only badge via i18n when canWrite is false', async () => {
    await i18n.changeLanguage('ja');
    const t = i18n.getFixedT('ja', 'detail');
    renderWithProviders(
      <ConsoleHeader state={{ kind: 'connected', canWrite: false, cols: 80, rows: 24 }} session={makeSession()} />,
    );
    expect(screen.getByText(t('console.readOnly'))).toBeInTheDocument();
  });

  it('renders connecting/replaying/disconnected/no-session states via i18n in ja', async () => {
    await i18n.changeLanguage('ja');
    const t = i18n.getFixedT('ja', 'detail');

    const { unmount: unmount1 } = renderWithProviders(
      <ConsoleHeader state={{ kind: 'no_session' }} session={null} />,
    );
    expect(screen.getByText(t('console.noActiveConsole'))).toBeInTheDocument();
    unmount1();

    const { unmount: unmount2 } = renderWithProviders(
      <ConsoleHeader state={{ kind: 'connecting' }} session={null} />,
    );
    expect(screen.getByText(t('console.connecting'))).toBeInTheDocument();
    unmount2();

    const { unmount: unmount3 } = renderWithProviders(
      <ConsoleHeader state={{ kind: 'replaying' }} session={null} />,
    );
    expect(screen.getByText(t('console.restoringScrollback'))).toBeInTheDocument();
    unmount3();

    renderWithProviders(
      <ConsoleHeader state={{ kind: 'disconnected', reason: 'boom' }} session={null} />,
    );
    expect(screen.getByText(t('console.disconnected', { reason: 'boom' }))).toBeInTheDocument();
  });
});
