import { firestore } from "firebase-admin";
import { Timestamp } from "firebase-admin/firestore";
export interface UserWalletModel {
  id: string;
  balance: number;
  updatedAt: Date;
  userId: string;
}

type HistoryType = "TopUp" | "Payment";
export interface UserWalletHistory {
  id?: string;
  initialAmount: number;
  finalAmount: number;
  transactionAmount: number;
  createdAt: Date;
  updatedAt?: Date;
  userId: string;
  status: string;
  description?: string;
  type: HistoryType;
}

export const UserWalletModelConverter: firestore.FirestoreDataConverter<UserWalletModel> =
  {
    toFirestore: function (modelObject: any): firestore.DocumentData {
      const data = { ...modelObject };
      return data;
    },
    fromFirestore: function (
      snapshot: firestore.QueryDocumentSnapshot<firestore.DocumentData>
    ): UserWalletModel {
      const data = snapshot.data()!;
      if (data.createdAt) {
        data.createdAt = (data.createdAt as firestore.Timestamp).toDate();
      }
      data.id = snapshot.id;
      return data as UserWalletModel;
    },
  };

export const UserWalletHistoryConverter: firestore.FirestoreDataConverter<UserWalletHistory> =
  {
    toFirestore: function (modelObject: any): firestore.DocumentData {
      const data = { ...modelObject };
      if (data.createdAt) {
        data.createdAt = Timestamp.fromDate(data.createdAt as Date);
      }
      return data;
    },
    fromFirestore: function (
      snapshot: firestore.QueryDocumentSnapshot<firestore.DocumentData>
    ): UserWalletHistory {
      const data = snapshot.data()!;
      if (data.createdAt) {
        data.createdAt = (data.createdAt as firestore.Timestamp).toDate();
      }
      data.id = snapshot.id;
      return data as UserWalletHistory;
    },
  };
