const admin = require("firebase-admin");
const {Storage} = require("@google-cloud/storage");
const logger = require("firebase-functions/logger");
const auth = require("../utils/auth");

const storage = new Storage();

exports.moveEventFileV2 = async (req, res) => {
  try {
    const decodedToken = await auth.authenticateUser(req);
    const {sourcePath, destinationPath} = req.body.data || {};

    if (!sourcePath || !destinationPath) {
      res.status(400).json({error: "Source and destination paths are required."});
      return;
    }

    const bucketName = admin.storage().bucket().name;
    const bucket = storage.bucket(bucketName);

    await bucket.file(sourcePath).copy(bucket.file(destinationPath));
    await bucket.file(sourcePath).delete();
    const [url] = await bucket.file(destinationPath).getSignedUrl({
      action: "read",
      expires: "03-01-2500",
    });

    res.status(200).json({url});
  } catch (error) {
    logger.error("Error moving file:", error);
    res.status(500).json({error: error.message});
  }
};
