const { onDocumentWritten } = require("firebase-functions/firestore");
const { getSunoTimestamps } = require("./lyrics");

exports.onProjectUpdate = onDocumentWritten(
  "projects/{projectId}",
  async (projectSnap) => {
    try {
      const { projectId } = projectSnap.params;

      const currentData = projectSnap?.data?.after?.data() || null;
      const previousData = projectSnap?.data?.before?.data() || null;

      if (!previousData?.songUrl && currentData?.songUrl) {
        await getSunoTimestamps(projectId);
      }
    } catch (e) {
      console.log(e);
    }
  },
);
