class Player extends Phaser.GameObjects.Container {
  constructor(scene, x, y) {
    super(scene, x, y);
    
    // Create the player sprite
    this.sprite = scene.add.sprite(0, 0, 'player');
    this.add(this.sprite);
    
    // Set up input controls using A/D keys instead of arrow keys
    this.keys = scene.input.keyboard.addKeys({
      a: Phaser.Input.Keyboard.KeyCodes.A,
      d: Phaser.Input.Keyboard.KeyCodes.D
    });
    
    // Player properties
    this.speed = 200;
    this.health = 100;
    this.isAlive = true;
    
    // Set initial position
    this.setPosition(x, y);
  }

  update() {
    if (!this.isAlive) return;

    // Move with A/D keys instead of arrow keys
    if (this.keys.a.isDown) {
      this.x -= this.speed * this.scene.game.loop.delta / 1000;
    }
    if (this.keys.d.isDown) {
      this.x += this.speed * this.scene.game.loop.delta / 1000;
    }

    // Keep player within bounds
    this.x = Phaser.Math.Clamp(this.x, 25, 775);
    
    // Update position of the container's child sprite
    this.sprite.setPosition(0, 0);
  }

  aimAt(targetX, targetY) {
    // Not used in current implementation but kept for potential future use
  }

  fire() {
    if (!this.isAlive) return;
    
    // Fire bullet from player position
    const bullet = new Bullet(this.scene, this.x, this.y - 20);
    this.scene.bullets.add(bullet);
    
    // Set bullet velocity
    bullet.setVelocityY(-300);
  }

  hit(damage) {
    if (!this.isAlive) return;
    
    this.health -= damage;
    
    if (this.health <= 0) {
      this.destroy();
    }
  }
}