import { NextResponse } from "next/server";
import { db } from "../../lib/db";

export async function GET() {
  try {
    const [rows] = await db.query(`
      SELECT id, name, code
      FROM units
      ORDER BY name ASC
    `);

    return NextResponse.json({
      data: rows,
    });

  } catch (err: any) {
    return NextResponse.json(
      { message: "Failed to fetch units", error: err.message },
      { status: 500 }
    );
  }
}