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

async function handler(req, res) {
  try {
    const { deviceId } = req.params;
    if (!deviceId) {
      return res.status(400).json({ error: "Device ID is required" });
    }
    const deviceResult = await db.select().from(devices).where(eq(devices.id, parseInt(deviceId))).limit(1);
    if (deviceResult.length === 0) {
      return res.status(404).json({ error: "Device not found" });
    }
    const device = deviceResult[0];
    const latestBackup = await db.select().from(backups).where(eq(backups.deviceId, device.deviceId)).orderBy(desc(backups.createdAt)).limit(1);
    if (latestBackup.length === 0) {
      return res.json({
        success: true,
        deviceId: device.id,
        deviceName: device.name,
        status: "no_backup",
        message: "No backup exists",
        hasBackup: false
      });
    }
    const backup = latestBackup[0];
    if (backup.status === "in_progress") {
      const progress = await db.select().from(uploadProgress).where(eq(uploadProgress.backupId, backup.backupId)).limit(1);
      if (progress.length > 0) {
        const uploadInfo = progress[0];
        const filesUploaded = uploadInfo.uploadedChunks || 0;
        const totalFiles = uploadInfo.totalChunks || 0;
        const percentComplete = totalFiles > 0 ? Math.round(filesUploaded / totalFiles * 100) : 0;
        return res.json({
          success: true,
          deviceId: device.id,
          deviceName: device.name,
          status: "in_progress",
          message: `Backup in progress ${filesUploaded}/${totalFiles}`,
          hasBackup: true,
          backup: {
            id: backup.id,
            backupId: backup.backupId,
            backupType: backup.backupType,
            status: backup.status,
            startedAt: backup.startedAt
          },
          progress: {
            filesUploaded,
            totalFiles,
            percentComplete,
            uploadedSize: uploadInfo.uploadedSize || 0,
            totalSize: uploadInfo.totalSize || 0
          }
        });
      }
      return res.json({
        success: true,
        deviceId: device.id,
        deviceName: device.name,
        status: "in_progress",
        message: "Backup in progress",
        hasBackup: true,
        backup: {
          id: backup.id,
          backupId: backup.backupId,
          backupType: backup.backupType,
          status: backup.status,
          startedAt: backup.startedAt
        }
      });
    }
    if (backup.status === "completed") {
      return res.json({
        success: true,
        deviceId: device.id,
        deviceName: device.name,
        status: "completed",
        message: "Backup complete",
        hasBackup: true,
        backup: {
          id: backup.id,
          backupId: backup.backupId,
          backupType: backup.backupType,
          status: backup.status,
          size: backup.size,
          startedAt: backup.startedAt,
          completedAt: backup.completedAt,
          encrypted: backup.encrypted,
          verified: backup.verified
        }
      });
    }
    return res.json({
      success: true,
      deviceId: device.id,
      deviceName: device.name,
      status: backup.status,
      message: backup.status === "failed" ? "Backup failed" : "Backup pending",
      hasBackup: true,
      backup: {
        id: backup.id,
        backupId: backup.backupId,
        backupType: backup.backupType,
        status: backup.status,
        startedAt: backup.startedAt,
        errorMessage: backup.errorMessage
      }
    });
  } catch (error) {
    console.error("Failed to get device backup status:", error);
    return res.status(500).json({
      error: "Failed to get backup status",
      message: String(error)
    });
  }
}

export { handler as h };
