import mongoose from "mongoose";

const AiwebsitetSchema = new mongoose.Schema(
  {
    submittedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },

    websiteUrl: {
      type: String,
      required: true,
      unique: true,
      match: /^(https?:\/\/)([\\w-]+\.)+[\\w-]+(\/[\\w\-._~:/?#[\]@!$&'()*+,;=%]*)?$/i, // URL validation regex
    },

    thumbnailImage: {
      type: String,
      required: true,
    },

    categories: {
      type: [String],

      required: true,
    },

    type: {
      type: [String],

      required: true,
    },

    pricingOption: {
      type: [String],

      required: true,
    },

    tagline: {
      type: String,
      maxlength: 60,
    },

    description: {
      type: String,
      maxlength: 1500,
      required: true,
    },

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

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

const Aiwebsite = mongoose.model("Aiwebsite", AiwebsitetSchema);
export default Aiwebsite;
