import { d as db, b as backups } from '../../../admin/create-upload-progress-table/POST-C-OoiMoW.js';
import { eq } from 'drizzle-orm';
import { q as quickValidate, v as verifyFileIntegrity } from '../../upload-encrypted/POST-CK3RHV5H.js';

async function handler(req, res) {
  try {
    const { backupId } = req.params;
    const { deviceId } = req.query;
    if (!backupId) {
      return res.status(400).json({
        success: false,
        error: "Backup ID is required"
      });
    }
    if (!deviceId) {
      return res.status(400).json({
        success: false,
        error: "Device ID is required for authorization"
      });
    }
    const backup = await db.select().from(backups).where(eq(backups.backupId, backupId)).limit(1);
    if (backup.length === 0) {
      return res.status(404).json({
        success: false,
        error: "Backup not found"
      });
    }
    const backupRecord = backup[0];
    if (backupRecord.deviceId !== deviceId) {
      return res.status(403).json({
        success: false,
        error: "Unauthorized: Backup belongs to different device"
      });
    }
    const filePath = backupRecord.filePath;
    if (!filePath) {
      return res.status(400).json({
        success: false,
        error: "Backup file path not found in database"
      });
    }
    const quickCheck = quickValidate(filePath);
    if (!quickCheck.valid) {
      return res.status(404).json({
        success: false,
        verified: false,
        error: quickCheck.error || "File validation failed"
      });
    }
    if (!backupRecord.hash || !backupRecord.checksum) {
      return res.status(400).json({
        success: false,
        error: "No verification data available for this backup"
      });
    }
    const expectedMetadata = {
      hash: backupRecord.hash,
      checksum: backupRecord.checksum,
      size: (backupRecord.size || 0) * 1024 * 1024
      // Convert MB to bytes
    };
    const verificationResult = await verifyFileIntegrity(filePath, expectedMetadata);
    if (verificationResult.valid) {
      await db.update(backups).set({
        verified: true,
        verifiedAt: /* @__PURE__ */ new Date()
      }).where(eq(backups.backupId, backupId));
    }
    const response = {
      success: true,
      verified: verificationResult.valid,
      details: {
        hash: verificationResult.hash,
        checksum: verificationResult.checksum,
        size: verificationResult.size,
        hashMatch: verificationResult.hash === expectedMetadata.hash,
        checksumMatch: verificationResult.checksum === expectedMetadata.checksum,
        sizeMatch: verificationResult.size === expectedMetadata.size
      }
    };
    if (!verificationResult.valid) {
      response.error = verificationResult.error;
    }
    res.json(response);
  } catch (error) {
    console.error("Backup verification error:", error);
    res.status(500).json({
      success: false,
      error: "Failed to verify backup",
      message: String(error)
    });
  }
}

export { handler as h };
