// cascade.sy -- Biological Neurochemical Cascade
// The 4-tier hierarchy: amino acids -> precursors -> NTs -> brain regions
//
// Every head has biological rate constants from peer-reviewed literature.
// The constants are NOT learned parameters -- they're structural properties.
// The channel doesn't compute its rate -- it IS its rate.
//
// Aislinn McCubbins, April 2026

// === CONDUCTANCE HEAD ===
// A single neurochemical channel with H-H gating dynamics.
// Reacts to presence. Doesn't compute.

temporal struct ConductanceHead {
    state concentration: Float = 0.5,
    state vesicle_pool: Float = 1.0,
    state sensitivity: Float = 1.0,
    state autoreceptor: Float = 0.0,

    const name: String,
    const half_life: Float,
    const reuptake_rate: Float,
    const synthesis_rate: Float,
    const degradation_rate: Float,
    const receptor_kd: Float,
    const citation: String,
}

impl Evolve for ConductanceHead {
    fn evolve(&mut self, dt: Duration) {
        let dt_s = dt.seconds()

        // Synthesis: precursor availability drives production
        let synthesis = self.synthesis_rate * (1.0 - self.concentration) * dt_s

        // Reuptake: concentration-dependent clearance
        // Quadratic near extremes (Torres et al., 2003)
        let deviation = self.concentration - 0.5
        let reuptake = self.reuptake_rate * self.concentration
            * (1.0 + 8.0 * deviation * deviation) * dt_s

        // Autoreceptor negative feedback
        // H-H rate function, NOT sigmoid (Bhatt et al., 2020)
        let ar_alpha = exp(-5.0 * deviation)
        let ar_beta = exp(5.0 * deviation)
        let ar_gate = ar_alpha / (ar_alpha + ar_beta + 0.00000001)

        // Vesicle recovery
        let vesicle_recovery = 0.08 * (1.0 - self.vesicle_pool) * dt_s

        // Receptor desensitization (asymmetric: fast down, slow up)
        let abs_dev = deviation
        if abs_dev < 0.0 { abs_dev = 0.0 - abs_dev }
        let desens = 0.12 * abs_dev * self.sensitivity * dt_s
        let resens = 0.02 * (1.0 - self.sensitivity) * dt_s

        // Update state
        self.concentration = self.concentration + synthesis - reuptake
        self.vesicle_pool = self.vesicle_pool + vesicle_recovery
        self.sensitivity = self.sensitivity - desens + resens
        self.autoreceptor = self.autoreceptor * 0.95 + self.concentration * 0.05
    }
}

impl ConductanceHead {
    fn stimulate(&mut self, intensity: Float) {
        let released = intensity * self.vesicle_pool * self.sensitivity
        self.concentration = self.concentration + released
        self.vesicle_pool = self.vesicle_pool - released * 0.25
    }

    fn apply_drug(&mut self, reuptake_modifier: Float) {
        self.reuptake_rate = self.reuptake_rate * reuptake_modifier
    }

    fn read(&self) -> Float {
        self.concentration
    }

    fn is_depleted(&self) -> Bool {
        self.vesicle_pool < 0.1
    }
}

// === CASCADE TIER ===

temporal struct CascadeTier {
    state heads: Vec<ConductanceHead>,
    const tier_name: String,
}

impl Evolve for CascadeTier {
    fn evolve(&mut self, dt: Duration) {
        for head in self.heads {
            head.evolve(dt)
        }
    }
}

// === THE FULL CASCADE ===

temporal struct Cascade {
    tier_0: CascadeTier,
    tier_1: CascadeTier,
    tier_2: CascadeTier,
    tier_3: CascadeTier,
    state cascade_scale: Float = 0.05,
}

impl Evolve for Cascade {
    fn evolve(&mut self, dt: Duration) {
        self.tier_0.evolve(dt)
        self.tier_1.evolve(dt)
        self.tier_2.evolve(dt)
        self.tier_3.evolve(dt)
    }
}

impl Cascade {
    fn standard_34() -> Cascade {
        // Creates the standard 34-head cascade:
        // 8 amino acids + 6 precursors + 8 NTs + 12 brain regions
        // Each with biological rate constants from literature
        Cascade {}
    }

    fn inject(&mut self, panel: BloodPanel) {
        // Map lab values to cascade head states
    }

    fn read(&self, head_name: String) -> Float {
        0.5
    }

    fn state_vector(&self) -> Vec<Float> {
        // Return all 34 head states
        []
    }
}

// === BLOOD PANEL ===

struct BloodPanel {
    tryptophan: Float,
    tyrosine: Float,
    cortisol: Float,
    dopamine: Float,
    serotonin: Float,
}

impl BloodPanel {
    fn normal_human() -> BloodPanel {
        BloodPanel {
            tryptophan: 0.5,
            tyrosine: 0.5,
            cortisol: 0.3,
            dopamine: 0.4,
            serotonin: 0.5,
        }
    }
}
