const mongoose = require('mongoose')
const timestamp = require('mongoose-timestamp')
const mongoosePaginate = require('mongoose-paginate-v2')
const projectSchema = new mongoose.Schema({
  title: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  expertise: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  sub_expertise: {
    type: [String], // تغییر نوع به آرایه از استرینگ
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  gender: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  age: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  conversation_projects: {
    type: Boolean,
    required: false,
    default: null
  },
  province: {
    type: Object,
    default: null
  },
  city: {
    type: Object,
    default: null
  },
  offer_time: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  offer_price: {
    type: Number,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  description: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 1024,
    default: null
  },
  project_type: {
    type: String,
    enum: ['free', 'normal', 'force', 'highlight'],
    default: 'normal'
  },
  payment_status: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  // فیلد برای وضعیت پروژه
  status: {
    type: String,
    enum: ['pre_payment', 'paid', 'accepted', 'rejected', 'done', 'cancled', 'ongoing'],
    default: 'pre_payment'
  },
  public_status: {
    type: String,
    enum: ['public', 'private'],
    default: 'public'
  },
  created_for_user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    default: null
  },
  creator_id: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  },
  requested_users: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }],
  selected_user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    default: null
  },
  final_price: {
    type: Number,
    required: false,
    default: null
  },
  final_time: {
    type: Number,
    required: false,
    default: null
  },
  installments: [{
    installment_number: {
      type: Number,
      required: true
    },
    amount: {
      type: Number,
      required: true
    },
    due_date: {
      type: Date,
      required: true
    }
  }],
  reject_reason: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 1024,
    default: null
  },
  ratings: [{
    creator_id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true
    },
    rating: {
      type: Number,
      required: true
    },
    comment: {
      type: String,
      trim: true
    }
  }],
  acceptedAt: {
    type: Date,
    default: null
  },
  isExpired: {
    type: Boolean,
    default: false
  }

})

projectSchema.plugin(timestamp)
// اضافه کردن پیجینیشن به مدل
projectSchema.plugin(mongoosePaginate)
const ProjectModel = mongoose.model('Project', projectSchema)
module.exports = ProjectModel
