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

import mongoose from "mongoose";
const mongooseSchema = mongoose.Schema;

export const schemaModel = {
  request: {
    type: mongooseSchema.Types.ObjectId,
    ref: "Request",
    unique: true,
    sparse: true,
    index: true,
  },
  fundKey: {
    type: String,
    required: true,
    uppercase: true,
    trim: true,
    index: true,
  },
  amountRial: {
    type: Number,
    required: true,
    min: 1,
  },
  status: {
    type: String,
    enum: ["initiated", "token_received", "paid", "failed", "cancelled"],
    default: "initiated",
    index: true,
  },
  gateway: {
    type: String,
    enum: ["parsian", "payment_card"],
    default: "parsian",
    index: true,
  },
  orderId: {
    type: String,
    required: true,
    unique: true,
    index: true,
  },
  token: {
    type: String,
    index: true,
  },
  receiptToken: {
    type: String,
    unique: true,
    sparse: true,
    index: true,
  },
  rrn: {
    type: String,
    index: true,
  },
  cardMask: String,
  terminalNo: Number,
  gatewayStatus: Number,
  gatewayRequest: mongooseSchema.Types.Mixed,
  gatewayResponse: mongooseSchema.Types.Mixed,
  callbackPayload: mongooseSchema.Types.Mixed,
  confirmResponse: mongooseSchema.Types.Mixed,
  clientIp: String,
  clientUa: String,
};

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

schema.set("toJSON", { virtuals: true });
schema.set("toObject", { virtuals: true });

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