import { NextResponse } from "next/server";
import { getServiceSupabase } from "@/lib/supabase/client";
import { getAuthenticatedUser } from "@/lib/server/auth";

export const runtime = "nodejs";

// GET - fetch single sprint by ID
export async function GET(
  request: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const user = await getAuthenticatedUser();
    if (!user) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }
    
    const { id } = await params;
    
    const supabase = getServiceSupabase();
    const { data, error } = await supabase
      .from("sprints")
      .select("*")
      .eq("id", id)
      .single();
    
    if (error) {
      if (error.code === "PGRST116") {
        return NextResponse.json({ error: "Sprint not found" }, { status: 404 });
      }
      throw error;
    }
    
    return NextResponse.json({ sprint: data });
  } catch (error) {
    console.error(">>> API GET /sprints/[id] error:", error);
    return NextResponse.json({ error: "Failed to fetch sprint" }, { status: 500 });
  }
}
