import { NextRequest, NextResponse } from "next/server"; import { getServerSession } from "@/lib/auth"; import { db } from "@/lib/db"; import { campaigns } from "@/lib/db/schema"; import { desc } from "drizzle-orm"; import { v4 as uuidv4 } from "uuid"; export async function GET() { const session = await getServerSession(); if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); const all = db.select().from(campaigns).orderBy(desc(campaigns.createdAt)).all(); return NextResponse.json(all); } export async function POST(req: NextRequest) { const session = await getServerSession(); if (!session || !session.isAdmin) { return NextResponse.json({ error: "Forbidden" }, { status: 403 }); } try { const { name, gridSize = 5 } = await req.json(); if (!name) return NextResponse.json({ error: "Name required" }, { status: 400 }); if (gridSize < 3 || gridSize > 10) { return NextResponse.json({ error: "Grid size 3-10" }, { status: 400 }); } const id = uuidv4(); const now = new Date().toISOString(); db.insert(campaigns).values({ id, name, gridSize, createdBy: session.id, createdAt: now }).run(); return NextResponse.json({ id, name, gridSize }); } catch { return NextResponse.json({ error: "Failed to create campaign" }, { status: 500 }); } }