import mongoConnection from "$connections/mongo/mongo.connection.js";

import mongoose from "mongoose";

const mongooseSchema = mongoose.Schema;

export const schemaModel = {
  title: {
    type: String,
    required: true,
    trim: true,
  },
  description: {
    type: String,
    trim: true,
  },
  createMode: {
    type: String,
    enum: ["single", "batch"],
    default: "single",
    index: true,
  },
  usageContext: {
    type: String,
    enum: ["distribution", "club_reward"],
    default: "distribution",
    index: true,
  },
  quantity: {
    type: Number,
    required: true,
    min: 1,
  },
  amount: {
    type: Number,
    required: true,
    min: 1,
  },
  fundSeoRegisterNo: {
    type: String,
    required: true,
    trim: true,
    index: true,
  },
  fundName: {
    type: String,
    required: true,
    trim: true,
  },
  totalAmount: {
    type: Number,
    required: true,
    min: 1,
  },
  status: {
    type: String,
    enum: ["active", "partially_used", "used", "cancelled"],
    default: "active",
    index: true,
  },
  user: {
    type: mongooseSchema.Types.ObjectId,
    ref: "User",
    required: true,
    index: true,
  },
  expiresAt: {
    type: Date,
    required: true,
    index: true,
  },
  cancelledAt: Date,
};

export const schema = new mongooseSchema(schemaModel, { timestamps: true });

schema.index({ user: 1, createdAt: -1 });

export default mongoConnection.model("PaymentBatchCard", schema);
