import pool from "../../../../lib/db/db";
import { NextResponse } from "next/server";

export async function GET(request) {
  try {
    const { searchParams } = new URL(request.url);
    const parentCommentId = searchParams.get("parent_comment_id");

    if (!parentCommentId) {
      return NextResponse.json(
        { success: false, error: "Parent comment ID is required" },
        { status: 400 }
      );
    }

    const conn = await pool.getConnection();

    try {
      const [rows] = await conn.query(
        `SELECT 
          id as reply_id,
          first_name,
          last_name,
          email,
          phone,
          comment,
          is_approved as status,
          created_at
        FROM scholarships_comments 
        WHERE parent_id = ?
        ORDER BY created_at ASC`,
        [parentCommentId]
      );

      return NextResponse.json({
        success: true,
        data: rows
      });
    } finally {
      conn.release();
    }
  } catch (err) {
    console.error("❌ GET Scholarship Replies API Error:", err);
    return NextResponse.json(
      {
        success: false,
        error: "Failed to fetch replies",
        details: err.message,
      },
      { status: 500 }
    );
  }
}