import { firestore } from "firebase-admin";
import { PaymentMethod } from "../payment-functions";
import { Timestamp } from "firebase-admin/firestore";

export enum TopUpOrderStatus {
  created = "created",
  paid = "paid",
  canceled = "canceled",
  failed = "failed",
}

export interface TopUpOrderModel {
  id?: string;
  amount: number;
  currency: string;
  status: TopUpOrderStatus;
  createdAt: Date;
  updatedAt?: Date;
  stripePaymentId?: string;
  userId: string;
  paymentMethod: PaymentMethod;
}

export const TopUpOrderModelConverter: firestore.FirestoreDataConverter<TopUpOrderModel> =
  {
    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>
    ): TopUpOrderModel {
      const data = snapshot.data()!;
      if (data.createdAt) {
        data.createdAt = (data.createdAt as firestore.Timestamp).toDate();
      }
      if (data.updatedAt) {
        data.updatedAt = (data.updatedAt as firestore.Timestamp).toDate();
      }
      data.id = snapshot.id;
      return data as TopUpOrderModel;
    },
  };
