/* harakiri_proc.c — userspace control interface
 *
 * /proc/harakiri/status — read: mode, agents, tags
 * /proc/harakiri/tag    — write "PID AGENT_ID": tag a task to an agent
 * /proc/harakiri/untag  — write "PID": remove a task's tag
 * /proc/harakiri/mode   — write "phone"|"gaming"|"desktop" (or 0/1/2)
 *
 * Example:
 *   echo "1234 5" > /proc/harakiri/tag    # pid 1234 serves brood-00
 *   cat /proc/harakiri/status
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>

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

extern struct hk_hivemind hk_hive;
extern void hk_sched_tag(struct hk_hivemind *h, u32 pid, u32 agent_id,
			 u8 role);
extern void hk_sched_untag(struct hk_hivemind *h, u32 pid);

static struct proc_dir_entry *hk_proc_dir;

static const char *hk_mode_name(u8 m)
{
	switch (m) {
	case HK_MODE_PHONE:  return "phone";
	case HK_MODE_GAMING: return "gaming";
	case HK_MODE_DESKTOP: return "desktop";
	default: return "?";
	}
}

static const char *hk_role_name(u8 r)
{
	switch (r) {
	case HK_VERA:   return "vera";
	case HK_MIRA:   return "mira";
	case HK_MERLIN: return "merlin";
	case HK_GAVEN:  return "gaven";
	case HK_BROOD:  return "brood";
	case HK_HIVE:   return "hive";
	case HK_PAINTR: return "paintr";
	default: return "?";
	}
}

static int hk_status_show(struct seq_file *m, void *v)
{
	struct hk_hivemind *h = &hk_hive;
	int i;

	seq_printf(m, "mode: %s (tick %u)\n", hk_mode_name(h->cog.mode),
		   h->tick);
	seq_puts(m, "agents:\n");
	for (i = 0; i < HK_HIVE_MAX_AGENTS; i++) {
		struct hk_hive_agent_slot *s = &h->slots[i];
		if (!s->used)
			continue;
		seq_printf(m, "  %u %-10s role=%s state=%u quanta=%u/%u eta=%u.%03u\n",
			   s->agent.id, s->agent.name,
			   hk_role_name(s->agent.role),
			   s->agent.state,
			   s->agent.quanta_done, s->agent.quanta,
			   s->agent.eta_x1000 / 1000,
			   s->agent.eta_x1000 % 1000);
	}
	seq_puts(m, "tags:\n");
	for (i = 0; i < HK_BROOD_TABLE_SIZE; i++) {
		struct hk_brood_tag *t = &h->brood.table[i];
		struct hk_hive_agent_slot *s;
		if (t->pid == 0)
			continue;
		s = hk_hive_find(h, t->agent_id);
		seq_printf(m, "  pid %u -> agent %u (%s) boost=%u\n",
			   t->pid, t->agent_id,
			   s ? s->agent.name : "?",
			   hk_brood_boost(&h->brood, t->pid));
	}
	return 0;
}

static int hk_status_open(struct inode *inode, struct file *file)
{
	return single_open(file, hk_status_show, NULL);
}

static ssize_t hk_tag_write(struct file *file, const char __user *buf,
			    size_t len, loff_t *ppos)
{
	char kbuf[32];
	u32 pid, agent_id;
	struct hk_hive_agent_slot *s;

	if (len >= sizeof(kbuf))
		return -EINVAL;
	if (copy_from_user(kbuf, buf, len))
		return -EFAULT;
	kbuf[len] = '\0';

	if (sscanf(kbuf, "%u %u", &pid, &agent_id) != 2)
		return -EINVAL;
	s = hk_hive_find(&hk_hive, agent_id);
	if (!s)
		return -ENOENT;

	hk_sched_tag(&hk_hive, pid, agent_id, s->agent.role);
	pr_info("harakiri: tagged pid %u -> agent %u (%s)\n",
		pid, agent_id, s->agent.name);
	return len;
}

static ssize_t hk_untag_write(struct file *file, const char __user *buf,
			      size_t len, loff_t *ppos)
{
	char kbuf[32];
	u32 pid;

	if (len >= sizeof(kbuf))
		return -EINVAL;
	if (copy_from_user(kbuf, buf, len))
		return -EFAULT;
	kbuf[len] = '\0';

	if (sscanf(kbuf, "%u", &pid) != 1)
		return -EINVAL;

	hk_sched_untag(&hk_hive, pid);
	pr_info("harakiri: untagged pid %u\n", pid);
	return len;
}

static ssize_t hk_mode_write(struct file *file, const char __user *buf,
			     size_t len, loff_t *ppos)
{
	char kbuf[16];
	u8 mode = 0xFF;

	if (len >= sizeof(kbuf))
		return -EINVAL;
	if (copy_from_user(kbuf, buf, len))
		return -EFAULT;
	kbuf[len] = '\0';

	if (strncmp(kbuf, "gaming", 6) == 0)
		mode = HK_MODE_GAMING;
	else if (strncmp(kbuf, "desktop", 7) == 0)
		mode = HK_MODE_DESKTOP;
	else if (strncmp(kbuf, "phone", 5) == 0)
		mode = HK_MODE_PHONE;
	else if (sscanf(kbuf, "%hhu", &mode) != 1 || mode >= HK_NMODES)
		return -EINVAL;

	hk_hive.cog.mode = mode;
	hk_hive.cog.manual = 1; /* spider must not override an explicit set */
	hk_hive.cog.ticks_since_switch = 0;
	hk_hive.brood.mode = mode; /* scheduler reads this directly, don't wait for tick */
	pr_info("harakiri: mode -> %s (manual)\n", hk_mode_name(mode));
	return len;
}

static const struct proc_ops hk_status_ops = {
	.proc_open	= hk_status_open,
	.proc_read	= seq_read,
	.proc_lseek	= seq_lseek,
	.proc_release	= single_release,
};

static const struct proc_ops hk_tag_ops = {
	.proc_write	= hk_tag_write,
};

static const struct proc_ops hk_untag_ops = {
	.proc_write	= hk_untag_write,
};

static const struct proc_ops hk_mode_ops = {
	.proc_write	= hk_mode_write,
};

int hk_proc_init(void)
{
	hk_proc_dir = proc_mkdir("harakiri", NULL);
	if (!hk_proc_dir)
		return -ENOMEM;
	proc_create("status", 0444, hk_proc_dir, &hk_status_ops);
	proc_create("tag", 0222, hk_proc_dir, &hk_tag_ops);
	proc_create("untag", 0222, hk_proc_dir, &hk_untag_ops);
	proc_create("mode", 0222, hk_proc_dir, &hk_mode_ops);
	pr_info("harakiri: /proc/harakiri ready\n");
	return 0;
}

void hk_proc_exit(void)
{
	remove_proc_entry("status", hk_proc_dir);
	remove_proc_entry("tag", hk_proc_dir);
	remove_proc_entry("untag", hk_proc_dir);
	remove_proc_entry("mode", hk_proc_dir);
	remove_proc_entry("harakiri", NULL);
}
