import { NextResponse } from "next/server";
import { getSessionUser } from "@/lib/auth/get-session-user";
import { readAutoPostLogs } from "@/lib/logging/auto-post-logger";

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 jobId = searchParams.get("jobId") ?? undefined;
  const limit = Number(searchParams.get("limit") ?? "100");
  const date = searchParams.get("date") ?? undefined;

  const logs = await readAutoPostLogs({ projectId, jobId, limit, date });

  return NextResponse.json({
    userId: user.id,
    count: logs.length,
    logFile: `logs/auto-post-${date ?? new Date().toISOString().slice(0, 10)}.log`,
    logs,
  });
}
