import { asc, eq } from 'drizzle-orm' export default defineEventHandler((event) => { const projectId = param(event) const db = useDb() return db.select({ id: tables.chapters.id, index: tables.chapters.index, title: tables.chapters.title, synopsis: tables.chapters.synopsis, status: tables.chapters.status, writerAgentId: tables.chapters.writerAgentId, writerLocked: tables.chapters.writerLocked, activeVersionId: tables.chapters.activeVersionId, hasBrief: tables.chapters.brief, wordCount: tables.chapterVersions.wordCount, updatedAt: tables.chapters.updatedAt }).from(tables.chapters) .leftJoin(tables.chapterVersions, eq(tables.chapterVersions.id, tables.chapters.activeVersionId)) .where(eq(tables.chapters.projectId, projectId)) .orderBy(asc(tables.chapters.index)) .all() .map(c => ({ ...c, hasBrief: Boolean(c.hasBrief?.trim()), wordCount: c.wordCount ?? 0 })) })