import Foundation

/// Maps tense guides ↔ grammar notes for the Guide tab's cross-link chips.
/// The forward map is the curated source; the reverse map is derived once.
///
/// A tense ID appears here only if at least one grammar note in
/// `GrammarNote.allNotesIncludingGenerated` covers a concept directly tied
/// to that tense (forms, contrast, triggers, choice). Two tenses currently
/// have no aligned notes and don't appear: `ind_pluscuamperfecto` and
/// `ind_preterito_anterior`.
enum GuideCrossLinks {
    /// Tense ID → ordered grammar note IDs that go deeper on this tense.
    /// Order matters — the first chip is the most-relevant note for the
    /// tense's primary teaching point.
    static let relatedNotes: [String: [String]] = [
        "ind_presente": [
            "present-indicative-conjugation",
            "irregular-yo-verbs",
            "stem-changing-verbs",
            "estar-gerund-progressive",
        ],
        "ind_preterito": [
            "preterite-vs-imperfect",
            "stem-changing-verbs",
        ],
        "ind_imperfecto": [
            "preterite-vs-imperfect",
        ],
        "ind_futuro": [
            "future-vs-ir-a",
        ],
        "ind_perfecto": [
            "present-perfect-tense",
        ],
        "ind_futuro_perfecto": [
            "future-perfect-tense",
        ],
        "cond_presente": [
            "conditional-if-clauses",
        ],
        "cond_perfecto": [
            "conditional-if-clauses",
        ],
        "subj_presente": [
            "subjunctive-triggers",
            "irregular-yo-verbs",
            "stem-changing-verbs",
        ],
        "subj_imperfecto_1": [
            "subjunctive-triggers",
            "conditional-if-clauses",
        ],
        "subj_imperfecto_2": [
            "subjunctive-triggers",
            "conditional-if-clauses",
        ],
        "subj_perfecto": [
            "subjunctive-triggers",
        ],
        "subj_pluscuamperfecto_1": [
            "subjunctive-triggers",
            "conditional-if-clauses",
        ],
        "subj_pluscuamperfecto_2": [
            "subjunctive-triggers",
            "conditional-if-clauses",
        ],
        "subj_futuro": [
            "subjunctive-triggers",
        ],
        "subj_futuro_perfecto": [
            "subjunctive-triggers",
        ],
        "imp_afirmativo": [
            "commands-imperative",
        ],
        "imp_negativo": [
            "commands-imperative",
            "subjunctive-triggers",
        ],
    ]

    /// Grammar note ID → tense IDs that point at this note, ordered by the
    /// shared `TenseInfo.order` so chips appear in canonical conjugation
    /// order.
    static let relatedTenses: [String: [String]] = {
        var inverse: [String: [String]] = [:]
        for (tenseId, noteIds) in relatedNotes {
            for noteId in noteIds {
                inverse[noteId, default: []].append(tenseId)
            }
        }
        for key in inverse.keys {
            inverse[key]?.sort { lhs, rhs in
                (TenseInfo.find(lhs)?.order ?? 999) < (TenseInfo.find(rhs)?.order ?? 999)
            }
        }
        return inverse
    }()

    static func noteIds(forTense tenseId: String) -> [String] {
        relatedNotes[tenseId] ?? []
    }

    static func tenseIds(forNote noteId: String) -> [String] {
        relatedTenses[noteId] ?? []
    }
}
