/**
 * main.tsx — Harness entry point for the headless workspace-app test runner (Task 5).
 *
 * Reads space/app/entry/token from the URL search params, builds a
 * token-authenticated HarnessGateway, and mounts the real AppRunner
 * component with autoApproveWrites so the postMessage bridge responds
 * to file reads and writes without confirmation dialogs.
 *
 * URL shape:  /app-harness?space=<spaceId>&app=<appName>&entry=<entryPath>&token=<jwt>
 */
import React from 'react';
import { createRoot } from 'react-dom/client';
import { AppRunner } from '../components/spaces/AppRunner';
import { createHarnessGateway } from './harness-gateway';

function HarnessApp() {
  const params = new URLSearchParams(window.location.search);
  const spaceId = params.get('space') ?? '';
  const appName = params.get('app') ?? '';
  const entryPath = params.get('entry') ?? '';
  const token = params.get('token') ?? '';

  if (!spaceId || !appName || !entryPath || !token) {
    return (
      <div style={{ fontFamily: 'monospace', padding: 24, color: '#c00' }}>
        <b>app-harness: missing required params</b>
        <pre>{JSON.stringify({ spaceId, appName, entryPath, token: token ? '[set]' : '[missing]' }, null, 2)}</pre>
      </div>
    );
  }

  const gateway = createHarnessGateway(spaceId, token);

  return (
    <AppRunner
      gateway={gateway}
      appName={appName}
      entryPath={entryPath}
      onClose={() => { /* headless — no-op */ }}
      autoApproveWrites
    />
  );
}

const container = document.getElementById('root');
if (!container) throw new Error('app-harness: #root not found');
createRoot(container).render(<HarnessApp />);
