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

async function handler(req, res) {
  try {
    const { serverId } = req.params;
    const { apiKey, storageUsed } = req.body;
    if (!serverId || !apiKey) {
      return res.status(400).json({
        success: false,
        error: "Missing required fields: serverId and apiKey are required"
      });
    }
    const server = await db.select().from(servers).where(eq(servers.serverId, serverId)).limit(1);
    if (server.length === 0) {
      return res.status(404).json({
        success: false,
        error: "Server not found"
      });
    }
    if (server[0].apiKey !== apiKey) {
      return res.status(401).json({
        success: false,
        error: "Invalid API key"
      });
    }
    await db.update(servers).set({
      status: "online",
      lastPing: /* @__PURE__ */ new Date(),
      storageUsed: storageUsed !== void 0 ? storageUsed : server[0].storageUsed,
      updatedAt: /* @__PURE__ */ new Date()
    }).where(eq(servers.id, server[0].id));
    const updatedServer = await db.select().from(servers).where(eq(servers.id, server[0].id)).limit(1);
    res.json({
      success: true,
      server: updatedServer[0]
    });
  } catch (error) {
    console.error("Server ping error:", error);
    res.status(500).json({
      success: false,
      error: "Failed to ping server",
      message: String(error)
    });
  }
}

export { handler as h };
