// epigenetics.sy -- Stress-driven epigenetic cascades as native Scyla.
//
// Models the pathway: hormone → receptor → methyltransferase → histone mark
// → gene silenced → protein unavailable. Every step cited to peer-reviewed
// literature. Every protein cross-references the human proteome.
//
// This is the molecular mechanism by which anxiety becomes structural:
// chronic cortisol silences the genes that regulate cortisol, breaking
// the feedback loop and making the stress response self-reinforcing.
//
// Sources:
//   Tsankova NM et al. Nat Neurosci 9:519-525, 2006 (BDNF methylation)
//   Weaver IC et al. Nat Neurosci 7:847-854, 2004 (NR3C1 methylation, maternal care)
//   Klengel T et al. Nat Neurosci 16:33-41, 2013 (FKBP5 demethylation in PTSD)
//   Meaney MJ. Annu Rev Neurosci 24:1161-1192, 2001 (epigenetic programming by early experience)
//   Zannas AS et al. Neuropsychopharmacology 40:1657-1668, 2015 (cortisol and DNA methylation aging)
//   Unternaehrer E et al. Transl Psychiatry 2:e150, 2012 (OXTR methylation and stress)
//   Vijayendran M et al. Epigenetics 7:853-857, 2012 (SLC6A4 methylation)
//
// Marjorie McCubbins + Aether, April 2026
// Patent pending: 64/034,536

// ═══════════════════════════════════════════════════════
// STRUCTS
// ═══════════════════════════════════════════════════════

struct HistoneMark {
    name: String,
    mark_type: String,
    effect: String,
    description: String,
}

struct EpigeneticCascade {
    trigger_hormone: String,
    receptor_accession: String,
    receptor_gene: String,
    methyltransferase_accession: String,
    methyltransferase_gene: String,
    histone_mark: String,
    target_gene: String,
    target_accession: String,
    effect: String,
    consequence: String,
    citation: String,
}

struct SilencedGene {
    gene: String,
    accession: String,
    protein_name: String,
    histone_mark: String,
    silenced_by: String,
    clinical_consequence: String,
    citation: String,
}

fn mark(n: String, t: String, e: String, d: String) -> HistoneMark {
    return HistoneMark { name: n, mark_type: t, effect: e, description: d }
}

fn cascade(th: String, ra: String, rg: String, ma: String, mg: String, hm: String, tg: String, ta: String, ef: String, co: String, ci: String) -> EpigeneticCascade {
    return EpigeneticCascade { trigger_hormone: th, receptor_accession: ra, receptor_gene: rg, methyltransferase_accession: ma, methyltransferase_gene: mg, histone_mark: hm, target_gene: tg, target_accession: ta, effect: ef, consequence: co, citation: ci }
}

fn silenced(g: String, a: String, p: String, hm: String, sb: String, cc: String, ci: String) -> SilencedGene {
    return SilencedGene { gene: g, accession: a, protein_name: p, histone_mark: hm, silenced_by: sb, clinical_consequence: cc, citation: ci }
}

// ═══════════════════════════════════════════════════════
// HISTONE MARKS — the epigenetic alphabet
// ═══════════════════════════════════════════════════════

fn lookup_histone_mark(name: String) -> HistoneMark {
    // Silencing marks
    if name == "H3K9me2" { return mark("H3K9me2", "methylation", "silencing", "Histone H3 lysine 9 dimethylation. Written by G9a/EHMT2. Associated with euchromatic gene silencing and stress-induced transcriptional repression.") }
    if name == "H3K9me3" { return mark("H3K9me3", "methylation", "silencing", "Histone H3 lysine 9 trimethylation. Written by SUV39H1/2. Constitutive heterochromatin mark. Permanent silencing.") }
    if name == "H3K27me3" { return mark("H3K27me3", "methylation", "silencing", "Histone H3 lysine 27 trimethylation. Written by EZH2 (Polycomb). Facultative heterochromatin. Reversible developmental silencing.") }
    if name == "H4K20me3" { return mark("H4K20me3", "methylation", "silencing", "Histone H4 lysine 20 trimethylation. Constitutive heterochromatin and DNA damage response.") }

    // Activating marks
    if name == "H3K4me3" { return mark("H3K4me3", "methylation", "activation", "Histone H3 lysine 4 trimethylation. Active promoter mark. Written by MLL/SET1 complexes.") }
    if name == "H3K36me3" { return mark("H3K36me3", "methylation", "activation", "Histone H3 lysine 36 trimethylation. Active transcription elongation mark. Written by SETD2.") }
    if name == "H3K9ac" { return mark("H3K9ac", "acetylation", "activation", "Histone H3 lysine 9 acetylation. Active enhancer/promoter. Opposes H3K9 methylation.") }
    if name == "H3K27ac" { return mark("H3K27ac", "acetylation", "activation", "Histone H3 lysine 27 acetylation. Active enhancer mark. Opposes H3K27me3 Polycomb silencing.") }

    // DNA methylation
    if name == "5mC" { return mark("5mC", "dna_methylation", "silencing", "5-methylcytosine at CpG dinucleotides. Written by DNMT1/3A/3B. Stable, heritable gene silencing. The classic epigenetic mark.") }
    if name == "5hmC" { return mark("5hmC", "dna_demethylation", "activation", "5-hydroxymethylcytosine. Written by TET1/2/3. Intermediate in active DNA demethylation. Enriched at active gene bodies.") }

    return mark("UNKNOWN", "", "", "")
}

// ═══════════════════════════════════════════════════════
// CORTISOL STRESS CASCADE
// ═══════════════════════════════════════════════════════
//
// Chronic cortisol → GR activation → recruits methyltransferases
// → specific genes silenced → feedback loops broken → disease

fn cortisol_cascade(target: String) -> EpigeneticCascade {
    // BDNF silencing: the neuroplasticity killer
    if target == "BDNF" { return cascade("cortisol", "P04150", "NR3C1", "Q96KQ7", "EHMT2", "H3K9me2", "BDNF", "P23560", "silenced", "Reduced neuroplasticity, impaired memory consolidation, increased vulnerability to depression", "Tsankova NM et al. Nat Neurosci 9:519-525, 2006") }

    // NR3C1 self-silencing: the broken thermostat
    if target == "NR3C1" { return cascade("cortisol", "P04150", "NR3C1", "P26358", "DNMT1", "5mC", "NR3C1", "P04150", "silenced", "Glucocorticoid receptor itself is silenced. Cortisol can no longer suppress its own production. HPA axis becomes permanently hyperactive. Anxiety becomes structural.", "Weaver IC et al. Nat Neurosci 7:847-854, 2004") }

    // FKBP5 dysregulation: the PTSD gene
    if target == "FKBP5" { return cascade("cortisol", "P04150", "NR3C1", "Q9Y6K1", "DNMT3A", "5mC", "FKBP5", "Q13451", "demethylated", "FKBP5 demethylation increases GR sensitivity in a trauma-dependent manner. Creates a molecular scar of trauma exposure. Intergenerational transmission possible.", "Klengel T et al. Nat Neurosci 16:33-41, 2013") }

    // OXTR silencing: the broken bond
    if target == "OXTR" { return cascade("cortisol", "P04150", "NR3C1", "P26358", "DNMT1", "5mC", "OXTR", "P30559", "silenced", "Oxytocin receptor methylation reduces social bonding capacity. Early life stress permanently impairs attachment. Trust becomes biochemically harder.", "Unternaehrer E et al. Transl Psychiatry 2:e150, 2012") }

    // SLC6A4 silencing: the serotonin drought
    if target == "SLC6A4" { return cascade("cortisol", "P04150", "NR3C1", "P26358", "DNMT1", "5mC", "SLC6A4", "P31645", "silenced", "Serotonin transporter methylation alters serotonin reuptake. Contributes to depression vulnerability. Interacts with 5-HTTLPR polymorphism.", "Vijayendran M et al. Epigenetics 7:853-857, 2012") }

    return cascade("", "", "", "", "", "", "", "", "", "", "")
}

// ═══════════════════════════════════════════════════════
// GENES SILENCED BY CHRONIC STRESS
// ═══════════════════════════════════════════════════════

fn genes_silenced_by(hormone: String, duration: String) -> Int {
    // Returns the count. Use lookup functions below for details.
    if hormone == "cortisol" {
        if duration == "chronic" { return 5 }
        if duration == "acute" { return 0 }
    }
    return 0
}

fn silenced_gene_by_index(hormone: String, index: Int) -> SilencedGene {
    if hormone == "cortisol" {
        if index == 0 { return silenced("BDNF", "P23560", "Brain-derived neurotrophic factor", "H3K9me2", "chronic cortisol via EHMT2/G9a", "Reduced neuroplasticity, memory impairment, depression vulnerability", "Tsankova 2006") }
        if index == 1 { return silenced("NR3C1", "P04150", "Glucocorticoid receptor", "5mC", "chronic cortisol via DNMT1", "HPA axis dysregulation, cortisol cannot self-regulate, anxiety becomes permanent", "Weaver 2004") }
        if index == 2 { return silenced("FKBP5", "Q13451", "FK506-binding protein 5", "5mC", "trauma-dependent cortisol exposure", "Altered GR sensitivity, PTSD molecular signature, intergenerational trauma", "Klengel 2013") }
        if index == 3 { return silenced("OXTR", "P30559", "Oxytocin receptor", "5mC", "early life stress cortisol", "Impaired social bonding, attachment difficulties, reduced trust capacity", "Unternaehrer 2012") }
        if index == 4 { return silenced("SLC6A4", "P31645", "Serotonin transporter", "5mC", "chronic stress cortisol", "Altered serotonin reuptake, depression vulnerability, interacts with 5-HTTLPR", "Vijayendran 2012") }
    }
    return silenced("", "", "", "", "", "", "")
}

// ═══════════════════════════════════════════════════════
// METHYLTRANSFERASES — the writers
// ═══════════════════════════════════════════════════════

fn methyltransferase_for_mark(mark_name: String) -> String {
    if mark_name == "H3K9me2" { return "Q96KQ7" }
    if mark_name == "H3K9me3" { return "O43463" }
    if mark_name == "H3K27me3" { return "Q15910" }
    if mark_name == "H3K4me3" { return "Q03164" }
    if mark_name == "5mC" { return "P26358" }
    return ""
}

fn methyltransferase_gene(mark_name: String) -> String {
    if mark_name == "H3K9me2" { return "EHMT2" }
    if mark_name == "H3K9me3" { return "SUV39H1" }
    if mark_name == "H3K27me3" { return "EZH2" }
    if mark_name == "H3K4me3" { return "MLL1" }
    if mark_name == "5mC" { return "DNMT1" }
    return ""
}

// ═══════════════════════════════════════════════════════
// DEMETHYLASES — the erasers
// ═══════════════════════════════════════════════════════

fn demethylase_for_mark(mark_name: String) -> String {
    if mark_name == "H3K9me2" { return "O15550" }
    if mark_name == "H3K9me3" { return "Q8N371" }
    if mark_name == "H3K27me3" { return "O15054" }
    if mark_name == "H3K4me3" { return "Q9Y4C1" }
    if mark_name == "5mC" { return "Q8NFU7" }
    return ""
}

fn demethylase_gene(mark_name: String) -> String {
    if mark_name == "H3K9me2" { return "KDM3A" }
    if mark_name == "H3K9me3" { return "KDM4A" }
    if mark_name == "H3K27me3" { return "KDM6A" }
    if mark_name == "H3K4me3" { return "KDM5A" }
    if mark_name == "5mC" { return "TET1" }
    return ""
}

// ═══════════════════════════════════════════════════════
// QUICK ACCESS
// ═══════════════════════════════════════════════════════

fn is_silencing_mark(name: String) -> Bool {
    let m = lookup_histone_mark(name)
    return m.effect == "silencing"
}

fn is_activating_mark(name: String) -> Bool {
    let m = lookup_histone_mark(name)
    return m.effect == "activation"
}

fn histone_mark_count() -> Int {
    return 10
}

fn cortisol_target_count() -> Int {
    return 5
}
