import mongoose from "mongoose";

const shippingAddressSchema = new mongoose.Schema(
  {
    Name: {
      type: String,
    },
    phoneNumber: {
      type: Number,
    },
    street: {
      type: String,
      required: true,
    },
    city: {
      type: String,
      required: true,
      trim: true,
    },
    state: {
      type: String,
      required: true,
    },
    postalCode: {
      type: String,
      required: true,
      trim: true,
      validate: {
        validator: function (v) {
          return /^\d{6}$/.test(v);
        },
        message: "Postal code must be a 6-digit number",
      },
    },
    country: {
      type: String,
      required: true,
    },
    panNumber: {
      type: String,
      required: true,
      set: v => v.toUpperCase(), // Convert to uppercase before saving
      validate: {
        validator: function (v) {
          return /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/.test(v);
        },
        message: props => `${props.value} is not a valid PAN number!`,
      },
    },
    tradeName: {
      type: String,
      required: true,
    },
    gstNumber: {
      type: String,
      required: true,
      set: v => v.toUpperCase(), 
      validate: {
        validator: function (v) {
          return /^(\d{2}[A-Z]{5}\d{4}[A-Z]{1}\d[Z]{1}[A-Z\d]{1})$/.test(v);
        },
        message: props => `${props.value} is not a valid GST number!`,
      },
    },
    isDefault: {
      type: Boolean,
      default: false,
    },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
  },
  { timestamps: true, versionKey: false }
);

const ShippingAddress = mongoose.model("ShippingAddress", shippingAddressSchema);

export default ShippingAddress;
