// models/Product.ts
// import { Decimal128 } from 'mongodb';
import mongoose, { Document, Schema } from 'mongoose';

export interface IProduct extends Document {
  name: string;
  slug: string;
  description: string;
  shortDescription?: string;
  category: mongoose.Types.ObjectId;
  subcategory?: mongoose.Types.ObjectId;
  defaultImage?: string;
  weight: number;
  dimensions: {
    length: number;
    width: number;
    height: number;
  };
  isActive: boolean;
  isFeatured: boolean;
  seoTitle?: string;
  seoDescription?: string;
  createdAt: Date;
  updatedAt: Date;
}

const ProductSchema: Schema = new Schema(
  {
    name: {
      type: String,
      required: [true, 'Product name is required'],
      trim: true,
      maxlength: [200, 'Product name cannot exceed 200 characters'],
    },

    slug: {
      type: String,
      required: [true, 'Product slug is required'],
      unique: true,
      trim: true,
      lowercase: true,
    },

    description: {
      type: String,
      required: [true, 'Product description is required'],
      maxlength: [5000, 'Description cannot exceed 5000 characters'],
    },

    shortDescription: {
      type: String,
      maxlength: [500, 'Short description cannot exceed 500 characters'],
    },

    category: {
      type: Schema.Types.ObjectId,
      ref: 'Category',
      required: [true, 'Category is required'],
    },

    subcategory: {
      type: Schema.Types.ObjectId,
      ref: 'Category',
    },
      price: {
      type: Number,
      default: null,
    },
    defaultImage: {
      type: String,
      default: null,
    },

    weight: {
      type: Number,
      required: [true, 'Product weight is required'],
      min: [0, 'Weight cannot be negative'],
    },

    dimensions: {
      length: {
        type: Number,
        required: [true, 'Length is required'],
        min: [0, 'Length cannot be negative'],
      },
      width: {
        type: Number,
        required: [true, 'Width is required'],
        min: [0, 'Width cannot be negative'],
      },
      height: {
        type: Number,
        required: [true, 'Height is required'],
        min: [0, 'Height cannot be negative'],
      },
    },

    isActive: {
      type: Boolean,
      default: true,
    },

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

    seoTitle: {
      type: String,
      maxlength: [60, 'SEO title cannot exceed 60 characters'],
    },
    seoDescription: {
      type: String,
      maxlength: [160, 'SEO description cannot exceed 160 characters'],
    },
  },
  {
    timestamps: true,
  }
);


// ✅ OPTIONAL: Only keep this if you DON'T generate slug in API
ProductSchema.pre('save', async function (this: IProduct) {
  if (this.isModified('name') && !this.slug) {
    this.slug = this.name
      .toLowerCase()
      .replace(/[^a-zA-Z0-9]/g, '-')
      .replace(/-+/g, '-')
      .replace(/^-|-$/g, '');
  }
});


// ✅ Indexes
ProductSchema.index({ category: 1, isActive: 1 });
ProductSchema.index({ isFeatured: 1, isActive: 1 });
ProductSchema.index({ name: 'text', description: 'text' });

export default mongoose.models.Product ||
  mongoose.model<IProduct>('Product', ProductSchema);