const mongoose = require("mongoose");

/**
 * Schema for Contact Form Submissions
 * Stores user inquiries from the contact form
 */
const contactSubmissionSchema = new mongoose.Schema(
    {
        name: {
            type: String,
            required: [true, "Name is required"],
            trim: true,
            maxlength: [100, "Name cannot exceed 100 characters"],
        },
        email: {
            type: String,
            required: [true, "Email is required"],
            trim: true,
            lowercase: true,
            match: [/^\S+@\S+\.\S+$/, "Please enter a valid email"],
        },
        phone: {
            type: String,
            trim: true,
            default: "",
        },
        address: {
            type: String,
            trim: true,
            default: "",
        },
        date: {
            type: String,
            trim: true,
            default: "",
        },
        message: {
            type: String,
            trim: true,
            default: "",
        },
        status: {
            type: String,
            enum: ["pending", "read", "replied", "archived"],
            default: "pending",
        },
        notes: {
            type: String,
            trim: true,
            default: "",
        },
        repliedAt: {
            type: Date,
            default: null,
        },
        ipAddress: {
            type: String,
            default: "",
        },
        userAgent: {
            type: String,
            default: "",
        },
    },
    {
        timestamps: true,
    }
);

// Index for faster queries
contactSubmissionSchema.index({ status: 1, createdAt: -1 });
contactSubmissionSchema.index({ email: 1 });

module.exports = mongoose.model("ContactSubmission", contactSubmissionSchema);
