import mongoose from 'mongoose';
const { Schema } = mongoose;

const systemSettingSchema = new Schema({
  _id: {
    type: String,
    default: 'system' // 固定 ID
  },
  innerDefaultAmount: {
    type: Number,
    required: true,
    default: 10000
  },
  // minProfitAmount: {
  //   type: Number,
  //   required: true,
  //   default: 0
  // },
  minShowAmount: {
    type: Number,
    required: true,
    default: 0
  },
  innerRebateRatio: {
    type: Number,
    required: true,
    default: 0
  },
  innerOuMinValue: {
    type: Number,
    required: true,
    default: 0,
  },
  obRebateRatio: {
    type: Number,
    required: true,
    default: 0
  },
  obRebateType: {
    type: Number,
    required: true,
    default: 0
  },
  obMaxDiff: {
    type: Number,
    required: true,
    default: 0
  },
  imRebateRatio: {
    type: Number,
    required: true,
    default: 0
  },
  imRebateType: {
    type: Number,
    required: true,
    default: 0
  },
  imMaxDiff: {
    type: Number,
    required: true,
    default: 0
  },
  hgRebateRatio: {
    type: Number,
    required: true,
    default: 0
  },
  hgRebateType: {
    type: Number,
    required: true,
    default: 0
  },
  hgRebateLower: {
    type: Number,
    required: true,
    default: 0
  },
  hgMaxDiff: {
    type: Number,
    required: true,
    default: 0
  },
  pcRebateRatio: {
    type: Number,
    required: true,
    default: 0
  },
  pcRebateType: {
    type: Number,
    required: true,
    default: 0
  },
  innerRouSubsidyValue: {
    type: Number,
    required: true,
    default: 0
  },
  // innerRouMaxAdjust: {
  //   type: Number,
  //   required: true,
  //   default: 100
  // },
  innerReMaxAdjust: {
    type: Number,
    required: true,
    default: 100
  },
  innerOuMaxAdjust: {
    type: Number,
    required: true,
    default: 100
  },
  innerWmSubsidyRatio: {
    type: Number,
    required: true,
    default: 0
  },
  innerOtSubsidyRatio: {
    type: Number,
    required: true,
    default: 0
  },
  halfTimeActiveTime: {
    type: Number,
    required: true,
    default: 0
  },
  expireTimeEvents: {
    type: Number,
    required: true,
    default: 45000
  },
  expireTimeSpecial: {
    type: Number,
    required: true,
    default: 60000
  },
  syncSettingEnabled: {
    type: Boolean,
    required: true,
    default: false
  },
  runWorkerEnabled: {
    type: Boolean,
    required: true,
    default: false
  },
}, {
  toJSON: {
    transform(doc, ret) {
      delete ret._id;
      delete ret.__v;
    }
  },
  toObject: {
    transform(doc, ret) {
      delete ret._id;
      delete ret.__v;
    }
  }
});

const Setting = mongoose.model('SystemSetting', systemSettingSchema);
const CALLBACKS = {
  update: null,
};

const onUpdate = (callback) => {
  CALLBACKS.update = callback;
}

const get = async () => {
  return await Setting.findById('system');
}

const update = async (fields) => {
  CALLBACKS.update?.(fields);
  return await Setting.findByIdAndUpdate(
    'system',
    { $set: fields },
    { upsert: true, new: true }
  );
}

const init = async (fields = {}) => {
  const systemSetting = await Setting.findById('system');
  if (systemSetting) {
    return;
  }
  const setting = new Setting({
    _id: 'system',
    ...fields,
  });
  return setting.save();
}

export { get, update, init, onUpdate };
export default { get, update, init, onUpdate };
