import mongoose from "mongoose";

const orderSchema = new mongoose.Schema(
  {
    // Keep order_number as your unique identifier
    order_number: {
      type: String,
      unique: true,
      default: () => `ORD-${Date.now()}-${Math.floor(Math.random() * 1000)}`
    },

    // Original date (kept for display)
    order_date: { type: Date, required: true },

    // Enforce uniqueness by calendar day (prevents timezone/time drift)
    order_date_key: {
      type: String, // YYYY-MM-DD
      required: true,
      unique: true,
      index: true
    },

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

    userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true
    }
  },
  { timestamps: true }
);

export default mongoose.model("Order", orderSchema);
