import React, { useState } from 'react';
import App from './App.tsx';
import AdminApp from './AdminApp.tsx';
import { ErrorBoundary } from './components/ErrorBoundary.tsx';

export default function Root() {
  const [simulatedDomain] = useState<'student' | 'admin'>(() => {
    // 1. Check hostname
    if (window.location.hostname.includes('admin-estudo') || window.location.hostname.startsWith('admin')) {
      return 'admin';
    }
    // 2. Check path
    if (window.location.pathname.startsWith('/admin')) {
      return 'admin';
    }
    
    // Always default to student if not explicitly admin
    return 'student';
  });

  const isAdmin = simulatedDomain === 'admin';

  React.useEffect(() => {
    // Dynamically update document title and favicon
    document.title = isAdmin ? 'MicrotecFlix - Admin Portal' : 'MicrotecFlix - Plataforma de Estudos';
    
    let link = document.querySelector("link[rel~='icon']") as HTMLLinkElement;
    if (!link) {
      link = document.createElement('link');
      link.rel = 'icon';
      document.head.appendChild(link);
    }
    link.href = isAdmin ? '/favicon-admin.svg' : '/favicon-student.svg';
  }, [isAdmin]);

  return (
    <ErrorBoundary>
      <div className="relative min-h-screen">
        {isAdmin ? <AdminApp /> : <App />}
      </div>
    </ErrorBoundary>
  );
}
