const mongoose = require("mongoose");

const deliverySchema = new mongoose.Schema(
  {
    deliveryType: {
      type: String,
      enum: [
        "Road",
        "Courier",
        "Transport",
        "Hand Delivery",
        "Other",
      ],
    },

    trackingNumber: String,

    personToReceive: String,

    locationBlock: String,

    inwardStatus: {
      type: String,
      enum: ["Pending", "Received", "Partial", "Completed"],
      default: "Pending",
    },

    isReturnable: {
      type: Boolean,
      default: false,
    },

    deliveryFiles: [
      {
        originalName: String,
        fileName: String,
        fileUrl: String,
        uploadedAt: {
          type: Date,
          default: Date.now,
        },
      },
    ],
  },
  { _id: false },
);

const inwardSchema = new mongoose.Schema(
  {
    vehicleId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "CommercialVehicle",
      //   required: true,
      default: null,
      //   index: true,
    },

    deliveryDetails: {
      type: deliverySchema,
      default: {},
    },

    product: {
      productId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Product",
      },
      productName: String,
    },

    // vendor: {
    //   vendorId: {
    //     type: mongoose.Schema.Types.ObjectId,
    //     ref: "Vendor",
    //   },
    //   vendorName: String,
    // },

    uom: {
      uomId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "UnitOfMeasurement",
      },
      symbol: String,
    },

    referenceNo: {
      type: String,
      required: true,
      trim: true,
      index: true,
    },

    vendorName: {
      type: String,
      trim: true,
    },

    totalQuantity: {
      type: Number,
      required: false,
    },

    // quantityUnit: {
    //   type: String,
    //   //   default: "Kg",
    // },

    status: {
      type: String,
      enum: ["Pending", "QC Done", "Warehouse Done", "Completed"],
      default: "Pending",
      index: true,
    },

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

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

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

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

module.exports = mongoose.model("Inward", inwardSchema);
