import mongoose, { Schema } from "mongoose";

const UserSchema = new Schema(
  {
    profile:{
      type: mongoose.Schema.Types.ObjectId,
      ref: "SettingProfile",

    },
    email: {
      type: String,
      required: true,
      unique: true,
      trim: true,
      lowercase: true,
    },
    userName: {
      type: String,
      required: true,
      trim: true,
    },
    passwordHash: { 
      type: String, 
    }, // passwordHash will be empty for social logins
    socialId: { 
      type: String, 
      unique: true,
      sparse: true, // Allows multiple null values, only enforces uniqueness on non-null
    },
    isSocialLogin: {  // ✅ ADD THIS FIELD
    type: Boolean,
    default: false
  },
    socialProvider: {
      type: String, // 'google', 'linkedin', 'facebook'
      enum: ['google', 'linkedin', 'facebook', null],
      default: null,
    },
    socialEmail: {
      type: String, // Actual email from social provider (if available)
      trim: true,
      lowercase: true,
    },
    photoUrl: { 
      type: String, 
    },
    // Email verification fields
    isVerified: {
      type: Boolean,
      default: false,
    },
    verificationToken: String,
    verificationTokenExpire: Date,
    // Reset password fields
    resetPasswordToken: String,
    resetPasswordExpire: Date,
    // Coupon usage tracking
    usedCoupons: [{
      couponId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Coupon'
      },
      code: String,
      usedAt: {
        type: Date,
        default: Date.now
      },
      serviceType: String, // Which service the coupon was used for
      discountAmount: Number
    }],
  },
  {
    timestamps: true,
  }
);

export const User = mongoose.model("User", UserSchema);
