import { notFound } from 'next/navigation';
import { auth } from '@/lib/auth';
import { getMotivatorSessionById } from '@/services/moving-motivators';
import { getUserTeams } from '@/services/teams';
import type { ResolvedCollaborator } from '@/services/auth';
import { MotivatorBoard, MotivatorLiveWrapper } from '@/components/moving-motivators';
import { Badge, SessionPageHeader } from '@/components/ui';

interface MotivatorSessionPageProps {
  params: Promise<{ id: string }>;
}

export default async function MotivatorSessionPage({ params }: MotivatorSessionPageProps) {
  const { id } = await params;
  const authSession = await auth();

  if (!authSession?.user?.id) {
    return null;
  }

  const [session, userTeams] = await Promise.all([
    getMotivatorSessionById(id, authSession.user.id),
    getUserTeams(authSession.user.id),
  ]);

  if (!session) {
    notFound();
  }

  return (
    <main className="mx-auto max-w-7xl px-4">
      <SessionPageHeader
        workshopType="motivators"
        sessionId={session.id}
        sessionTitle={session.title}
        isOwner={session.isOwner}
        canEdit={session.canEdit}
        ownerUser={session.user}
        date={session.date}
        collaborator={session.resolvedParticipant as ResolvedCollaborator}
        badges={
          <Badge variant="primary">
            {session.cards.filter((c) => c.influence !== 0).length} / 10 évalués
          </Badge>
        }
      />

      {/* Live Wrapper + Board */}
      <MotivatorLiveWrapper
        sessionId={session.id}
        sessionTitle={session.title}
        currentUserId={authSession.user.id}
        shares={session.shares}
        isOwner={session.isOwner}
        canEdit={session.canEdit}
        userTeams={userTeams}
      >
        <MotivatorBoard sessionId={session.id} cards={session.cards} canEdit={session.canEdit} />
      </MotivatorLiveWrapper>
    </main>
  );
}
