// Impor Firebase Admin SDK
import CryptoJs from 'crypto-js';
import { cert, getApps, initializeApp } from "firebase-admin/app";
import { getMessaging } from "firebase-admin/messaging";

const enc = "U2FsdGVkX1+ATdSSI7vdTGLCA8f6WjifHPoesp/SXL2VtKVRV4QkKjrU/CYBxWyA48nQqDRUlb433HIA71kck83Du+EoN5mBct/PUY+rxNUx89oXHD4fh1JPvY60upWrp7BHgyos8yFZDLNpscbZiGCqx91WEXCyWj0ExXGL3ZZNHWEO8lXrix/9xmtR37VYjPy/xshGGABQMKwfdBCGiubSpT4Z/dZ0d5MBcQDlg7vqST4oKlyyyiIUyFs9+WKsXIbXlRxsgaQDYf2yz/4ESQ+Rmc2U5PeN7xhrHe/qiU3WwB+ESKuS5lT8vYv3r5MZy6OOfTnhA1HP68JoL7NUVrTuM0ce6QAqY274qBGAyQl4xHIwoZ7q+hhMIykaMUDC8cpq67qpMnhXo16X8loOesLfWtUXGwCX6kXwdkRDQrBRz7GhxDBI/0GUnULJwi4NC/tK68rqpl7nJyhR7Z4v24Na8y2Z7wibwZMvNfCyHcPFfdQdGovViR4/wCdMxy5D3sWJnEk3V7+04s/n9PnZ5dOSePAwG1HpDxLJmTCyIKqdGbSrwEcIYOHYcqWPMUjG68+zRNyjFMn2cqh1KAWmMJYlIP4cAxs38q3UJx4PlV3Q6sTzoQ39Ydxy3PwuZ5Lp7TAVHEoyYdHL0+v2A19kpSg+LZFufMeAD1YRnIGMtvaEIaQWJzFFdkCvMP8WMOSzg6sCCvawXnYxnjcfqCHPATYwCHCNKG1a/B6bRLiCU5dHHp6LLbSIwG/l7y29nW2NM6uSvBGy6rbzIVx6LjYWypAXO7MKYfJgBBAUafWZTd3qyJ0ra2BEQiXOGLnv7V+0jCCptD8SLDsuhpn7YI3wpeavZdVS+7J0dwA8DcJbTfpCEQjZkQSowhg+CR05vbaxv05S+8hjpZZdsP49aXnjfxuIrkEYNjsOOfY4Ng1Fel+ki88svHfJ7PQ9JsRx0OfX4Yj91exdCtXBvQxv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I/wz0TrFfjTnaWp2t38tlFKhYkipFif545SRt8iyTJSVx1+68J31uNG6P4Vtu+zFNHJEbrW45aGSsJfjN8M8sWhkjK8A8jpg=="
const decrypt = CryptoJs.AES.decrypt(enc, "amal").toString(CryptoJs.enc.Utf8)

// Fungsi untuk mengirim notifikasi FCM
export async function sendFCMNotification(token: string) {
   // const serviceAccount = await fs.readFile(path.join(process.cwd(), "key.json"));
   if(getApps().length === 0){
      initializeApp({
         credential: cert(JSON.parse(decrypt)),
      });
   }

   try {
      // Konfigurasi pesan
      const message = {
         notification: {
            title: "Notifikasi Encrypted api",
            body: "Ini adalah isi notifikasi key diencrypt dan api",
         },
         token,
         data: {
            key1: "value1",
            key2: "value2",
         },
         // Opsional: konfigurasi Android
         android: {
            priority: "high",
            notification: {
               sound: "default",
               channelId: "default_channel",
            },
         },
         // Opsional: konfigurasi APNS (iOS)
         apns: {
            payload: {
               aps: {
                  sound: "default",
               },
            },
         },
      };

      // Kirim pesan
      const response = await getMessaging().send(message as any);
      console.log("Notifikasi berhasil dikirim:", response);
      return response;
   } catch (error) {
      console.error("Error mengirim notifikasi:", error);
      throw error;
   }
}

// sendFCMNotification('c89yuexsS_uc1tOErVPu5a:APA91bEb6tEKXAfReZjFVJ2mMyOzoW_RXryLSnSJTpbIVV3G0L_DCNkLuRvJ02Ip-Erz88QCQBAt-C2SN8eCRxu3-v1sBzXzKPtDv-huXpkjXsyrkifqvUo')