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

const fieldSchema = new mongoose.Schema({
  key: { type: String, required: true },
  label: { type: String, required: true },
  type: { type: String, enum: ['text', 'textarea', 'number', 'select', 'checkbox', 'date', 'email', 'phone'], default: 'text' },
  required: { type: Boolean, default: false },
  options: [String],
  placeholder: String,
}, { _id: false });

const schema = new mongoose.Schema({
  ...tenantFields,
  name: { type: String, required: true },
  slug: { type: String, trim: true, lowercase: true },
  type: { type: String, enum: ['public', 'internal'], default: 'internal' },
  targetEntity: { type: String, required: true },
  description: String,
  fields: [fieldSchema],
  successMessage: String,
  autoReplyEnabled: { type: Boolean, default: false },
  autoReplySubject: String,
  autoReplyBody: String,
  status: { type: String, enum: ['draft', 'published', 'archived'], default: 'draft' },
});

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