import mongoose from 'mongoose';
import { generateId } from '../../utils.js';

const addressSchema = new mongoose.Schema({
  building: { required: false, type: String },
  addressLine1: { required: false, type: String },
  addressLine2: { required: false, type: String },
  city: { required: false, type: String },
  state: { required: false, type: String },
  postcode: { required: false, type: String },
  country: { required: false, type: String },
});

const vendorSchema = new mongoose.Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    name: { required: true, type: String },
    website: { required: false, type: String },
    email: { required: false, type: String },
    phone: { required: false, type: String },
    contact: { required: false, type: String },
    country: { required: false, type: String },
    active: { required: true, type: Boolean, default: true },
    address: { required: false, type: addressSchema },
  },
  { timestamps: true }
);

vendorSchema.virtual('id').get(function () {
  return this._id;
});

vendorSchema.set('toJSON', { virtuals: true });

export const vendorModel = mongoose.model('vendor', vendorSchema);
