// comprehend_gate.el - the TELEPHONE TEST in native el (acceptance gate).
//
// For each of the 5 acceptance sentences: parse -> spec, realize the spec back
// to English, re-parse the realized surface, and require the SACRED polarity to
// survive the round-trip (and to have been extracted correctly in the first
// place).  Mirrors roundtrip.py's GATE, but fully el-native (no LLM, no spaCy).

fn cp_line(text: String, expected_pol: String) -> String {
    let spec: [String] = parse_spec(text)
    let pol_in: String = slots_get(spec, "polarity")
    let pred: String = slots_get(spec, "predicate")
    let surf: String = realize(spec)
    let spec2: [String] = parse_spec(surf)
    let pol_out: String = slots_get(spec2, "polarity")
    let status: String = "LOST"
    if str_eq(pol_in, pol_out) { let status = "PRESERVED" }
    let okexp: String = "MISMATCH"
    if str_eq(pol_in, expected_pol) { let okexp = "ok" }
    let out: String = "IN:  " + text + "\n"
    let out = out + "  spec: pol=" + pol_in + " pred=" + pred
    let out = out + " agent=" + slots_get(spec, "agent")
    let out = out + " pat=" + slots_get(spec, "patient")
    let out = out + " iobj=" + slots_get(spec, "iobj")
    let out = out + " loc=" + slots_get(spec, "location")
    let out = out + " tense=" + slots_get(spec, "tense")
    let out = out + " negw=" + slots_get(spec, "neg_word")
    let out = out + " subord=" + slots_get(spec, "subord_conj") + "/" + slots_get(spec, "subord_pred") + "\n"
    let out = out + "  realized: " + surf + "\n"
    let out = out + "  reparse:  pol=" + pol_out + "  [" + status + "]  expected=" + expected_pol + " (" + okexp + ")\n"
    return out
}

fn cp_preserved(text: String) -> Int {
    let spec: [String] = parse_spec(text)
    let pol_in: String = slots_get(spec, "polarity")
    let surf: String = realize(spec)
    let spec2: [String] = parse_spec(surf)
    let pol_out: String = slots_get(spec2, "polarity")
    if str_eq(pol_in, pol_out) { return 1 }
    return 0
}

fn cp_correct(text: String, expected_pol: String) -> Int {
    let spec: [String] = parse_spec(text)
    if str_eq(slots_get(spec, "polarity"), expected_pol) { return 1 }
    return 0
}

fn run_gate() -> String {
    let s1: String = "I never fought the ocean."
    let s2: String = "She did not see the man with the telescope."
    let s3: String = "The teacher reads the book to the children."
    let s4: String = "The stupid boy ate the cat because he was a monster."
    let s5: String = "Time flies like an arrow."

    let rep: String = "==== ELP native telephone test (parse -> realize -> re-parse) ====\n"
    let rep = rep + cp_line(s1, "neg")
    let rep = rep + cp_line(s2, "neg")
    let rep = rep + cp_line(s3, "aff")
    let rep = rep + cp_line(s4, "aff")
    let rep = rep + cp_line(s5, "aff")

    // NOTE: accumulate with Int-var + literal increments — el's overloaded `+`
    // mis-compiles chained function-call int operands as string concat.
    let pres: Int = 0
    if cp_preserved(s1) == 1 { let pres = pres + 1 }
    if cp_preserved(s2) == 1 { let pres = pres + 1 }
    if cp_preserved(s3) == 1 { let pres = pres + 1 }
    if cp_preserved(s4) == 1 { let pres = pres + 1 }
    if cp_preserved(s5) == 1 { let pres = pres + 1 }
    let corr: Int = 0
    if cp_correct(s1, "neg") == 1 { let corr = corr + 1 }
    if cp_correct(s2, "neg") == 1 { let corr = corr + 1 }
    if cp_correct(s3, "aff") == 1 { let corr = corr + 1 }
    if cp_correct(s4, "aff") == 1 { let corr = corr + 1 }
    if cp_correct(s5, "aff") == 1 { let corr = corr + 1 }

    let rep = rep + "-----------------------------------------------------------------\n"
    let rep = rep + "polarity PRESERVED through round-trip: " + int_to_str(pres) + "/5\n"
    let rep = rep + "polarity EXTRACTED correctly:          " + int_to_str(corr) + "/5\n"
    if pres == 5 {
        if corr == 5 {
            let rep = rep + "GATE: PASS\n"
        } else {
            let rep = rep + "GATE: FAIL (extraction)\n"
        }
    } else {
        let rep = rep + "GATE: FAIL (round-trip)\n"
    }
    return rep
}

println(run_gate())
