package org.project.entity.enemies;

import org.project.entity.Entity;
import org.project.util.ConsoleColors;

/**
 * Abstract base for every monster, including the Dragon boss.
 *
 * It holds the stats and rewards shared by all enemies and provides a plain
 * default attack. Each subclass overrides {@link #attack(Entity)} (and sometimes
 * {@link #takeDamage(int)}) to add its signature ability — a crit, a
 * resurrection, lifesteal, or armor-piercing fire.
 */
public abstract class Enemy implements Entity {

    protected String name;
    private int hp;
    private final int maxHP;
    protected int damage;

    private final int xpReward;
    private final int coinReward;
    private final KeyType keyType;      // null for the Dragon (it drops no key)
    private final int keyDropChance;    // percent chance to drop its key

    private boolean stunned = false;

    protected Enemy(String name, int maxHP, int damage,
                    int xpReward, int coinReward, KeyType keyType, int keyDropChance) {
        this.name = name;
        this.maxHP = maxHP;
        this.hp = maxHP;
        this.damage = damage;
        this.xpReward = xpReward;
        this.coinReward = coinReward;
        this.keyType = keyType;
        this.keyDropChance = keyDropChance;
    }

    /** Default attack: hit the target for this enemy's damage. */
    @Override
    public void attack(Entity target) {
        System.out.println(ConsoleColors.red("👾 " + name + " attacks!"));
        target.takeDamage(damage);
    }

    @Override
    public void takeDamage(int damage) {
        hp -= damage;
        if (hp < 0) {
            hp = 0;
        }
        // Note: the attacker prints the damage/HP lines, so this stays quiet.
    }

    /** Enemies don't have a defensive stance in this game. */
    @Override
    public void defend() {
        // no-op
    }

    @Override
    public void heal(int health) {
        hp += health;
        if (hp > maxHP) {
            hp = maxHP;
        }
    }

    /** Enemies use no mana, so this does nothing. */
    @Override
    public void fillMana(int mana) {
        // no-op
    }

    // --- Stun handling (used by the Knight's Shield Bash) ---

    public void stun() {
        stunned = true;
    }

    public boolean isStunned() {
        return stunned;
    }

    public void clearStun() {
        stunned = false;
    }

    // --- Getters ---

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getHp() {
        return hp;
    }

    @Override
    public int getMaxHP() {
        return maxHP;
    }

    @Override
    public int getMp() {
        return 0;
    }

    @Override
    public int getMaxMP() {
        return 0;
    }

    @Override
    public boolean isAlive() {
        return hp > 0;
    }

    public int getXpReward() {
        return xpReward;
    }

    public int getCoinReward() {
        return coinReward;
    }

    public KeyType getKeyType() {
        return keyType;
    }

    public int getKeyDropChance() {
        return keyDropChance;
    }
}
