import { NextResponse } from "next/server";
import { db } from "../../../../lib/db";

export async function GET(
  req: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    // console.log("Idnya: ", id);
    const location_id = Number(id);

    const [rows]: any = await db.query(
      "SELECT id, name FROM locations WHERE id = ? LIMIT 1",
      [location_id]
    );
    // [location_id]

    if (!rows.length) {
      return NextResponse.json(
        { message: "Location tidak ditemukan" },
        { status: 404 }
      );
    }

    return NextResponse.json({
      location: rows[0],
    });
  } catch (err) {
    console.error(err);
    return NextResponse.json(
      { message: "Server error" },
      { status: 500 }
    );
  }
}