import { Schema } from "mongoose";
import { applyIdTransform, registerModel, stringId } from "../schema";
import { LOYALTY_TXN_TYPES } from "../types";

const LoyaltyTxnSchema = new Schema(
  {
    _id: stringId,
    userId: { type: String, ref: "User", required: true, index: true },
    type: { type: String, enum: LOYALTY_TXN_TYPES, required: true },
    delta: { type: Number, required: true },
    pointsAfter: { type: Number, required: true },
    bookingId: { type: String, ref: "Booking", default: undefined },
    note: { type: String, default: undefined },
  },
  { timestamps: { createdAt: true, updatedAt: false } },
);

LoyaltyTxnSchema.index({ userId: 1, createdAt: -1 });
applyIdTransform(LoyaltyTxnSchema);

export const LoyaltyTxn = registerModel("LoyaltyTxn", LoyaltyTxnSchema);
