import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

export async function GET(request: NextRequest) {
  const searchParams = request.nextUrl.searchParams;
  const repoPath = searchParams.get("path");
  const days = parseInt(searchParams.get("days") || "365");
  const maxCommits = parseInt(searchParams.get("maxCommits") || "5000");

  if (!repoPath) {
    return NextResponse.json({ error: "Path missing" }, { status: 400 });
  }

  try {
    const { analyzeGitRepo } = await import("@/lib/git-analyzer");
    const stats = await analyzeGitRepo(repoPath, "main", days, {
      maxCommits,
      skipAI: true, // AI is done separately for speed
    });
    return NextResponse.json({ stats });
  } catch (error: any) {
    return NextResponse.json({ error: error?.message || "Analyse échouée" }, { status: 500 });
  }
}
