const mongoose = require("mongoose");

const returnableGoodsSchema = new mongoose.Schema(
  {
    /* ===== BASIC ITEM INFO ===== */

    itemId: {
      type: String,
      required: true,
      trim: true,
      //   index: true,
    },

    itemName: {
      type: String,
      required: true,
      trim: true,
    },
    // vendorId: {
    //   type: mongoose.Schema.Types.ObjectId,
    //   ref: "Vendor",
    // },
    vendorName: {
      type: String,
      required: true,
    },

    itemPhoto: {
      type: String, // file url
      default: null,
    },

    quantity: {
      type: Number,
      required: false,
      min: 1,
    },
    sequenceNumber: {
      type: Number,
      //   index: true,
    },
    condition: {
      type: String,
      enum: ["Good", "Damaged", "Repair", "Lost"],
      default: "Good",
    },

    remarks: {
      type: String,
      trim: true,
      default: "",
    },

    /* ===== MOVEMENT INFO ===== */

    pickupDate: {
      type: Date,
      required: false,
      //   index: true,
    },

    returnDate: {
      type: Date,
      //   required: true,
      //   index: true,
    },

    referenceBy: {
      type: mongoose.Schema.Types.ObjectId,
      // type : String,
      ref: "User",
      //   index: true,
    },

    receiverName: {
      type: String,
      //   required: true,
      trim: true,
    },

    receiverPhone: {
      type: String,
      trim: true,
      default: "",
    },

    returnBy: {
      type: String,
      //   required: true,
      trim: true,
    },

    returnByPhone :{
      type : String,
      trim : true,
      default : "",
    },
    // approvedBy: {
    //   type: mongoose.Schema.Types.ObjectId,
    //   ref: "User",
    //   default: null,
    // },

    /* ===== STATUS FLOW ===== */

    status: {
      type: String,
      enum: ["Pending", "Returned", "Closed", "Cancelled"],
      default: "Pending",
      //   index: true,
    },

    actualReturnDate: {
      type: Date,
      default: null,
    },

    /* ===== ORG MULTI TENANT ===== */

    orgId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Organization",
      required: true,
      //   index: true,
    },

    /* ===== AUDIT FIELDS ===== */

    createdBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      default: null,
    },

    updatedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      default: null,
    },

    isDeleted: {
      type: Boolean,
      default: false,
      //   index: true,
    },
  },
  {
    timestamps: true, // createdAt updatedAt
  },
);

module.exports = mongoose.model("ReturnableGoods", returnableGoodsSchema);
