/* harakiri_spider_mouse.h — Spider/Mouse: dual-mode cognition loop
 *
 * Two speeds of thought, after OhioKernel's design:
 *
 *   SPIDER — slow, deliberative. Periodically surveys agent activity and
 *   system load, then proposes the mode the system SHOULD be in. Web-weaving:
 *   sees the whole picture before moving.
 *
 *   MOUSE — fast, reactive. Fires on discrete events (game launched, call
 *   incoming, screen off) and switches mode NOW. Darting: no deliberation,
 *   just the right reflex.
 *
 * Hysteresis guards both: no mode flaps faster than HK_MIN_MODE_TICKS,
 * and Spider proposals need confidence >= HK_SPIDER_CONFIDENCE.
 */
#ifndef _HARAKIRI_SPIDER_MOUSE_H
#define _HARAKIRI_SPIDER_MOUSE_H

#include <linux/types.h>
#include <harakiri/harakiri_agent.h>
#include <harakiri/harakiri_brood.h>

/* Cognition events for the Mouse fast path */
enum hk_event {
	HK_EV_GAME_LAUNCH = 0,	/* a game process started */
	HK_EV_GAME_EXIT,	/* last game process ended */
	HK_EV_CALL_IN,		/* incoming call */
	HK_EV_CALL_END,
	HK_EV_SCREEN_OFF,
	HK_EV_SCREEN_ON,
	HK_EV_DOCK,		/* docked / desktop posture */
	HK_EV_UNDOCK,
	HK_NEVENTS
};

/* Ticks (cognition cycles) before another mode switch is allowed */
#define HK_MIN_MODE_TICKS	10
/* Spider proposal confidence threshold (x1000) */
#define HK_SPIDER_CONFIDENCE	600

struct hk_cognition {
	u8	mode;		/* enum hk_mode: current */
	u8	proposed;	/* enum hk_mode: spider's proposal */
	u8	manual;		/* 1 = mode set explicitly, spider must not override */
	u16	ticks_since_switch;
	u32	spider_cycle;	/* increments each slow evaluation */
	u32	eta_x1000;	/* running confidence */
	/* Activity counters per role, decayed by the spider */
	u32	activity[HK_NROLES];
};

static inline void hk_cog_init(struct hk_cognition *c)
{
	c->mode = HK_MODE_PHONE;
	c->proposed = HK_MODE_PHONE;
	c->manual = 0;
	c->ticks_since_switch = HK_MIN_MODE_TICKS; /* allow immediate first switch */
	c->spider_cycle = 0;
	c->eta_x1000 = 1000;
	{
		int i;
		for (i = 0; i < HK_NROLES; i++)
			c->activity[i] = 0;
	}
}

/*
 * MOUSE — fast path. Called on a discrete event. Returns true if the
 * mode actually changed. Hysteresis applies: too soon after the last
 * switch, the event is noted but ignored.
 */
static inline bool hk_mouse_trigger(struct hk_cognition *c, u8 event)
{
	u8 want = c->mode;

	switch (event) {
	case HK_EV_GAME_LAUNCH:
		want = HK_MODE_GAMING;
		break;
	case HK_EV_GAME_EXIT:
		want = HK_MODE_PHONE;
		break;
	case HK_EV_CALL_IN:
		/* calls punch through gaming mode but don't change it */
		return false;
	case HK_EV_DOCK:
		want = HK_MODE_DESKTOP;
		break;
	case HK_EV_UNDOCK:
	case HK_EV_SCREEN_OFF:
		want = HK_MODE_PHONE;
		break;
	default:
		return false;
	}

	if (want == c->mode)
		return false;
	if (c->ticks_since_switch < HK_MIN_MODE_TICKS)
		return false; /* too soon — hold the line */

	c->mode = want;
	c->ticks_since_switch = 0;
	return true;
}

/*
 * SPIDER — slow path. Feed it per-role activity counts observed since
 * the last call; it decays old data, folds in the new, and proposes a
 * mode. The proposal only becomes the mode when confidence is high.
 * Call once per cognition tick.
 */
static inline void hk_spider_eval(struct hk_cognition *c,
				  const u32 *new_activity /* [HK_NROLES] */)
{
	int i;
	u32 brood_act, total = 0;

	c->spider_cycle++;
	if (c->ticks_since_switch < 0xFFFFu)
		c->ticks_since_switch++;

	/* Decay old activity by 1/2, add new observations */
	for (i = 0; i < HK_NROLES; i++) {
		c->activity[i] = (c->activity[i] >> 1) + new_activity[i];
		total += c->activity[i];
	}

	if (total == 0) {
		c->proposed = HK_MODE_PHONE;
		c->eta_x1000 = 1000;
		return;
	}

	brood_act = c->activity[HK_BROOD];

	/* Heuristic: sustained Brood dominance → gaming; Merlin/Hive → desktop */
	if (brood_act * 2 > total) {
		c->proposed = HK_MODE_GAMING;
		c->eta_x1000 = (brood_act * 1000) / total;
	} else if ((c->activity[HK_MERLIN] + c->activity[HK_HIVE]) * 2 > total) {
		c->proposed = HK_MODE_DESKTOP;
		c->eta_x1000 = ((c->activity[HK_MERLIN] + c->activity[HK_HIVE]) * 1000) / total;
	} else {
		c->proposed = HK_MODE_PHONE;
		c->eta_x1000 = 1000 - (brood_act * 1000) / (total + 1);
	}
}

/*
 * Commit the Spider's proposal if confident and hysteresis allows.
 * Returns true on a mode change.
 */
static inline bool hk_spider_commit(struct hk_cognition *c)
{
	if (c->manual)
		return false; /* explicit mode set — spider stands down */
	if (c->proposed == c->mode)
		return false;
	if (c->eta_x1000 < HK_SPIDER_CONFIDENCE)
		return false;
	if (c->ticks_since_switch < HK_MIN_MODE_TICKS)
		return false;
	c->mode = c->proposed;
	c->ticks_since_switch = 0;
	return true;
}

#endif /* _HARAKIRI_SPIDER_MOUSE_H */
