import mongoose from "mongoose";

const UserSchema = new mongoose.Schema(
  {
    fullname: {
      type: String,
      required: function () {
        return this.role !== "admin";
      },
      unique: true,
      trim: true,
    },
    email: {
      type: String,
      required: true,
      unique: true,
      validate: {
        validator: function (value) {
          if (this.role === "admin") return true;
          return /.+\@.+\..+/.test(value);
        },
        message: "Please use a valid email",
      },
    },
    password: {
      type: String,
      required: true,
      select: false,
    },
    gender: {
      type: String,
      enum: ["male", "female"],
      required: function () {
        // return this.role === 'patient' || this.role === 'doctor';
        return this.role === "patient";
      },
    },
    birthdate: {
      type: Date,
      required: function () {
        // return this.role === 'patient' || this.role === 'doctor';
        return this.role === "patient";
      },
    },
    role: {
      type: String,
      enum: ["patient", "doctor", "admin"],
      default: "patient",
      required: true,
    },
    profilePictureUrl: {
      type: String,
      default: "",
      ref: "ProfilePicture",
    },
    startDateOfPractice: {
      type: Date,
      required: function () {
        return this.role === "doctor";
      },
    },
    graduationUniversity: {
      type: String,
      required: function () {
        return this.role === "doctor";
      },
    },
    specialization: {
      type: String,
      required: function () {
        return this.role === "doctor";
      },
    },

    // authProvider: { type: String, enum: ["email", "google"], default: "email" },
    // googleId: { type: String, unique: true, sparse: true },
    // emailVerified: { type: Boolean, default: false },
  },
  {
    timestamps: true,
    // toJSON: {
    //   virtuals: true,
    //   transform: function (doc, ret) {
    //     delete ret.password; // Never return password in JSON responses
    //     delete ret.__v;
    //     return ret;
    //   },
    // },
  },
);

export default mongoose.model("User", UserSchema);
