import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { getTrips, createTrip } from "@/lib/queries";

export async function GET(req: NextRequest) {
  const user = await getCurrentUser(req);
  if (!user) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
  const trips = await getTrips(user.id);
  return NextResponse.json(trips);
}

export async function POST(req: NextRequest) {
  const user = await getCurrentUser(req);
  if (!user) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
  const body = await req.json();
  const { name, description, start_date, end_date, color } = body;
  if (!name?.trim()) return NextResponse.json({ error: "name is required" }, { status: 400 });
  const trip = await createTrip(user.id, { name: name.trim(), description, start_date, end_date, color });
  return NextResponse.json(trip, { status: 201 });
}
