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

async function handler(req, res) {
  try {
    const { backupId, deviceId } = req.query;
    if (backupId && typeof backupId === "string") {
      const progress = await db.select().from(uploadProgress).where(eq(uploadProgress.backupId, backupId)).limit(1);
      if (progress.length === 0) {
        return res.status(404).json({
          success: false,
          error: "Progress not found"
        });
      }
      res.json({
        success: true,
        progress: progress[0]
      });
    } else if (deviceId && typeof deviceId === "string") {
      const progress = await db.select().from(uploadProgress).where(eq(uploadProgress.deviceId, deviceId));
      res.json({
        success: true,
        progress
      });
    } else {
      const progress = await db.select().from(uploadProgress).where(eq(uploadProgress.status, "uploading"));
      res.json({
        success: true,
        progress
      });
    }
  } catch (error) {
    console.error("Failed to fetch progress:", error);
    res.status(500).json({
      success: false,
      error: "Failed to fetch progress",
      message: String(error)
    });
  }
}

export { handler as h };
