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

type PageProps = {
  params: Promise<{
    id: string;
  }>;
};


export async function PATCH(request, { params }: PageProps) {
  try {
    const { id } = await params;
    const body = await request.json();
    const { status } = body;

    if (status === undefined) {
      return NextResponse.json(
        { success: false, error: "Status is required" },
        { status: 400 }
      );
    }

    const conn = await pool.getConnection();
    
    try {
      await conn.query(
        "UPDATE scholarships_comments SET is_approved = ? WHERE id = ?",
        [status ? 1 : 0, id]
      );

      return NextResponse.json({
        success: true,
        message: `Reply ${status ? "approved" : "set to pending"} successfully`
      });
    } finally {
      conn.release();
    }
  } catch (err) {
    console.error("❌ PATCH Scholarship Reply API Error:", err);
    return NextResponse.json(
      {
        success: false,
        error: "Failed to update reply status",
        details: err.message,
      },
      { status: 500 }
    );
  }
}

export async function DELETE(request, { params }: PageProps) {
  try {
    const { id } = await params;

    const conn = await pool.getConnection();
    
    try {
      await conn.query("DELETE FROM scholarships_comments WHERE id = ?", [id]);

      return NextResponse.json({
        success: true,
        message: "Reply deleted successfully"
      });
    } finally {
      conn.release();
    }
  } catch (err) {
    console.error("❌ DELETE Scholarship Reply API Error:", err);
    return NextResponse.json(
      {
        success: false,
        error: "Failed to delete reply",
        details: err.message,
      },
      { status: 500 }
    );
  }
}