import { NextResponse } from "next/server";
import { getSessionUser } from "@/lib/auth/get-session-user";
import { readPublishLogs } from "@/lib/logging/publish-logs";

export async function GET(request: Request) {
  const user = await getSessionUser();
  if (!user) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const { searchParams } = new URL(request.url);
  const projectId = searchParams.get("projectId") ?? undefined;
  const platform = searchParams.get("platform") ?? undefined;
  const limit = Number(searchParams.get("limit") ?? "100");
  const date = searchParams.get("date") ?? undefined;

  const logs = await readPublishLogs({ projectId, platform, limit, date });

  return NextResponse.json({
    userId: user.id,
    count: logs.length,
    logFile: `logs/publish-${date ?? new Date().toISOString().slice(0, 10)}.log`,
    logs,
  });
}
