import { NextRequest, NextResponse } from 'next/server' import { auth } from '@/auth' import prisma from '@/lib/prisma' export async function GET( _request: NextRequest, { params }: { params: Promise<{ id: string }> }, ) { try { const session = await auth() if (!session?.user?.id) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) const { id: notebookId } = await params const site = await prisma.notebookSite.findUnique({ where: { notebookId }, select: { slug: true, isPublic: true, publishedAt: true, template: true, selectedNoteIds: true }, }) return NextResponse.json({ site: site || null }) } catch (error: any) { return NextResponse.json({ error: error.message }, { status: 500 }) } } function toSlug(str: string): string { return str .toLowerCase() .normalize('NFD').replace(/[\u0300-\u036f]/g, '') .replace(/[^a-z0-9\s-]/g, '') .trim() .replace(/\s+/g, '-') .replace(/-+/g, '-') .slice(0, 40) } function generateNotebookSlug(name: string): string { const base = toSlug(name) || 'carnet' const suffix = Math.random().toString(36).slice(2, 7) return `${base}-${suffix}` } export async function POST( request: NextRequest, { params }: { params: Promise<{ id: string }> }, ) { try { const session = await auth() if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const { id: notebookId } = await params const { selectedNoteIds, template, description } = await request.json() if (!selectedNoteIds || !Array.isArray(selectedNoteIds) || selectedNoteIds.length === 0) { return NextResponse.json({ error: 'selectedNoteIds is required' }, { status: 400 }) } const notebook = await prisma.notebook.findFirst({ where: { id: notebookId, userId: session.user.id, trashedAt: null }, select: { id: true, name: true, site: { select: { slug: true } } }, }) if (!notebook) return NextResponse.json({ error: 'Notebook not found' }, { status: 404 }) // Reuse existing slug or generate a new one const slug = notebook.site?.slug || generateNotebookSlug(notebook.name) const site = await prisma.notebookSite.upsert({ where: { notebookId }, create: { notebookId, slug, isPublic: true, template: template || 'magazine', selectedNoteIds: JSON.stringify(selectedNoteIds), description: description || null, }, update: { isPublic: true, template: template || 'magazine', selectedNoteIds: JSON.stringify(selectedNoteIds), description: description || null, updatedAt: new Date(), }, }) return NextResponse.json({ slug: site.slug, url: `/c/${site.slug}` }) } catch (error: any) { console.error('[notebooks/publish] Error:', error) return NextResponse.json({ error: error.message || 'Failed to publish' }, { status: 500 }) } } export async function DELETE( _request: NextRequest, { params }: { params: Promise<{ id: string }> }, ) { try { const session = await auth() if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const { id: notebookId } = await params const notebook = await prisma.notebook.findFirst({ where: { id: notebookId, userId: session.user.id }, select: { id: true }, }) if (!notebook) return NextResponse.json({ error: 'Not found' }, { status: 404 }) await prisma.notebookSite.deleteMany({ where: { notebookId } }) return NextResponse.json({ success: true }) } catch (error: any) { return NextResponse.json({ error: error.message }, { status: 500 }) } }