import { notFound } from 'next/navigation';
import { auth } from '@/lib/auth';
import { getYearReviewSessionById } from '@/services/year-review';
import { getUserTeams } from '@/services/teams';
import type { ResolvedCollaborator } from '@/services/auth';
import { YearReviewBoard, YearReviewLiveWrapper } from '@/components/year-review';
import { Badge, SessionPageHeader } from '@/components/ui';

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

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

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

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

  if (!session) {
    notFound();
  }

  return (
    <main className="mx-auto max-w-7xl px-4">
      <SessionPageHeader
        workshopType="year-review"
        sessionId={session.id}
        sessionTitle={session.title}
        isOwner={session.isOwner}
        canEdit={session.canEdit}
        ownerUser={session.user}
        date={session.updatedAt}
        collaborator={session.resolvedParticipant as ResolvedCollaborator}
        badges={<>
          <Badge variant="primary">{session.items.length} items</Badge>
          <Badge variant="default">Année {session.year}</Badge>
        </>}
      />

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