@file:Suppress("HardCodedStringLiteral")

package eu.gaudian.translator.model.grammar

import eu.gaudian.translator.model.grammar.GrammarConstants.POS_ADJ
import eu.gaudian.translator.model.grammar.GrammarConstants.POS_NOUN
import eu.gaudian.translator.model.grammar.GrammarConstants.POS_VERB
import eu.gaudian.translator.model.grammar.MorphologyRule.GenericRule
import eu.gaudian.translator.model.grammar.MorphologyRule.GridRule
import eu.gaudian.translator.model.grammar.MorphologyRule.VerbRule

object MorphologyRegistry {

    /**
     * Retrieves the specific rule for a Language and POS.
     * Defaults to GenericRule if no specific rule is defined.
     */
    fun getRule(langCode: String, pos: String?): MorphologyRule {
        val normalizedPos = pos?.lowercase()?.trim() ?: return GenericRule

        // 1. Look for specific match (e.g., "de" + "noun")
        return rules["$langCode|$normalizedPos"]
        // 2. Look for language default (if we had one) or fallback
            ?: GenericRule
    }

    // =========================================================================
    // CONFIGURATION AREA - DEFINE YOUR RULES HERE
    // =========================================================================

    private val rules = mapOf(
        // --- GERMAN (DE) -----------------------------------------------------
        "de|$POS_NOUN" to GridRule(
            title = "Deklination",
            rowTags = listOf("nominative", "genitive", "dative", "accusative"),
            colTags = listOf("singular", "plural")
        ),
        "de|$POS_ADJ" to GridRule(
            title = "Adjektivendungen",
            rowTags = listOf("masculine", "feminine", "neuter"),
            colTags = listOf("singular", "plural"),
            fallbackStrategy = { row, col, lemma ->
                // Fallback: Use lemma for Masc+Sing if missing (common in some dicts)
                if (row == "masculine" && col == "singular") lemma else null
            }
        ),
        "de|$POS_VERB" to VerbRule(
            pronouns = listOf("ich", "du", "er/sie/es", "wir", "ihr", "sie"),
            tenses = mapOf(
                "present" to "Präsens",
                "past" to "Präteritum",
                "subjunctive_i" to "Konjunktiv I",
                "subjunctive_ii" to "Konjunktiv II",
                "imperative" to "Imperativ"
            )
        ),

        // --- FRENCH (FR) -----------------------------------------------------
        "fr|$POS_NOUN" to GridRule(
            title = "Nombre",
            rowTags = listOf("singular", "plural"),
            colTags = listOf("") // Single column (just list rows)
        ),
        "fr|$POS_ADJ" to GridRule(
            title = "Accord",
            rowTags = listOf("masculine", "feminine"),
            colTags = listOf("singular", "plural")
        ),
        "fr|$POS_VERB" to VerbRule(
            pronouns = listOf("je", "tu", "il/elle", "nous", "vous", "ils/elles"),
            tenses = mapOf(
                "indicative_present" to "Présent",
                "indicative_imperfect" to "Imparfait",
                "indicative_future" to "Futur",
                "subjunctive_present" to "Subjonctif",
                "conditional_present" to "Conditionnel"
            )
        ),

        // --- PORTUGUESE (PT) Example -----------------------------------------
        "pt|$POS_VERB" to VerbRule(
            pronouns = listOf("eu", "tu", "ele/ela", "nós", "vós", "eles/elas"),
            tenses = mapOf(
                "present" to "Presente",
                "imperfect" to "Imperfeito",
                "perfect" to "Perfeito"
            )
        )
    )
}