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

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

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

    activityId = vechicleData?.activityId;
    activityTypeId = vechicleData?.activityTypeId;
    licensePlateNumber = vechicleData?.licensePlateNumber;
    quoteNumber = vechicleData?.quoteNumber;
    ncb = vechicleData?.ncb;

    mobileNumber = RealmCustomerDetails?.mobileNumber;
    name = RealmCustomerDetails?.name;
    email = RealmCustomerDetails?.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", error);
  }
}

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