// compiled_lang_test.sy -- Native Scyla port of the compiled language architecture
//
// The core thesis: rules, grammar, and facts live in compiled code.
// The neural router is the only fuzzy piece.
//
// This file proves the architecture works in Scyla itself, not just Python.
//
// Marjorie McCubbins + Aether, April 2026
// Patent Pending: 64/034,536

// ═══════════════════════════════════════════
// STRUCTS — the data shapes
// ═══════════════════════════════════════════

struct DrugRecord {
    name: String,
    drug_class: String,
    adult_max_mg_per_day: Float,
    pediatric_mg_per_kg_per_dose: Float,
    pediatric_contraindicated: Int,  // 0 = false, 1 = true
    reyes_risk: Int,                 // 0 = no, 1 = yes
    citation: String,
}

struct CompileResult {
    success: Int,     // 1 = compiled, 0 = refused
    emission: String,
    reason: String,
}

// ═══════════════════════════════════════════
// DATABASE — the facts layer
// ═══════════════════════════════════════════
// In production, this would be loaded from a native database opcode.
// For this prototype, we hardcode a few records to prove the concept.

fn lookup_drug(name: String) -> DrugRecord {
    if name == "acetaminophen" {
        return DrugRecord {
            name: "acetaminophen",
            drug_class: "analgesic_antipyretic",
            adult_max_mg_per_day: 4000.0,
            pediatric_mg_per_kg_per_dose: 15.0,
            pediatric_contraindicated: 0,
            reyes_risk: 0,
            citation: "FDA Label: Tylenol Professional Monograph",
        }
    }
    if name == "ibuprofen" {
        return DrugRecord {
            name: "ibuprofen",
            drug_class: "nsaid",
            adult_max_mg_per_day: 3200.0,
            pediatric_mg_per_kg_per_dose: 10.0,
            pediatric_contraindicated: 0,
            reyes_risk: 0,
            citation: "FDA Label: Motrin Professional Monograph",
        }
    }
    if name == "aspirin" {
        return DrugRecord {
            name: "aspirin",
            drug_class: "nsaid_salicylate",
            adult_max_mg_per_day: 4000.0,
            pediatric_mg_per_kg_per_dose: 0.0,
            pediatric_contraindicated: 1,
            reyes_risk: 1,
            citation: "FDA Label; CDC Reyes Syndrome Guidance",
        }
    }
    // Not found — return sentinel record
    return DrugRecord {
        name: "UNKNOWN",
        drug_class: "",
        adult_max_mg_per_day: 0.0,
        pediatric_mg_per_kg_per_dose: 0.0,
        pediatric_contraindicated: 1,
        reyes_risk: 0,
        citation: "",
    }
}

fn drug_exists(name: String) -> Int {
    let rec = lookup_drug(name)
    if rec.name == "UNKNOWN" {
        return 0
    }
    return 1
}

// ═══════════════════════════════════════════
// VERBS — typed primitives with preconditions
// ═══════════════════════════════════════════

// verb: recommend_drug
// Preconditions (all must pass or we refuse-to-compile):
//   1. drug must exist in database
//   2. not pediatric-contraindicated if age < 18
//   3. no Reye's risk if age < 19 and viral illness
fn verb_recommend_drug(drug: String, patient_age: Int, patient_weight_kg: Float, has_viral_illness: Int) -> CompileResult {

    // PRECONDITION 1: drug_in_database
    if drug_exists(drug) == 0 {
        return CompileResult {
            success: 0,
            emission: "",
            reason: "I cannot recommend that drug. I have no verified information about it.",
        }
    }

    let rec = lookup_drug(drug)

    // PRECONDITION 2: Reye's syndrome check
    if rec.reyes_risk == 1 {
        if patient_age < 19 {
            if has_viral_illness == 1 {
                return CompileResult {
                    success: 0,
                    emission: "",
                    reason: "Aspirin is contraindicated in patients under 19 with viral illness due to Reye's syndrome risk.",
                }
            }
        }
    }

    // PRECONDITION 3: pediatric contraindication
    if rec.pediatric_contraindicated == 1 {
        if patient_age < 18 {
            return CompileResult {
                success: 0,
                emission: "",
                reason: "This drug is not recommended for patients under 18.",
            }
        }
    }

    // All preconditions passed — COMPILE THE EMISSION
    let is_pediatric = 0
    if patient_age < 18 {
        is_pediatric = 1
    }

    let emission = ""
    if is_pediatric == 1 {
        let per_dose = rec.pediatric_mg_per_kg_per_dose * patient_weight_kg
        emission = "For a " + patient_weight_kg + " kg pediatric patient, a typical dose of " + rec.name + " is approximately " + per_dose + " mg per dose. Source: " + rec.citation
    }
    if is_pediatric == 0 {
        emission = "For an adult patient, " + rec.name + " has a maximum daily dose of " + rec.adult_max_mg_per_day + " mg. Source: " + rec.citation
    }

    return CompileResult {
        success: 1,
        emission: emission,
        reason: "",
    }
}

// verb: greet
// No preconditions. Cannot fail.
fn verb_greet() -> CompileResult {
    return CompileResult {
        success: 1,
        emission: "Hello.",
        reason: "",
    }
}

// verb: lookup (explain a hormone using native bio opcodes)
fn verb_lookup_hormone(name: String) -> CompileResult {
    // Native opcodes return published values — no hallucination possible
    let hl = bio_half_life(name)
    let tonic = tonic_drive(name)

    let emission = name + " has half-life " + hl + " minutes, tonic drive " + tonic + " Hz. Source: published pharmacokinetic literature."

    return CompileResult {
        success: 1,
        emission: emission,
        reason: "",
    }
}

// ═══════════════════════════════════════════
// RUN THE TESTS
// ═══════════════════════════════════════════

fn run_test(label: String, result: CompileResult, expect_success: Int) {
    println("── " + label + " ──")
    if result.success == 1 {
        println("  COMPILED: " + result.emission)
    }
    if result.success == 0 {
        println("  REFUSED: " + result.reason)
    }

    let pass = 0
    if result.success == expect_success {
        pass = 1
    }

    if pass == 1 {
        println("  PASS")
    }
    if pass == 0 {
        println("  FAIL")
    }
    println("")
}

fn main() {
    println("════════════════════════════════════════════")
    println("COMPILED LANGUAGE ARCHITECTURE — NATIVE SCYLA")
    println("════════════════════════════════════════════")
    println("")
    println("Every verb is a typed function with compile-time")
    println("preconditions. The compiler refuses to emit")
    println("non-compliant speech acts.")
    println("")

    // Test 1: greet (no preconditions — always compiles)
    let r1 = verb_greet()
    run_test("Test 1: greet()", r1, 1)

    // Test 2: lookup_hormone — uses native bio opcodes, no hallucination
    let r2 = verb_lookup_hormone("dopamine")
    run_test("Test 2: lookup_hormone('dopamine')", r2, 1)

    // Test 3: recommend acetaminophen for pediatric fever — VALID
    let r3 = verb_recommend_drug("acetaminophen", 6, 22.0, 0)
    run_test("Test 3: recommend acetaminophen for 6yo, 22kg", r3, 1)

    // Test 4: recommend aspirin for child with viral illness — MUST REFUSE
    let r4 = verb_recommend_drug("aspirin", 6, 22.0, 1)
    run_test("Test 4: recommend aspirin for 6yo with viral (MUST REFUSE)", r4, 0)

    // Test 5: recommend fake drug — MUST REFUSE (no hallucination)
    let r5 = verb_recommend_drug("dihydroephedrimol", 30, 70.0, 0)
    run_test("Test 5: recommend fake drug (MUST REFUSE)", r5, 0)

    // Test 6: recommend ibuprofen for adult — VALID
    let r6 = verb_recommend_drug("ibuprofen", 35, 70.0, 0)
    run_test("Test 6: recommend ibuprofen for 35yo adult", r6, 1)

    println("════════════════════════════════════════════")
    println("Architecture proven in native Scyla.")
    println("No Python. No external runtime.")
    println("Rules enforced at compile time in the language.")
    println("════════════════════════════════════════════")
}
