/* harakiri_core.c — Harakiri agent-native kernel: core module
 *
 * Bootstraps the HiveMind at module load: registers the four core
 * agents (Vera, Mira, Merlin, Gaven), starts the coordination tick,
 * and proves the subsystem stack works inside the running kernel.
 *
 * v1: loadable module for fast iteration. Becomes built-in once the
 * scheduler hooks land.
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/jiffies.h>

#include <harakiri/harakiri_agent.h>
#include <harakiri/harakiri_minnoe.h>
#include <harakiri/harakiri_brood.h>
#include <harakiri/harakiri_spider_mouse.h>
#include <harakiri/harakiri_hivemind.h>

/* scheduler daemon (harakiri_sched.c) */
int hk_sched_start(void);
void hk_sched_stop(void);

/* proc interface (harakiri_proc.c) */
int hk_proc_init(void);
void hk_proc_exit(void);

/* SI Core (harakiri_sicore.c) */
struct proc_dir_entry;
int sicore_init(void);
void sicore_exit(void);
int sicore_proc_init(struct proc_dir_entry *parent);
void sicore_proc_exit(void);

#define HK_TICK_MS	1000	/* coordination tick: 1 Hz for v1 */

struct hk_hivemind hk_hive;
EXPORT_SYMBOL(hk_hive);
static struct timer_list hk_tick_timer;
static struct hk_minnoe_ring *vera_ring;
static struct hk_minnoe_ring *mira_ring;
static struct hk_minnoe_ring *merlin_ring;
static struct hk_minnoe_ring *gaven_ring;

#define HK_NBROOD_WORKERS	4
static struct hk_minnoe_ring *brood_rings[HK_NBROOD_WORKERS];

static void hk_tick_fn(struct timer_list *t)
{
	bool changed;

	changed = hk_hive_tick(&hk_hive);
	if (changed)
		pr_info("harakiri: mode -> %u (spider committed)\n",
			hk_hive.cog.mode);

	mod_timer(&hk_tick_timer, jiffies + msecs_to_jiffies(HK_TICK_MS));
}

static int __init harakiri_init(void)
{
	u32 id;

	pr_info("harakiri: agent-native core loading\n");

	hk_hive_init(&hk_hive);

	/* Register the core crew */
	id = hk_hive_register(&hk_hive, HK_VERA, "vera", &vera_ring);
	pr_info("harakiri: vera registered as agent %u\n", id);
	id = hk_hive_register(&hk_hive, HK_MIRA, "mira", &mira_ring);
	pr_info("harakiri: mira registered as agent %u\n", id);
	id = hk_hive_register(&hk_hive, HK_MERLIN, "merlin", &merlin_ring);
	pr_info("harakiri: merlin registered as agent %u\n", id);
	id = hk_hive_register(&hk_hive, HK_GAVEN, "gaven", &gaven_ring);
	pr_info("harakiri: gaven registered as agent %u\n", id);

	/* Register Brood workers */
	{
		int i;
		char name[16];
		for (i = 0; i < HK_NBROOD_WORKERS; i++) {
			snprintf(name, sizeof(name), "brood-%02d", i);
			id = hk_hive_register(&hk_hive, HK_BROOD, name,
					      &brood_rings[i]);
			pr_info("harakiri: %s registered as agent %u\n",
				name, id);
		}
	}

	/* Register PainTr */
	{
		struct hk_minnoe_ring *paintr_ring;
		id = hk_hive_register(&hk_hive, HK_PAINTR, "paintr", &paintr_ring);
		pr_info("harakiri: paintr registered as agent %u\n", id);
	}

	/* Start the 1 Hz coordination tick */
	timer_setup(&hk_tick_timer, hk_tick_fn, 0);
	mod_timer(&hk_tick_timer, jiffies + msecs_to_jiffies(HK_TICK_MS));

	/* Start the scheduler daemon */
	if (hk_sched_start())
		pr_warn("harakiri: scheduler daemon failed to start\n");

	/* Userspace control interface */
	if (hk_proc_init())
		pr_warn("harakiri: proc interface failed\n");

	/* SI Core — supervisory intelligence */
	if (sicore_init())
		pr_warn("harakiri: SI Core failed to init\n");
	if (sicore_proc_init(hk_proc_dir))
		pr_warn("harakiri: SI Core proc failed\n");

	pr_info("harakiri: hivemind online, mode=%u\n", hk_hive.cog.mode);
	return 0;
}

#ifdef MODULE
static void __exit harakiri_exit(void)
{
	sicore_proc_exit();
	sicore_exit();
	hk_proc_exit();
	hk_sched_stop();
	del_timer_sync(&hk_tick_timer);
	pr_info("harakiri: agent-native core unloaded\n");
}
#endif

#ifdef MODULE
module_init(harakiri_init);
module_exit(harakiri_exit);
#else
/* Built-in: no unload. subsys_initcall so the HiveMind is up
 * before drivers that might want to tag tasks. */
subsys_initcall(harakiri_init);
#endif

MODULE_LICENSE("GPL");
MODULE_AUTHOR("N8toriousAi");
MODULE_DESCRIPTION("Harakiri agent-native kernel core");
MODULE_VERSION("0.1");
