const mongoose = require('mongoose');
const { tenantFields, applyPlatformPlugins } = require('../platformFields');

const schema = new mongoose.Schema({
  ...tenantFields,
  formDefinition: { type: mongoose.Schema.ObjectId, ref: 'FormDefinition', required: true, autopopulate: true },
  targetEntity: String,
  payload: mongoose.Schema.Types.Mixed,
  source: { type: String, enum: ['public', 'internal'], default: 'internal' },
  relatedRecordId: mongoose.Schema.ObjectId,
  submittedAt: { type: Date, default: Date.now },
});

module.exports = mongoose.model('FormSubmission', applyPlatformPlugins(schema));
