#pragma once
#include "block.h"
#include <cstdint>
#include <cmath>

enum class MobType  : uint8_t { Sheep = 0, Wolf = 1, Human = 2 };
enum class MobState : uint8_t {
    Idle,
    Walk,
    Flee,       // sheep/human: running from wolf
    HuntSheep,  // wolf/human: chasing sheep
    Stalk,      // wolf: 3-second stare at player before charging
    Chase,      // wolf: full-speed charge at player
    Eat,        // sheep: grazing — replaces grass with dirt
    Drink,      // sheep: standing near water
    Chop,       // human: breaking a tree block
    Build,      // human: placing a city block
    Guard,      // human: standing guard at the gate post
    Wander,     // human: wandering inside the castle courtyard
};

struct Mob {
    MobType  type;
    MobState state      = MobState::Idle;
    double   x = 0.0, y = 0.0, z = 0.0;
    float    yaw        = 0.0f;
    float    velGrav    = 0.0f;   // velocity along gravity "up" direction
    float    upX = 0.0f, upY = 1.0f, upZ = 0.0f;  // current gravity up direction
    bool     grounded   = false;
    bool     dead       = false;

    float walkDirX = 0.0f, walkDirY = 0.0f, walkDirZ = 0.0f;
    float aiTimer    = 0.0f;  // general AI re-decision timer
    float actionTimer = 0.0f; // action in progress (eat / chop / build)

    // Sheep timers
    float eatTimer   = 25.0f;
    float drinkTimer = 280.0f;

    // Wolf stalk
    float stalkTimer = 0.0f;

    // Shared target
    int   targetMobIdx = -1;
    float targetX = 0.0f, targetY = 0.0f, targetZ = 0.0f;

    // Human: city building
    int  buildStep  = -1;  // -1 = needs to claim next block
    int  chopBX = 0, chopBY = 0, chopBZ = 0;
    bool hasWood    = false;
    float buildPause = 0.0f;

    // Human: post-construction role
    uint8_t aiRole = 0;            // 0 = worker/wanderer, 1 = gate guard
    float guardPostX = 0.0f;       // world-space guard post (gate centre)
    float guardPostY = 0.0f;
    float guardPostZ = 0.0f;
    float guardPaceTimer = 0.0f;   // timer for pacing left↔right at post
    int   guardPaceDir  = 1;       // +1 / -1

    // Visual
    float animPhase = 0.0f;

    // Combat
    float hp           = 8.0f;  // current HP
    float biteCooldown = 0.0f;  // wolf: seconds until next bite
    float hitFlash     = 0.0f;  // red flash timer (0.3 s when hit)
    float knockVelX    = 0.0f;  // horizontal knockback velocity
    float knockVelZ    = 0.0f;  // horizontal knockback velocity

    uint64_t rngState = 0;
    float rnd() {
        rngState ^= rngState << 13;
        rngState ^= rngState >> 7;
        rngState ^= rngState << 17;
        return static_cast<float>(rngState & 0xFFFF) / 65535.0f;
    }
    float rnd(float lo, float hi) { return lo + rnd() * (hi - lo); }
};
