/**
 * Module Data Model
 *
 * Generic storage for dynamic module data (brand strategy, etc.).
 */

import mongoose, { Schema, Document } from 'mongoose';

export interface IModuleData extends Document {
  moduleId: string;
  companyId: string;
  data: any;
  createdAt: Date;
  updatedAt: Date;
}

const ModuleDataSchema = new Schema<IModuleData>({
  moduleId: { type: String, required: true, index: true },
  companyId: { type: String, required: true },
  data: { type: Schema.Types.Mixed, required: true },
}, {
  timestamps: true,
});

ModuleDataSchema.index({ moduleId: 1, companyId: 1 }, { unique: true });

// Prevent OverwriteModelError during hot reloads and if the model is already registered
const ModuleDataModel = mongoose.models.ModuleData
  ? mongoose.models.ModuleData as mongoose.Model<IModuleData>
  : mongoose.model<IModuleData>('ModuleData', ModuleDataSchema);

export { ModuleDataModel as ModuleData };