const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');

const userSchema = new mongoose.Schema({
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
});

userSchema.pre('save', async function(next) {
  if (this.isModified('password')) {
    this.password = await bcrypt.hash(this.password, 10);
  }
  next();
});

userSchema.methods.comparePassword = function(candidatePassword) {
  return bcrypt.compare(candidatePassword, this.password);
};

const User = mongoose.model('User', userSchema);

const projectSchema = new mongoose.Schema({
  title: String,
  description: String,
  images: [String],
});

const educationSchema = new mongoose.Schema({
  degree: String,
  institution: String,
  startYear: Number,
  endYear: Number,
});

const jobExperienceSchema = new mongoose.Schema({
  jobTitle: String,
  company: String,
  startYear: Number,
  endYear: Number,
});

const resumeSchema = new mongoose.Schema({
  pdfURL: String,
});

const socialSchema = new mongoose.Schema({
  platform: String,
  url: String,
});

const Project = mongoose.model('Project', projectSchema);
const Education = mongoose.model('Education', educationSchema);
const JobExperience = mongoose.model('JobExperience', jobExperienceSchema);
const Resume = mongoose.model('Resume', resumeSchema);
const Social = mongoose.model('Social', socialSchema);

module.exports = { User, Project, Education, JobExperience, Resume, Social };
