import { d as db, c as devices } from '../../admin/create-upload-progress-table/POST-C-OoiMoW.js';
import { eq } from 'drizzle-orm';

async function handler(req, res) {
  try {
    const deviceId = Number(req.params.deviceId);
    if (!deviceId || isNaN(deviceId)) {
      return res.status(400).json({
        success: false,
        error: "Invalid device ID"
      });
    }
    console.log("🗑️  Deleting device:", deviceId);
    const existingDevice = await db.select().from(devices).where(eq(devices.id, deviceId)).limit(1);
    if (existingDevice.length === 0) {
      return res.status(404).json({
        success: false,
        error: "Device not found"
      });
    }
    await db.delete(devices).where(eq(devices.id, deviceId));
    console.log("✅ Device deleted successfully:", deviceId);
    res.json({
      success: true,
      message: "Device deleted successfully"
    });
  } catch (error) {
    console.error("❌ Delete device error:", error);
    res.status(500).json({
      success: false,
      error: "Failed to delete device",
      message: String(error)
    });
  }
}

export { handler as h };
