import mongoose, { Schema, Document } from 'mongoose';

export interface IUser extends Document {
  telegramId: number;
  username: string;
  level: number;
  experience: number;
  balance: number;
  achievements: Array<{
    id: string;
    name: string;
    description: string;
    unlockedAt: Date;
  }>;
  inventory: Array<{
    itemId: string;
    quantity: number;
  }>;
  createdAt: Date;
  updatedAt: Date;
}

const UserSchema: Schema = new Schema({
  telegramId: {
    type: Number,
    required: true,
    unique: true,
  },
  username: {
    type: String,
    required: true,
  },
  level: {
    type: Number,
    default: 1,
  },
  experience: {
    type: Number,
    default: 0,
  },
  balance: {
    type: Number,
    default: 0,
  },
  achievements: [{
    id: String,
    name: String,
    description: String,
    unlockedAt: {
      type: Date,
      default: Date.now,
    },
  }],
  inventory: [{
    itemId: String,
    quantity: Number,
  }],
}, {
  timestamps: true,
});

// Виртуальное поле для расчета следующего уровня
UserSchema.virtual('nextLevelExp').get(function() {
  return Math.floor(100 * Math.pow(1.5, this.level - 1));
});

export default mongoose.models.User || mongoose.model<IUser>('User', UserSchema); 