import { NextRequest, NextResponse } from "next/server";
import { getTimelineDays, getEarliestDate } from "@/lib/chronicle-db";

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url);
    const before = searchParams.get("before") ?? null;
    const limit = Math.min(parseInt(searchParams.get("limit") ?? "7"), 30);

    const days = getTimelineDays(before, limit);
    const earliest = getEarliestDate();

    return NextResponse.json({ days, earliest });
  } catch (err) {
    console.error("[chronicle/timeline]", err);
    return NextResponse.json({ error: "Failed to load timeline" }, { status: 500 });
  }
}
