import { auth } from '../../auth';
import Navigation from '../Navigation';
import ProfileTabs from './ProfileTabs';
import MinProfilTab from './tabs/MinProfilTab';

export const metadata = { title: 'Min profil' };

type Props = {
  searchParams: Promise<{ tab?: string }>;
};

export default async function ProfilPage({ searchParams }: Props) {
  const { tab = 'profil' } = await searchParams;
  const session = await auth();
  const isAdmin = (session?.user as any)?.role === 'admin';

  // DatabsTab och AnvandareTab laddas dynamiskt för att hålla page.tsx tunn
  let TabContent: React.ComponentType;
  if (tab === 'databas' && isAdmin) {
    const { default: DatabsTab } = await import('./tabs/DatabsTab');
    TabContent = DatabsTab;
  } else if (tab === 'anvandare' && isAdmin) {
    const { default: AnvandareTab } = await import('./tabs/AnvandareTab');
    TabContent = AnvandareTab;
  } else {
    TabContent = MinProfilTab;
  }

  return (
    <>
      <Navigation />
      <main style={{ padding: '1rem', maxWidth: '1200px', margin: '0 auto' }}>
        <h1 style={{ marginBottom: '1.5rem' }}>Min profil</h1>
        <ProfileTabs activeTab={tab === 'databas' || tab === 'anvandare' ? tab : 'profil'} isAdmin={isAdmin} />
        <TabContent />
      </main>
    </>
  );
}

