import { notFound } from 'next/navigation';
import Link from 'next/link';
import { auth } from '@/lib/auth';
import { getSessionById } from '@/services/sessions';
import { SwotBoard } from '@/components/swot/SwotBoard';
import { SessionLiveWrapper } from '@/components/collaboration';
import { EditableTitle } from '@/components/session';
import { Badge, CollaboratorDisplay } from '@/components/ui';

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

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

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

  const session = await getSessionById(id, authSession.user.id);

  if (!session) {
    notFound();
  }

  return (
    <main className="mx-auto max-w-7xl px-4 py-8">
      {/* Header */}
      <div className="mb-8">
        <div className="flex items-center gap-2 text-sm text-muted mb-2">
          <Link href="/sessions?tab=swot" className="hover:text-foreground">
            SWOT
          </Link>
          <span>/</span>
          <span className="text-foreground">{session.title}</span>
          {!session.isOwner && (
            <Badge variant="accent" className="ml-2">
              Partagé par {session.user.name || session.user.email}
            </Badge>
          )}
        </div>

        <div className="flex items-start justify-between">
          <div>
            <EditableTitle
              sessionId={session.id}
              initialTitle={session.title}
              isOwner={session.isOwner}
            />
            <div className="mt-2">
              <CollaboratorDisplay
                collaborator={session.resolvedCollaborator}
                size="lg"
                showEmail
              />
            </div>
          </div>
          <div className="flex items-center gap-3">
            <Badge variant="primary">{session.items.length} items</Badge>
            <Badge variant="success">{session.actions.length} actions</Badge>
            <span className="text-sm text-muted">
              {new Date(session.date).toLocaleDateString('fr-FR', {
                day: 'numeric',
                month: 'long',
                year: 'numeric',
              })}
            </span>
          </div>
        </div>
      </div>

      {/* Live Session Wrapper */}
      <SessionLiveWrapper
        sessionId={session.id}
        sessionTitle={session.title}
        currentUserId={authSession.user.id}
        shares={session.shares}
        isOwner={session.isOwner}
        canEdit={session.canEdit}
      >
        <SwotBoard sessionId={session.id} items={session.items} actions={session.actions} />
      </SessionLiveWrapper>
    </main>
  );
}
