from __future__ import annotations

from collections.abc import Mapping

from gperm.util import detect_locale


MESSAGES: dict[str, dict[str, str]] = {
    "en": {
        "app.help": "Manage and sync permission settings across AI coding agent CLIs.",
        "version.help": "Show version and exit.",
        "project.help": "Project directory to inspect or update.",
        "agent.help": "Target agent. May be passed multiple times.",
        "level.help": "Target settings scope.",
        "format.help": "Output format.",
        "yes.help": "Apply changes without interactive confirmation.",
        "dry_run.help": "Preview changes without writing files.",
        "profile.help": "Override the configured profile for this command.",
        "command.help": "Override the configured agent command.",
        "config.help": "Use an explicit gperm config file instead of auto-discovery.",
        "config_app.help": "Inspect or initialize gperm config.",
        "config.show.help": "Show effective gperm config and active config sources.",
        "config.init.help": "Write a starter config file.",
        "import.help": "Import a native Claude or OpenCode settings file into gperm config.",
        "doctor.help": "Run setup diagnostics for gperm and supported agent CLIs.",
        "agents.help": "List supported agents and compatibility notes.",
        "check.help": "Check whether agent settings match the selected gperm profiles.",
        "sync.help": "Synchronize native agent settings to the selected gperm profiles.",
        "inline.help": "Print inline CLI flags for the selected agent profile.",
        "exec.help": "Run an agent command with inline flags generated from the selected profile.",
        "scope.global": "global",
        "scope.project": "project",
        "scope.all": "all",
        "source.user": "user config",
        "source.project": "project override",
        "source.defaults": "built-in defaults",
        "status.match": "match",
        "status.drift": "drift",
        "status.skipped": "skipped",
        "status.unsupported": "unsupported",
        "config.active_sources": "Active config sources",
        "config.project_root": "Project root",
        "config.default_profile": "Default profile",
        "config.resolved_profiles": "Resolved profiles",
        "config.ignored": "Ignored",
        "sync.no_changes": "No changes required.",
        "sync.pending_changes": "Planned changes",
        "sync.confirm": "Apply these changes?",
        "sync.applied": "Applied",
        "sync.skipped": "Skipped",
        "sync.aborted": "Aborted.",
        "inline.no_args": "This agent has no inline permission flags to expand.",
        "inline.command": "Full command",
        "inline.flags": "Flags",
        "exec.running": "Running",
        "check.summary": "Check summary",
        "warn.header": "Warnings",
        "note.header": "Notes",
        "unsupported.project": "Native project-level persistence is not available for this agent.",
        "ignored.project": "This project is ignored by the active gperm config.",
        "init.exists": "Config already exists. Use --force to overwrite it.",
        "init.wrote": "Wrote",
        "init.skipped": "Config already exists. Skipped.",
        "origin.header": "Using",
        "doctor.title": "Setup diagnostics",
        "doctor.summary": "Summary",
        "doctor.config": "Config",
        "doctor.agents": "Agents",
        "doctor.item": "Item",
        "doctor.status": "Status",
        "doctor.details": "Details",
        "doctor.ok": "ok",
        "doctor.warn": "warn",
        "doctor.fail": "fail",
        "doctor.found": "found",
        "doctor.missing": "missing",
        "doctor.profile": "profile",
        "doctor.command": "command",
        "doctor.active_sources": "active sources",
        "doctor.using_defaults": "using built-in defaults only",
        "doctor.project_ignored": "project is ignored by gperm",
        "doctor.no_issues": "No blocking issues detected.",
        "import.wrote": "Imported settings into",
        "import.profile": "Imported profile",
        "import.source": "Source file",
        "import.target": "Target config",
        "import.scope": "Target scope",
    },
    "ja": {
        "app.help": "AI coding agent CLI の権限設定を一元管理して同期します。",
        "version.help": "バージョンを表示して終了します。",
        "project.help": "確認または更新対象のプロジェクトディレクトリです。",
        "agent.help": "対象の agent です。複数指定できます。",
        "level.help": "対象の設定スコープです。",
        "format.help": "出力形式です。",
        "yes.help": "確認なしで変更を適用します。",
        "dry_run.help": "書き込まずに変更内容だけ表示します。",
        "profile.help": "このコマンドで使う profile を設定値より優先して指定します。",
        "command.help": "設定された agent command をこのコマンドだけ上書きします。",
        "config.help": "自動検出の代わりに gperm config を明示指定します。",
        "config_app.help": "gperm の設定を確認または初期化します。",
        "config.show.help": "有効な gperm 設定と利用中の設定ソースを表示します。",
        "config.init.help": "初期設定ファイルを書き出します。",
        "import.help": "Claude または OpenCode のネイティブ設定ファイルを gperm 設定へ取り込みます。",
        "doctor.help": "gperm と対応 agent CLI のセットアップ診断を行います。",
        "agents.help": "対応 agent と互換性メモを表示します。",
        "check.help": "各 agent の設定が選択された gperm profile と一致しているか確認します。",
        "sync.help": "選択された gperm profile に各 agent のネイティブ設定を同期します。",
        "inline.help": "選択した agent profile を CLI フラグへ展開して表示します。",
        "exec.help": "選択した profile から生成したフラグ付きで agent command を実行します。",
        "scope.global": "global",
        "scope.project": "project",
        "scope.all": "all",
        "source.user": "ユーザー設定",
        "source.project": "プロジェクト上書き設定",
        "source.defaults": "組み込み既定値",
        "status.match": "一致",
        "status.drift": "差分あり",
        "status.skipped": "スキップ",
        "status.unsupported": "未対応",
        "config.active_sources": "有効な設定ソース",
        "config.project_root": "プロジェクトルート",
        "config.default_profile": "既定 profile",
        "config.resolved_profiles": "解決後 profile",
        "config.ignored": "無視対象",
        "sync.no_changes": "変更はありません。",
        "sync.pending_changes": "適用予定の変更",
        "sync.confirm": "これらの変更を適用しますか？",
        "sync.applied": "適用しました",
        "sync.skipped": "スキップしました",
        "sync.aborted": "中止しました。",
        "inline.no_args": "この agent は展開できる inline 権限フラグを持っていません。",
        "inline.command": "実行コマンド",
        "inline.flags": "フラグ",
        "exec.running": "実行中",
        "check.summary": "確認結果",
        "warn.header": "警告",
        "note.header": "補足",
        "unsupported.project": "この agent にはネイティブな project-level 永続設定がありません。",
        "ignored.project": "このプロジェクトは有効な gperm 設定で無視対象です。",
        "init.exists": "設定ファイルは既に存在します。上書きするには --force を使ってください。",
        "init.wrote": "書き込みました",
        "init.skipped": "設定ファイルは既に存在するため、スキップしました。",
        "origin.header": "使用中",
        "doctor.title": "セットアップ診断",
        "doctor.summary": "概要",
        "doctor.config": "設定",
        "doctor.agents": "Agent",
        "doctor.item": "項目",
        "doctor.status": "状態",
        "doctor.details": "詳細",
        "doctor.ok": "正常",
        "doctor.warn": "警告",
        "doctor.fail": "異常",
        "doctor.found": "検出",
        "doctor.missing": "未検出",
        "doctor.profile": "profile",
        "doctor.command": "command",
        "doctor.active_sources": "有効ソース",
        "doctor.using_defaults": "組み込みデフォルトのみを使用中",
        "doctor.project_ignored": "この project は gperm の無視対象です",
        "doctor.no_issues": "致命的な問題は見つかりませんでした。",
        "import.wrote": "設定を取り込みました",
        "import.profile": "取り込み profile",
        "import.source": "入力ファイル",
        "import.target": "出力 config",
        "import.scope": "出力スコープ",
    },
}


class Translator:
    def __init__(self, locale: str) -> None:
        self.locale = locale if locale in MESSAGES else "en"

    @classmethod
    def from_env(cls, env: Mapping[str, str] | None = None) -> "Translator":
        return cls(detect_locale(env))

    def text(self, key: str) -> str:
        return MESSAGES.get(self.locale, MESSAGES["en"]).get(key, MESSAGES["en"].get(key, key))
