const mongoose = require('mongoose');

const shipSchema = new mongoose.Schema({
  userId: {
    type: String,
    required: true,
    ref: 'Player'
  },
  
  // Ship identification
  id: {
    type: String,
    required: true,
    unique: true
  },
  name: {
    type: String,
    required: true
  },
  class: {
    type: String,
    required: true,
    enum: ['Fighter', 'Cruiser', 'Battleship', 'Carrier', 'Explorer']
  },
  level: {
    type: Number,
    default: 1
  },
  
  // Ship stats
  stats: {
    health: { type: Number, required: true },
    maxHealth: { type: Number, required: true },
    attack: { type: Number, required: true },
    defense: { type: Number, required: true },
    speed: { type: Number, required: true },
    criticalChance: { type: Number, default: 0.05 },
    criticalDamage: { type: Number, default: 1.5 },
    hull: { type: Number, required: true }
  },
  
  // Ship appearance
  texture: {
    type: String,
    required: true
  },
  
  // Ship progression
  experience: {
    type: Number,
    default: 0
  },
  requiredExp: {
    type: Number,
    default: 100
  },
  upgrades: [{
    type: String
  }],
  
  // Ship status
  isEquipped: {
    type: Boolean,
    default: false
  },
  isCurrent: {
    type: Boolean,
    default: false
  },
  
  // Shop information (if purchased)
  price: {
    type: Number,
    default: 0
  },
  rarity: {
    type: String,
    enum: ['common', 'uncommon', 'rare', 'epic', 'legendary'],
    default: 'common'
  },
  description: {
    type: String,
    default: ''
  },
  
  // Timestamps
  acquiredAt: {
    type: Date,
    default: Date.now
  },
  lastUsed: {
    type: Date,
    default: Date.now
  }
}, {
  timestamps: true
});

// Indexes for performance
shipSchema.index({ userId: 1 });
shipSchema.index({ id: 1 });
shipSchema.index({ isEquipped: 1 });
shipSchema.index({ isCurrent: 1 });

// Methods
shipSchema.methods.addExperience = function(amount) {
  this.experience += amount;
  
  // Level up logic
  while (this.experience >= this.requiredExp) {
    this.experience -= this.requiredExp;
    this.level += 1;
    this.requiredExp = this.level * 100;
    
    // Increase stats on level up
    this.stats.maxHealth += 10;
    this.stats.health = this.stats.maxHealth;
    this.stats.attack += 2;
    this.stats.defense += 1;
    this.stats.speed += 1;
  }
  
  return this.level;
};

shipSchema.methods.takeDamage = function(damage) {
  const actualDamage = Math.max(0, damage - this.stats.defense);
  this.stats.health = Math.max(0, this.stats.health - actualDamage);
  
  if (this.stats.health === 0) {
    this.isDestroyed = true;
  }
  
  return actualDamage;
};

shipSchema.methods.heal = function(amount) {
  const healAmount = Math.min(amount, this.stats.maxHealth - this.stats.health);
  this.stats.health += healAmount;
  this.isDestroyed = false;
  
  return healAmount;
};

shipSchema.methods.isAlive = function() {
  return this.stats.health > 0;
};

shipSchema.methods.getStatSummary = function() {
  return {
    name: this.name,
    class: this.class,
    level: this.level,
    health: `${this.stats.health}/${this.stats.maxHealth}`,
    attack: this.stats.attack,
    defense: this.stats.defense,
    speed: this.stats.speed,
    criticalChance: `${(this.stats.criticalChance * 100).toFixed(1)}%`,
    criticalDamage: `${this.stats.criticalDamage}x`
  };
};

shipSchema.methods.upgrade = function(upgradeType) {
  switch (upgradeType) {
    case 'health':
      this.stats.maxHealth += 20;
      this.stats.health = this.stats.maxHealth;
      break;
    case 'attack':
      this.stats.attack += 5;
      break;
    case 'defense':
      this.stats.defense += 3;
      break;
    case 'speed':
      this.stats.speed += 2;
      break;
    default:
      throw new Error('Unknown upgrade type');
  }
  
  if (!this.upgrades.includes(upgradeType)) {
    this.upgrades.push(upgradeType);
  }
  
  this.lastUsed = new Date();
};

module.exports = mongoose.model('Ship', shipSchema);
