import { Schema, model } from 'mongoose';
import { paginate, toJSON } from '@/shared/utils/plugins';
import { ISource, ISourceModel } from '../constructionStatus.interfaces';

const sourceSchema = new Schema<ISource>(
  {
    name: { type: String, required: true, trim: true },
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },
    company: { type: Schema.Types.ObjectId, ref: 'Company', required: true },
    isDefault: { type: Boolean, default: false },
  },
  { timestamps: true },
);

// ✅ Compound unique index (name + company)
// sourceSchema.index({ company: 1, name: 1 }, { unique: true });

sourceSchema.plugin(toJSON);
sourceSchema.plugin(paginate);

const Source = model<ISource, ISourceModel>('Source', sourceSchema);
export default Source;
