const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

// 🔥 DELETE USER (Auth + Firestore)
exports.deleteUser = functions.https.onCall(async (data, context) => {
  try {
    const uid = data.uid;

    if (!uid) {
      throw new functions.https.HttpsError(
        "invalid-argument",
        "UID wajib diisi"
      );
    }

    // 🔐 Hapus dari Authentication
    await admin.auth().deleteUser(uid);

    // 🔥 Hapus dari Firestore
    await admin.firestore().collection("users").doc(uid).delete();

    return { success: true };
  } catch (error) {
    console.error(error);
    throw new functions.https.HttpsError(
      "internal",
      error.message
    );
  }
});