import mongoose from "mongoose";

const customerSchema = new mongoose.Schema(
  {
    cust_id: {
      type: String,
      unique: true,
      default: () => `CUST-${Date.now()}-${Math.floor(Math.random() * 1000)}`
    },
    cust_fname: { type: String, required: true, trim: true },
    cust_lname: { type: String, required: true, trim: true },
    location: { type: String, required: true, trim: true },
    telephone: {
      type: String,
      required: true,
      trim: true,
      match: [/^\+?[0-9]{10,15}$/, "Telephone must be 10-15 digits."]
    },
    ProductCode: { type: mongoose.Schema.Types.ObjectId, ref: "Product", required: true },
    order_number: { type: mongoose.Schema.Types.ObjectId, ref: "Order", required: true },
    quantity: { type: Number, required: true, min: 1 },
    total_price: { type: Number, required: true, min: 0 },

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

export default mongoose.model("Customer", customerSchema);