import Foundation
import SharedModels

/// Static conjugation ending tables for all 20 tenses, used in Guide views.
/// Data sourced from the Spanish Verb Tenses chart and Conjuu ES conjugation rules.
struct TenseEndingTable: Identifiable {
    let id: String // tense ID
    let formula: String // e.g. "stem + ending" or "haber + past participle"
    let isCompound: Bool
    let auxiliaryForms: [String]? // haber conjugations for compound tenses
    let arEndings: [String] // 6 persons: yo, tú, él, nosotros, vosotros, ellos
    let erEndings: [String]
    let irEndings: [String]
    let stemRule: String // e.g. "infinitive minus -ar", "full infinitive"
    let arExample: (verb: String, stem: String)
    let erExample: (verb: String, stem: String)
    let irExample: (verb: String, stem: String)

    static let persons = ["yo", "tú", "él/ella", "nosotros", "vosotros", "ellos/ellas"]

    /// Build a full conjugation example for display
    func exampleForms(for verbType: String) -> [(person: String, form: String)] {
        let endings: [String]
        let example: (verb: String, stem: String)
        switch verbType {
        case "ar": endings = arEndings; example = arExample
        case "er": endings = erEndings; example = erExample
        case "ir": endings = irEndings; example = irExample
        default: return []
        }

        if isCompound, let aux = auxiliaryForms {
            let participleSuffix = verbType == "ar" ? "ado" : "ido"
            let participle = example.stem + participleSuffix
            return zip(Self.persons, aux).map { person, auxForm in
                (person, "\(auxForm) \(participle)")
            }
        }

        return zip(Self.persons, endings).map { person, ending in
            (person, example.stem + ending)
        }
    }

    // MARK: - All tense tables

    static let all: [TenseEndingTable] = [
        // ─── INDICATIVE ───

        TenseEndingTable(
            id: "ind_presente",
            formula: "stem + ending",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["-o", "-as", "-a", "-amos", "-áis", "-an"],
            erEndings: ["-o", "-es", "-e", "-emos", "-éis", "-en"],
            irEndings: ["-o", "-es", "-e", "-imos", "-ís", "-en"],
            stemRule: "Remove -ar/-er/-ir from infinitive",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "ind_preterito",
            formula: "stem + ending",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["-é", "-aste", "-ó", "-amos", "-asteis", "-aron"],
            erEndings: ["-í", "-iste", "-ió", "-imos", "-isteis", "-ieron"],
            irEndings: ["-í", "-iste", "-ió", "-imos", "-isteis", "-ieron"],
            stemRule: "Remove -ar/-er/-ir from infinitive",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "ind_imperfecto",
            formula: "stem + ending",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["-aba", "-abas", "-aba", "-ábamos", "-abais", "-aban"],
            erEndings: ["-ía", "-ías", "-ía", "-íamos", "-íais", "-ían"],
            irEndings: ["-ía", "-ías", "-ía", "-íamos", "-íais", "-ían"],
            stemRule: "Remove -ar/-er/-ir from infinitive",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "ind_futuro",
            formula: "infinitive + ending",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["-é", "-ás", "-á", "-emos", "-éis", "-án"],
            erEndings: ["-é", "-ás", "-á", "-emos", "-éis", "-án"],
            irEndings: ["-é", "-ás", "-á", "-emos", "-éis", "-án"],
            stemRule: "Use full infinitive as stem",
            arExample: ("hablar", "hablar"), erExample: ("comer", "comer"), irExample: ("vivir", "vivir")
        ),

        TenseEndingTable(
            id: "ind_perfecto",
            formula: "haber (presente) + past participle",
            isCompound: true,
            auxiliaryForms: ["he", "has", "ha", "hemos", "habéis", "han"],
            arEndings: ["-ado", "-ado", "-ado", "-ado", "-ado", "-ado"],
            erEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            irEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            stemRule: "Past participle: stem + -ado (ar) / -ido (er/ir)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "ind_pluscuamperfecto",
            formula: "haber (imperfecto) + past participle",
            isCompound: true,
            auxiliaryForms: ["había", "habías", "había", "habíamos", "habíais", "habían"],
            arEndings: ["-ado", "-ado", "-ado", "-ado", "-ado", "-ado"],
            erEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            irEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            stemRule: "Past participle: stem + -ado (ar) / -ido (er/ir)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "ind_futuro_perfecto",
            formula: "haber (futuro) + past participle",
            isCompound: true,
            auxiliaryForms: ["habré", "habrás", "habrá", "habremos", "habréis", "habrán"],
            arEndings: ["-ado", "-ado", "-ado", "-ado", "-ado", "-ado"],
            erEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            irEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            stemRule: "Past participle: stem + -ado (ar) / -ido (er/ir)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "ind_preterito_anterior",
            formula: "haber (pretérito) + past participle",
            isCompound: true,
            auxiliaryForms: ["hube", "hubiste", "hubo", "hubimos", "hubisteis", "hubieron"],
            arEndings: ["-ado", "-ado", "-ado", "-ado", "-ado", "-ado"],
            erEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            irEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            stemRule: "Past participle: stem + -ado (ar) / -ido (er/ir)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        // ─── CONDITIONAL ───

        TenseEndingTable(
            id: "cond_presente",
            formula: "infinitive + ending",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["-ía", "-ías", "-ía", "-íamos", "-íais", "-ían"],
            erEndings: ["-ía", "-ías", "-ía", "-íamos", "-íais", "-ían"],
            irEndings: ["-ía", "-ías", "-ía", "-íamos", "-íais", "-ían"],
            stemRule: "Use full infinitive as stem",
            arExample: ("hablar", "hablar"), erExample: ("comer", "comer"), irExample: ("vivir", "vivir")
        ),

        TenseEndingTable(
            id: "cond_perfecto",
            formula: "haber (condicional) + past participle",
            isCompound: true,
            auxiliaryForms: ["habría", "habrías", "habría", "habríamos", "habríais", "habrían"],
            arEndings: ["-ado", "-ado", "-ado", "-ado", "-ado", "-ado"],
            erEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            irEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            stemRule: "Past participle: stem + -ado (ar) / -ido (er/ir)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        // ─── SUBJUNCTIVE ───

        TenseEndingTable(
            id: "subj_presente",
            formula: "stem + ending (opposite vowel)",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["-e", "-es", "-e", "-emos", "-éis", "-en"],
            erEndings: ["-a", "-as", "-a", "-amos", "-áis", "-an"],
            irEndings: ["-a", "-as", "-a", "-amos", "-áis", "-an"],
            stemRule: "Remove -ar/-er/-ir, use \"opposite\" vowel (ar→e, er/ir→a)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "subj_imperfecto_1",
            formula: "3rd person preterite stem + -ra endings",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["-ara", "-aras", "-ara", "-áramos", "-arais", "-aran"],
            erEndings: ["-iera", "-ieras", "-iera", "-iéramos", "-ierais", "-ieran"],
            irEndings: ["-iera", "-ieras", "-iera", "-iéramos", "-ierais", "-ieran"],
            stemRule: "From 3rd person plural preterite, remove -ron",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "subj_imperfecto_2",
            formula: "3rd person preterite stem + -se endings",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["-ase", "-ases", "-ase", "-ásemos", "-aseis", "-asen"],
            erEndings: ["-iese", "-ieses", "-iese", "-iésemos", "-ieseis", "-iesen"],
            irEndings: ["-iese", "-ieses", "-iese", "-iésemos", "-ieseis", "-iesen"],
            stemRule: "From 3rd person plural preterite, remove -ron",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "subj_perfecto",
            formula: "haber (subj. presente) + past participle",
            isCompound: true,
            auxiliaryForms: ["haya", "hayas", "haya", "hayamos", "hayáis", "hayan"],
            arEndings: ["-ado", "-ado", "-ado", "-ado", "-ado", "-ado"],
            erEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            irEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            stemRule: "Past participle: stem + -ado (ar) / -ido (er/ir)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "subj_pluscuamperfecto_1",
            formula: "haber (subj. imperfecto -ra) + past participle",
            isCompound: true,
            auxiliaryForms: ["hubiera", "hubieras", "hubiera", "hubiéramos", "hubierais", "hubieran"],
            arEndings: ["-ado", "-ado", "-ado", "-ado", "-ado", "-ado"],
            erEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            irEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            stemRule: "Past participle: stem + -ado (ar) / -ido (er/ir)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "subj_pluscuamperfecto_2",
            formula: "haber (subj. imperfecto -se) + past participle",
            isCompound: true,
            auxiliaryForms: ["hubiese", "hubieses", "hubiese", "hubiésemos", "hubieseis", "hubiesen"],
            arEndings: ["-ado", "-ado", "-ado", "-ado", "-ado", "-ado"],
            erEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            irEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            stemRule: "Past participle: stem + -ado (ar) / -ido (er/ir)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "subj_futuro",
            formula: "3rd person preterite stem + -re endings",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["-are", "-ares", "-are", "-áremos", "-areis", "-aren"],
            erEndings: ["-iere", "-ieres", "-iere", "-iéremos", "-iereis", "-ieren"],
            irEndings: ["-iere", "-ieres", "-iere", "-iéremos", "-iereis", "-ieren"],
            stemRule: "Rarely used; from 3rd person plural preterite stem",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "subj_futuro_perfecto",
            formula: "haber (subj. futuro) + past participle",
            isCompound: true,
            auxiliaryForms: ["hubiere", "hubieres", "hubiere", "hubiéremos", "hubiereis", "hubieren"],
            arEndings: ["-ado", "-ado", "-ado", "-ado", "-ado", "-ado"],
            erEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            irEndings: ["-ido", "-ido", "-ido", "-ido", "-ido", "-ido"],
            stemRule: "Past participle: stem + -ado (ar) / -ido (er/ir)",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        // ─── IMPERATIVE ───

        TenseEndingTable(
            id: "imp_afirmativo",
            formula: "stem + ending",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["—", "-a", "-e", "-emos", "-ad", "-en"],
            erEndings: ["—", "-e", "-a", "-amos", "-ed", "-an"],
            irEndings: ["—", "-e", "-a", "-amos", "-id", "-an"],
            stemRule: "Remove -ar/-er/-ir; tú form = 3rd person singular present",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),

        TenseEndingTable(
            id: "imp_negativo",
            formula: "no + subjunctive present",
            isCompound: false, auxiliaryForms: nil,
            arEndings: ["—", "no -es", "no -e", "no -emos", "no -éis", "no -en"],
            erEndings: ["—", "no -as", "no -a", "no -amos", "no -áis", "no -an"],
            irEndings: ["—", "no -as", "no -a", "no -amos", "no -áis", "no -an"],
            stemRule: "Uses subjunctive present forms with \"no\" prefix",
            arExample: ("hablar", "habl"), erExample: ("comer", "com"), irExample: ("vivir", "viv")
        ),
    ]

    static func find(_ tenseId: String) -> TenseEndingTable? {
        all.first { $0.id == tenseId }
    }
}
