import { firestore } from "firebase-admin";
import { PaymentMethod } from "./payment-functions";
import { RoomModel } from "./model/chat/roomModel";
import {
  TopUpOrderModel,
  TopUpOrderModelConverter,
} from "./model/topup_order_model";
import {
  UserWalletHistory,
  UserWalletHistoryConverter,
  UserWalletModel,
  UserWalletModelConverter,
} from "./model/user_wallet_model";

export enum OrderStatus {
  notPay = "notPay",
  pay = "payment_success",
}

export enum Role {
  Doctor = "doctor",
  User = "user",
  Admin = "admin",
}

export enum AccountStatus {
  Active = "active",
  NonActive = "nonactive",
}

export type OrderModel = {
  charged: boolean;
  status: OrderStatus;
  timeSlotId: string;
  userId: string;
  createdAt: firestore.Timestamp;
  stripePaymentId?: string;
  paymentMethod?: PaymentMethod;
  amount?: number;
  linkReceipt?: string;
  currency?: string;
};

export type TimeSlotModel = {
  available: boolean;
  doctorId: string;
  duration: number;
  userId: string;
  charged: boolean | undefined;
  parentTimeslotId: string | null | undefined;
  price: number;
  timeSlot: firestore.Timestamp;
  bookByWho: BookByWho | undefined;
  doctor: DoctorModel | undefined;
  purchaseTime: firestore.Timestamp | undefined | null;
  status: string | undefined;
};

type DoctorModel = {
  doctorName: string;
  doctorPicture: string;
  accountStatus: boolean;
  balance: number;
  createdAt: firestore.Timestamp;
  updatedAt: firestore.Timestamp;
  doctorBasePrice: number;
  doctorBiography: string;
  doctorCategory: DoctorCategoryModel;
  doctorHospital: string;
};

type DoctorCategoryModel = {
  categoryName: string;
  iconUrl: string;
  categoryId: string;
};

export type RefundModel = {
  createdAt: firestore.Timestamp;
  timeSlotId: string;
  status: string;
  amount: number;
  refundId: string;
  currency: string;
  paymentId?: string;
};

export type UserModel = {
  createdAt: firestore.Timestamp;
  displayName: string;
  doctorId: string;
  role: Role;
  token: string | string[];
  uid: string;
  email: string;
  photoUrl: string;
};

export type BookByWho = {
  displayName: string;
  photoUrl: string;
  userId: string;
};

const createCollection = <T = firestore.DocumentData>(
  collectionName: string
) => {
  return firestore().collection(
    collectionName
  ) as firestore.CollectionReference<T>;
};

export const orderCol = createCollection<OrderModel>("Order");
export const timeSlotCol = createCollection<TimeSlotModel>("DoctorTimeslot");
export const doctorCol = createCollection<DoctorModel>("Doctors");
export const usersCol = createCollection<UserModel>("Users");
export const refundCol = createCollection<RefundModel>("Refund");
export const roomsCol = createCollection<Partial<RoomModel>>("Rooms");
export const topUpOrderCol = createCollection<TopUpOrderModel>(
  "TopUpOrder"
).withConverter(TopUpOrderModelConverter);
export const userWalletCol = createCollection<UserWalletModel>(
  "UserWallet"
).withConverter(UserWalletModelConverter);

// Create a nested collection for userWallet/history/
export const userWalletHistoryCol = (
  userWalletId: string
): firestore.CollectionReference<UserWalletHistory> => {
  const userWalletRef = firestore().collection("UserWallet").doc(userWalletId);
  return userWalletRef
    .collection("History")
    .withConverter(UserWalletHistoryConverter);
};
