import mongoose from "mongoose";

// Define URL regex at the top
const urlRegex = /^(https?:\/\/)([\w-]+\.)+[\w-]+(\/[\w\-._~:/?#[\]@!$&'()*+,;=%]*)?$/i;

const submitDirectorySchema = new mongoose.Schema(
  {
    submittedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true,
    },
    websiteUrl: {
      type: String,
      required: true,
      unique: true,
      match: urlRegex,
    },
    screenshot: {
      type: String,
      required: true,
    },
    title: {
      type: String,
      required: true,
    },
    description: {
      type: String,
      maxlength: 1000,
    },
    companyLogo: {
      type: String,
      required: true,
    },
    country: {
      type: String,
      required: true,
    },
    contactEmail: {
      type: String,
      required: true,
      match: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
    },
    contactNumber: {
      type: String,
      required: true,
    },
    profile: {
      type: String,
      required: true,
    },
    openTo: {
      type: String,
      required: true,
    },
    foundingYear: {
      type: Number,
      required: true,
    },
    totalEmployees: {
      type: String,
      required: true,
    },
    budget: {
      type: String,
      required: true,
    },
    languages: {
      type: [String],
    },
    facebook: {
      type: String,
      validate: { validator: (v) => !v || urlRegex.test(v), message: "Invalid URL." },
    },
    instagram: {
      type: String,
      validate: { validator: (v) => !v || urlRegex.test(v), message: "Invalid URL." },
    },
    twitter: {
      type: String,
      validate: { validator: (v) => !v || urlRegex.test(v), message: "Invalid URL." },
    },
    linkedin: {
      type: String,
      validate: { validator: (v) => !v || urlRegex.test(v), message: "Invalid URL." },
    },
    tiktok: {
      type: String,
      validate: { validator: (v) => !v || urlRegex.test(v), message: "Invalid URL." },
    },
    youtube: {
      type: String,
      validate: { validator: (v) => !v || urlRegex.test(v), message: "Invalid URL." },
    },

    // Submission status - for two-phase payment flow
    status: {
      type: String,
      enum: ['pending_payment', 'published', 'payment_failed'],
      default: 'pending_payment',
      required: true,
    },

    // Payment information
    paymentOrderID: {
      type: String,
      unique: true,
      sparse: true, // Allows multiple null values but unique non-null values
    },
    paymentStatus: {
      type: String,
      enum: ['COMPLETED', 'APPROVED', 'PENDING', 'FREE_COUPON'],
    },
    paymentAmount: {
      type: Number,
    },
    paymentCurrency: {
      type: String,
      default: 'USD',
    },
    payerEmail: {
      type: String,
    },
    payerName: {
      type: String,
    },
    paymentDate: {
      type: Date,
    },
    paymentVerified: {
      type: Boolean,
      default: false,
    },
    // Coupon fields for free submissions
    couponCode: {
      type: String,
      required: false,
    },
    isFreeSubmission: {
      type: Boolean,
      default: false,
    },
  },
  { timestamps: true }
);

const SubmitDirectory = mongoose.models.SubmitDirectory || mongoose.model("SubmitDirectory", submitDirectorySchema);
export default SubmitDirectory;