const mongoose = require('mongoose');

const playerDataSchema = new mongoose.Schema({
  userId: {
    type: String,
    required: true,
    unique: true
  },
  username: {
    type: String,
    required: true
  },
  // Player stats
  stats: {
    level: { type: Number, default: 1 },
    experience: { type: Number, default: 0 },
    totalExperience: { type: Number, default: 0 },
    credits: { type: Number, default: 1000 },
    gems: { type: Number, default: 0 },
    skillPoints: { type: Number, default: 0 },
    totalKills: { type: Number, default: 0 },
    dungeonsCleared: { type: Number, default: 0 },
    questsCompleted: { type: Number, default: 0 },
    playTime: { type: Number, default: 0 },
    lastLogin: { type: Date, default: Date.now }
  },
  // Game systems data
  inventory: {
    items: [{ type: mongoose.Schema.Types.Mixed }],
    maxSize: { type: Number, default: 50 }
  },
  ship: {
    type: mongoose.Schema.Types.Mixed,
    default: null
  },
  base: {
    type: mongoose.Schema.Types.Mixed,
    default: null
  },
  quests: {
    active: [{ type: mongoose.Schema.Types.Mixed }],
    completed: [{ type: String }]
  },
  skills: {
    type: mongoose.Schema.Types.Mixed,
    default: {}
  },
  idleSystem: {
    type: mongoose.Schema.Types.Mixed,
    default: {}
  },
  dungeonSystem: {
    type: mongoose.Schema.Types.Mixed,
    default: {}
  },
  crafting: {
    type: mongoose.Schema.Types.Mixed,
    default: {}
  },
  // Server-specific data
  currentServerId: {
    type: String,
    default: null
  },
  lastServerJoin: {
    type: Date,
    default: null
  },
  totalPlayTime: {
    type: Number,
    default: 0
  }
}, {
  timestamps: true
});

// Indexes for performance
playerDataSchema.index({ username: 1 });
playerDataSchema.index({ 'stats.level': 1 });

// Note: userId field already has unique: true which creates an index automatically

// Methods
playerDataSchema.methods.addExperience = function(amount) {
  this.stats.experience += amount;
  this.stats.playTime += Date.now() - (this.lastLogin || Date.now());
  this.lastLogin = new Date();
  return this.stats.experience;
};

playerDataSchema.methods.addCredits = function(amount) {
  this.stats.credits += amount;
  return this.stats.credits;
};

playerDataSchema.methods.updatePlayTime = function() {
  const sessionTime = Date.now() - (this.lastLogin || Date.now());
  this.stats.playTime += sessionTime;
  this.totalPlayTime += sessionTime;
  this.lastLogin = new Date();
  return sessionTime;
};

playerDataSchema.methods.joinServer = function(serverId) {
  this.currentServerId = serverId;
  this.lastServerJoin = new Date();
  this.lastLogin = new Date();
};

playerDataSchema.methods.leaveServer = function() {
  console.log('[PLAYER DATA] leaveServer called for:', this.username);
  
  // Update play time before leaving
  const sessionTime = Date.now() - (this.lastLogin || Date.now());
  this.stats.playTime += sessionTime;
  this.totalPlayTime += sessionTime;
  this.lastLogin = new Date();
  
  // Clear server association
  this.currentServerId = null;
  this.lastServerJoin = null;
  
  console.log('[PLAYER DATA] Updated play time:', sessionTime, 'Total play time:', this.totalPlayTime);
  
  return sessionTime;
};

module.exports = mongoose.model('PlayerData', playerDataSchema);
