const firebase = require("firebase");
require("firebase/firestore");
require("firebase/app");
const express = require("express");
const app = express();
app.use(express.json());

const firebaseConfig = {
  apiKey: "AIzaSyDGslRhmh-ABzM7ID8YDZkSubt4hKAdGm4",
  authDomain: "tenecall-app.firebaseapp.com",
  projectId: "tenecall-app",
  storageBucket: "tenecall-app.appspot.com",
  messagingSenderId: "243534415023",
  appId: "1:243534415023:web:e265e860e96ad4b1f4c05a",
  measurementId: "G-6L8JPZ5VL7",
};

firebase.initializeApp(firebaseConfig);

firebase
  .auth()
  .signInWithEmailAndPassword("Api@api.com", "api1234")
  .then((userCredential) => {
    var user = userCredential.user;
    if (user) {
      app.get("/device/:id", async (req, res) => {
        let id = req.params.id;

        var ref = firebase
          .firestore()
          .collection("Internal_accesspoint")
          .where("MAC", "==", id)
          .get();

        ref
          .then(function (doc) {
            // if (doc.exists) {
            if (doc.empty) {
              var ref = firebase
                .firestore()
                .collection("External_accesspoint")
                .where("MAC", "==", id)
                .get();

              ref
                .then(function (doc) {
                  // if (doc.exists) {
                  if (doc.empty) {
                    res.send({ msg: "Data Not Found!" });
                  } else {
                    doc.forEach((element) => {
                      if (element.data().business_id !== "") {
                        let business_id = element.data().business_id;
                        var ref_business = firebase
                          .firestore()
                          .collection("businesses")
                          .doc(business_id)
                          .get();

                        ref_business.then(function (locationDoc) {
                          res.send({
                            // BLE_MAC: element.data().BLE_MAC,
                            MAC: element.data().MAC,
                            accesspoint_sn: element.data().accesspoint_sn,
                            // model: element.data().model,
                            // business_id: element.data().business_id,
                            location: locationDoc.data().location,
                            business_nickname:
                              locationDoc.data().business_nickname,
                          });
                        });
                      } else {
                        res.send({
                          //   BLE_MAC: element.data().BLE_MAC,
                          MAC: element.data().MAC,
                          accesspoint_sn: element.data().accesspoint_sn,
                          //   model: element.data().model,
                        });
                      }
                    });
                  }
                })
                .catch(function (error) {
                  console.log({ msg: "Error getting document:", error });
                });
            } else {
              doc.forEach((element) => {
                if (element.data().business_id !== "") {
                  let business_id = element.data().business_id;
                  var ref_business = firebase
                    .firestore()
                    .collection("businesses")
                    .doc(business_id)
                    .get();

                  ref_business.then(function (locationDoc) {
                    res.send({
                      // BLE_MAC: element.data().BLE_MAC,
                      MAC: element.data().MAC,
                      accesspoint_sn: element.data().accesspoint_sn,
                      // model: element.data().model,
                      // business_id: element.data().business_id,
                      location: locationDoc.data().location,
                      business_nickname: locationDoc.data().business_nickname,
                    });
                  });
                } else {
                  res.send({
                    //   BLE_MAC: element.data().BLE_MAC,
                    MAC: element.data().MAC,
                    accesspoint_sn: element.data().accesspoint_sn,
                    //   model: element.data().model,
                  });
                }
              });
            }
          })
          .catch(function (error) {
            console.log({ msg: "Error getting document:", error });
          });
      });
    }
  })
  .catch((error) => {
    var errorCode = error.code;
    var errorMessage = error.message;
  });

app.listen(4000, () => console.log("Up & Running at 4000"));
