import { NextResponse } from "next/server"; import { apiError } from "@/lib/api.ts"; import { requireUser } from "@/lib/auth/session.ts"; import { all } from "@/lib/db/index.ts"; export async function GET(req: Request) { try { const user = await requireUser(); const url = new URL(req.url); const q = url.searchParams.get("q")?.trim(); const archived = url.searchParams.get("archived") === "1" ? 1 : 0; let rows; if (q) { rows = all( `SELECT DISTINCT c.id, c.title, c.folder, c.pinned, c.archived, c.course_code, c.mode, c.knowledge_mode, c.model, c.updated_at FROM conversations c LEFT JOIN messages m ON m.conversation_id = c.id WHERE c.user_id = ? AND c.archived = ? AND (c.title LIKE ? OR m.content LIKE ?) ORDER BY c.pinned DESC, c.updated_at DESC LIMIT 100`, user.id, archived, `%${q}%`, `%${q}%` ); } else { rows = all( `SELECT id, title, folder, pinned, archived, course_code, mode, knowledge_mode, model, updated_at FROM conversations WHERE user_id = ? AND archived = ? ORDER BY pinned DESC, updated_at DESC LIMIT 200`, user.id, archived ); } return NextResponse.json({ conversations: rows }); } catch (e) { return apiError(e); } }