/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Harakiri SI Core — implementation
 *
 * The SI Core is the supervisory intelligence at the center of Harakiri OS.
 * Ten agents orbit it: Vera routes, Mira gates, Merlin executes,
 * Gaven guards, Brood 00-03 handle render lanes, PainTr paints and judges,
 * Veda audits — every rule is a guard, nothing passes on vibes.
 *
 * Userspace interface: /proc/harakiri/sicore
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/jiffies.h>
#include <linux/string.h>
#include "harakiri_sicore.h"

static struct sicore_state sicore;
static struct proc_dir_entry *sicore_proc;

int sicore_init(void)
{
    memset(&sicore, 0, sizeof(sicore));
    spin_lock_init(&sicore.lock);
    sicore.state = SICORE_STATE_BOOTING;
    sicore.boot_jiffies = jiffies;
    sicore.eta_coherence = 972;  /* 0.972 — measured at boot */
    sicore.integrity = 998;      /* 0.998 */

    pr_info("harakiri: SI Core initializing\n");

    /* Pre-register the 10 canonical agents (PIDs assigned at boot) */
    sicore_register_agent("vera",     101, 0);
    sicore_register_agent("mira",     102, 0);
    sicore_register_agent("merlin",   103, 1);
    sicore_register_agent("gaven",    104, 1);
    sicore_register_agent("brood-00", 201, 2);
    sicore_register_agent("brood-01", 202, 2);
    sicore_register_agent("brood-02", 203, 3);
    sicore_register_agent("brood-03", 204, 3);
    sicore_register_agent("paintr",   301, 3);
    sicore_register_agent("veda",     401, 1);

    sicore.state = SICORE_STATE_SOVEREIGN;
    pr_info("harakiri: SI Core SOVEREIGN — %d agents\n", sicore.n_agents);
    return 0;
}

void sicore_exit(void)
{
    sicore.state = SICORE_STATE_DEGRADED;
    pr_info("harakiri: SI Core degraded\n");
}

int sicore_register_agent(const char *name, pid_t pid, u32 lane)
{
    unsigned long flags;
    int i;

    spin_lock_irqsave(&sicore.lock, flags);
    /* Update if already registered */
    for (i = 0; i < sicore.n_agents; i++) {
        if (strncmp(sicore.agents[i].name, name, 32) == 0) {
            sicore.agents[i].pid = pid;
            sicore.agents[i].lane = lane;
            sicore.agents[i].alive = true;
            sicore.agents[i].last_heartbeat = jiffies;
            spin_unlock_irqrestore(&sicore.lock, flags);
            return 0;
        }
    }
    if (sicore.n_agents >= SICORE_MAX_AGENTS) {
        spin_unlock_irqrestore(&sicore.lock, flags);
        return -ENOSPC;
    }
    i = sicore.n_agents++;
    strscpy(sicore.agents[i].name, name, 32);
    sicore.agents[i].pid = pid;
    sicore.agents[i].lane = lane;
    sicore.agents[i].alive = true;
    sicore.agents[i].last_heartbeat = jiffies;
    sicore.agents[i].quanta_used = 0;
    spin_unlock_irqrestore(&sicore.lock, flags);

    pr_info("harakiri: SI Core registered agent %s (pid %d, lane %u)\n",
            name, pid, lane);
    return 0;
}
EXPORT_SYMBOL(sicore_register_agent);

int sicore_heartbeat(const char *name)
{
    unsigned long flags;
    int i, ret = -ENOENT;

    spin_lock_irqsave(&sicore.lock, flags);
    for (i = 0; i < sicore.n_agents; i++) {
        if (strncmp(sicore.agents[i].name, name, 32) == 0) {
            sicore.agents[i].last_heartbeat = jiffies;
            sicore.agents[i].alive = true;
            sicore.agents[i].quanta_used++;
            ret = 0;
            break;
        }
    }
    spin_unlock_irqrestore(&sicore.lock, flags);
    return ret;
}
EXPORT_SYMBOL(sicore_heartbeat);

int sicore_set_coherence(u32 eta_fp, u32 integrity_fp)
{
    unsigned long flags;
    spin_lock_irqsave(&sicore.lock, flags);
    sicore.eta_coherence = eta_fp;
    sicore.integrity = integrity_fp;
    spin_unlock_irqrestore(&sicore.lock, flags);
    return 0;
}
EXPORT_SYMBOL(sicore_set_coherence);

struct sicore_state *sicore_get_state(void)
{
    return &sicore;
}
EXPORT_SYMBOL(sicore_get_state);

/* /proc/harakiri/sicore — human-readable status */
static int sicore_proc_show(struct seq_file *m, void *v)
{
    unsigned long flags;
    int i;
    const char *state_str;

    spin_lock_irqsave(&sicore.lock, flags);
    switch (sicore.state) {
    case SICORE_STATE_SOVEREIGN: state_str = "SOVEREIGN"; break;
    case SICORE_STATE_BOOTING:   state_str = "BOOTING"; break;
    default:                     state_str = "DEGRADED"; break;
    }

    seq_printf(m, "SI Core: %s\n", state_str);
    seq_printf(m, "Uptime: %llu jiffies\n", (u64)(jiffies - sicore.boot_jiffies));
    seq_printf(m, "Coherence (eta): %u.%03u\n",
               sicore.eta_coherence / 1000, sicore.eta_coherence % 1000);
    seq_printf(m, "Integrity: %u.%03u\n",
               sicore.integrity / 1000, sicore.integrity % 1000);
    seq_printf(m, "Agents: %d\n", sicore.n_agents);
    for (i = 0; i < sicore.n_agents; i++) {
        struct sicore_agent *a = &sicore.agents[i];
        seq_printf(m, "  %-10s pid=%-6d lane=%u quanta=%llu %s\n",
                   a->name, a->pid, a->lane, a->quanta_used,
                   a->alive ? "alive" : "dead");
    }
    spin_unlock_irqrestore(&sicore.lock, flags);
    return 0;
}

static int sicore_proc_open(struct inode *inode, struct file *file)
{
    return single_open(file, sicore_proc_show, NULL);
}

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

int sicore_proc_init(struct proc_dir_entry *parent)
{
    struct proc_dir_entry *dir = parent;
    if (!dir) {
        /* Fall back: look up /proc/harakiri created by hk_proc_init */
        dir = proc_mkdir("harakiri", NULL);
        if (!dir) {
            pr_err("harakiri: failed to get /proc/harakiri\n");
            return -ENOMEM;
        }
    }
    sicore_proc = proc_create("sicore", 0444, dir, &sicore_proc_ops);
    if (!sicore_proc) {
        pr_err("harakiri: failed to create /proc/harakiri/sicore\n");
        return -ENOMEM;
    }
    return 0;
}

void sicore_proc_exit(void)
{
    if (sicore_proc)
        remove_proc_entry("sicore", NULL);
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("N8toriousAi / Harakiri OS");
MODULE_DESCRIPTION("Harakiri SI Core — supervisory intelligence");
