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, allocateGB } = req.body;
    if (!serverId || !apiKey || allocateGB === void 0) {
      return res.status(400).json({
        success: false,
        error: "Missing required fields: serverId, apiKey, and allocateGB 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"
      });
    }
    const currentAllocated = server[0].storageAllocated || 0;
    const newAllocated = currentAllocated + allocateGB;
    if (server[0].storageTotal && newAllocated > server[0].storageTotal) {
      return res.status(400).json({
        success: false,
        error: "Allocation exceeds total storage capacity",
        available: server[0].storageTotal - currentAllocated
      });
    }
    await db.update(servers).set({
      storageAllocated: newAllocated,
      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],
      storageAvailable: (updatedServer[0].storageTotal || 0) - (updatedServer[0].storageAllocated || 0)
    });
  } catch (error) {
    console.error("Storage allocation error:", error);
    res.status(500).json({
      success: false,
      error: "Failed to allocate storage",
      message: String(error)
    });
  }
}

export { handler as h };
