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

const WithdrawalRequestSchema = new Schema(
  {
    _id: stringId,
    ownerId: { type: String, ref: "User", required: true, index: true },
    amountTnd: { type: Number, required: true },
    amountLyd: { type: Number, required: true },
    exchangeRateRate: { type: Number, default: 0 },
    method: { type: String, enum: ["BANK_TND", "BANK_LYD"], required: true },
    status: { type: String, enum: WITHDRAWAL_STATUSES, default: "PENDING", index: true },
    note: { type: String, default: undefined },
    rejectionReason: { type: String, default: undefined },
    reviewedBy: { type: String, ref: "User", default: undefined },
    reviewedAt: { type: Date, default: undefined },
  },
  { timestamps: true },
);

WithdrawalRequestSchema.index({ status: 1, createdAt: -1 });
applyIdTransform(WithdrawalRequestSchema);

export const WithdrawalRequest = registerModel("WithdrawalRequest", WithdrawalRequestSchema);
