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

async function handler(req, res) {
  try {
    const { deviceId, limit = "50", offset = "0" } = req.query;
    const limitNum = Math.min(parseInt(limit, 10) || 50, 200);
    const offsetNum = parseInt(offset, 10) || 0;
    let deviceBackups;
    let totalBackups;
    if (deviceId) {
      deviceBackups = await db.select().from(backups).where(eq(backups.deviceId, deviceId)).orderBy(desc(backups.completedAt)).limit(limitNum).offset(offsetNum);
      totalBackups = await db.select().from(backups).where(eq(backups.deviceId, deviceId));
    } else {
      deviceBackups = await db.select().from(backups).orderBy(desc(backups.completedAt)).limit(limitNum).offset(offsetNum);
      totalBackups = await db.select().from(backups);
    }
    res.json({
      success: true,
      backups: deviceBackups,
      total: totalBackups.length,
      limit: limitNum,
      offset: offsetNum
    });
  } catch (error) {
    console.error("Backup list error:", error);
    res.status(500).json({
      success: false,
      error: "Failed to fetch backups",
      message: String(error)
    });
  }
}

export { handler as h };
