/**
 * Schema AI - Main React Entry Point.
 *
 * Detects which admin page we're on and mounts the appropriate component.
 *
 * @package Schema_AI
 */

import { createRoot } from '@wordpress/element';
import { SchemaProvider } from './context/SchemaContext';
import { UserProvider } from './context/UserContext';
import Dashboard from './components/Dashboard';
import SchemaTester from './components/SchemaTester';
import UpgradePage from './components/UpgradePage';
import MetaBox from './components/MetaBox';

const App = ( { children } ) => (
	<UserProvider>
		<SchemaProvider>
			{ children }
		</SchemaProvider>
	</UserProvider>
);

// Dashboard page.
const dashboardEl = document.getElementById( 'schema-ai-dashboard' );
if ( dashboardEl ) {
	createRoot( dashboardEl ).render(
		<App><Dashboard /></App>
	);
}

// Schema Tester page.
const testerEl = document.getElementById( 'schema-ai-tester' );
if ( testerEl ) {
	createRoot( testerEl ).render(
		<App><SchemaTester /></App>
	);
}

// Upgrade page.
const upgradeEl = document.getElementById( 'schema-ai-upgrade' );
if ( upgradeEl ) {
	createRoot( upgradeEl ).render(
		<App><UpgradePage /></App>
	);
}

// Meta box in post editor.
const metaBoxEl = document.getElementById( 'schema-ai-meta-box-app' );
if ( metaBoxEl ) {
	const postId = window.schemaAiData?.postId || metaBoxEl.dataset.postId;
	createRoot( metaBoxEl ).render(
		<App><MetaBox postId={ parseInt( postId, 10 ) } /></App>
	);
}
