/**
 * Landing Page Lead Model
 *
 * A form submission captured from a published landing page (when lead-capture mode is
 * 'app'). Keeps leads first-party so they can flow into Contacts / Brevo automation.
 * Entirely additive — new collection.
 */

import mongoose, { Schema, Document } from 'mongoose';

export interface ILandingPageLead extends Document {
  companyId: string;
  landingPageId: string;
  name?: string;
  email?: string;
  phone?: string;
  data: Record<string, any>; // all submitted fields
  sourceUrl?: string;
  createdAt: Date;
  updatedAt: Date;
}

const LandingPageLeadSchema = new Schema<ILandingPageLead>({
  companyId: { type: String, required: true, index: true },
  landingPageId: { type: String, required: true, index: true },
  name: { type: String },
  email: { type: String },
  phone: { type: String },
  data: { type: Schema.Types.Mixed, default: {} },
  sourceUrl: { type: String },
}, {
  timestamps: true,
});

LandingPageLeadSchema.index({ companyId: 1, landingPageId: 1, createdAt: -1 });

export const LandingPageLead =
  mongoose.models.LandingPageLead || mongoose.model<ILandingPageLead>('LandingPageLead', LandingPageLeadSchema);
