import pool from "@/lib/db";
import { getSession } from "@/lib/auth";
import { NextResponse } from "next/server";

export async function GET() {
    try {
        const session = await getSession();
        if (!session || session.user.tipo_acceso !== 1) {
            return NextResponse.json({ message: "No autorizado" }, { status: 401 });
        }

        const [rows]: any = await pool.execute(
            "SELECT id, nombre, apellidos, usuario, activo, tipo_acceso FROM empleados WHERE activo = 1 ORDER BY nombre ASC"
        );

        return NextResponse.json(rows);
    } catch (error) {
        console.error("Error fetching employees:", error);
        return NextResponse.json({ message: "Error interno" }, { status: 500 });
    }
}
