import mongoose from "mongoose";

const postJobSchema = new mongoose.Schema(
  {
    submittedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true,
    },
    title: {
      type: String,
      required: true,
    },
    description: {
      type: String,
      maxlength: 1000,
    },
    skills: {
      type: [String],
      required: true,
    },
    type: {
      type: String,
      required: true,
    },
    category: {
      type: String,
      required: true,
    },
    gender: {
      type: String,
      required: true,
    },
    experience: {
      type: String,
      required: true,
    },
    languages: {
      type: [String],
    },
    // Apply method fields
    applyMethod: {
      type: String,
      required: true,
    },
    applyEmail: {
      type: String,
      match: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
    },
    applyUrl: {
      type: String,
      match: /^(https?:\/\/)([\w-]+\.)+[\w-]+(\/[\w\-._~:/?#[\]@!$&'()*+,;=%]*)?$/i,
    },
    // Company fields
    companyName: {
      type: String,
      required: true,
    },
    companyWebsite: {
      type: String,
      required: true,
      match: /^(https?:\/\/)([\w-]+\.)+[\w-]+(\/[\w\-._~:/?#[\]@!$&'()*+,;=%]*)?$/i,
    },
    companyLogo: {
      type: String,
      required: true,
    },
    country: {
      type: String,
      required: true,
    },
    companyEmail: {
      type: String,
      match: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
    },
    companyContact: {
      type: String,
      required: true,
    },
    hideCompanyEmail: {
      type: Boolean,
      default: false
    },
    hideCompanyContact: {
      type: Boolean,
      default: false
    },

    // 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 PostJob = mongoose.model("PostJob", postJobSchema);
export default PostJob;