import mongoose from "mongoose";
const websiteSchema = new mongoose.Schema(
  {
    submittedBy: {
      type: mongoose.Schema.Types.ObjectId, // ObjectId that refers to User model
      ref: "User", // Reference to the User model
      required: true,
    },
    Votes: [
      {
        type: mongoose.Schema.Types.ObjectId, // ObjectId that refers to vote model
        ref: "Vote", // Reference to the vote model
      },
    ],
    Submitted_URL: {
      type: String,
      required: true,
      unique: true,
    },
    Img: { type: String, required: true },

    Title: { type: String, required: true },
    Description: { type: String, required: true },

    Category: { type: String, required: true },
    Technology: { type: [String], default: [], required: false }, // Array for multiple technologies
    Tags: { type: [String], default: [], required: true },
    Colors: { type: [String], default: [], required: true },

    ProfileType: { type: String, required: true },
    Freelancer_Name: { type: String, required: true },

    URL: { type: String, required: true },
    Country: { type: String, required: true },

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

    // Payment tracking fields - now optional for pending submissions
    paymentOrderID: {
      type: String,
      required: false, // Not required until payment is confirmed
      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 } // adds createdAt (submit time) & updatedAt
);

// ✅ Add indexes for better query performance
websiteSchema.index({ createdAt: -1 }); // For sorting by date
websiteSchema.index({ Category: 1 }); // For filtering by category
websiteSchema.index({ Technology: 1 }); // For filtering by technology
websiteSchema.index({ Country: 1 }); // For filtering by country
websiteSchema.index({ ProfileType: 1 }); // For filtering by profile type
websiteSchema.index({ Tags: 1 }); // For filtering by tags
websiteSchema.index({ Colors: 1 }); // For filtering by colors
// paymentOrderID already has unique: true in schema, no need for additional index
websiteSchema.index({ submittedBy: 1 }); // For user-specific queries
websiteSchema.index({ status: 1 }); // For filtering by submission status



//const WebsiteSubmission = mongoose.model("WebsiteSubmission", websiteSchema);
const WebsiteSubmission =
  mongoose.models.WebsiteSubmission ||
  mongoose.model("WebsiteSubmission", websiteSchema);
export default WebsiteSubmission;
