import crashlytics from "@react-native-firebase/crashlytics";
import Database from "../../../../../../Components/common/Database";
import analytics from "@react-native-firebase/analytics";

let realm = Database.getRealm();

export function errorRecord(err) {
  crashlytics().recordError(err?.message);
  crashlytics().log(err?.message);
}

export async function firebaseAttributes(log) {
  try {
    var vechicleData = realm.objects("PIVechicleData");
    var RealmCustomerDetails = realm.objects("CustomerDetails");
    let mobileNumber,
      activityId,
      activityTypeId,
      licensePlateNumber,
      quoteNumber,
      ncb,
      name,
      email;

    activityId = vechicleData[0]?.activityId;
    activityTypeId = vechicleData[0].activityTypeId;
    licensePlateNumber = vechicleData[0]?.licensePlateNumber;
    quoteNumber = vechicleData[0]?.quoteNumber;
    ncb = vechicleData[0]?.ncb;

    mobileNumber = RealmCustomerDetails[0]?.mobileNumber;
    name = RealmCustomerDetails[0]?.name;
    email = RealmCustomerDetails[0]?.email;

    await Promise.all([
      crashlytics().setUserId(mobileNumber),
      crashlytics().setAttributes({
        activityId: activityId.toString(),
        activityTypeId: activityTypeId.toString(),
        licensePlateNumber: licensePlateNumber,
        quoteNumber: quoteNumber,
        ncb: ncb.toString(),
        mobileNumber: mobileNumber,
        name: name,
        Email: email,
      }),
    ]);
    crashlytics().log(log);
  } catch (error) {
    console.log("Error in firebaseAttributes");
  }
}

export function firebaseEvents(
  eventName,
  data,
  activityId,
  activityTypeId,
  retryCount
) {
  console.log(
    "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",
    activityId,
    activityTypeId,
    retryCount
  );
  analytics().logEvent(eventName, {
    Data: JSON.stringify(data, null, "  "),
    timestamp: Date.now().toString(),
    activityId: activityId,
    activityTypeId: activityTypeId,
    retryCount: retryCount,
  });
}
