// SPDX-License-Identifier: Apache-2.0
//! Minimal i18n: a static key/value table over a few supported languages.
//! Mockup-scale; replace with a proper i18n crate (e.g. `fluent`) before
//! production.

use leptos::prelude::*;

use crate::state::AppState;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Lang {
    En,
    Ja,
}

impl Lang {
    #[must_use]
    pub fn code(self) -> &'static str {
        match self {
            Self::En => "en",
            Self::Ja => "ja",
        }
    }

    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::En => "EN",
            Self::Ja => "日本語",
        }
    }
}

/// Look up a translation key for the current language.
///
/// Falls back to English if the key is not found in the target language,
/// then to a placeholder marker `"???"` so missing keys are obvious in dev.
#[must_use]
pub fn t(key: &str) -> &'static str {
    let lang = AppState::get().lang.get();
    lookup(lang, key).unwrap_or_else(|| lookup(Lang::En, key).unwrap_or("???"))
}

fn lookup(lang: Lang, key: &str) -> Option<&'static str> {
    match (lang, key) {
        // Brand / common
        (_, "brand") => Some("hejpics"),
        (Lang::En, "nav.library") => Some("Library"),
        (Lang::Ja, "nav.library") => Some("ライブラリ"),
        (Lang::En, "nav.shared") => Some("Shared"),
        (Lang::Ja, "nav.shared") => Some("共有"),
        (Lang::En, "nav.add") => Some("Add"),
        (Lang::Ja, "nav.add") => Some("追加"),
        (Lang::En, "nav.sharing") => Some("Sharing"),
        (Lang::Ja, "nav.sharing") => Some("共有管理"),
        (Lang::En, "nav.me") => Some("Me"),
        (Lang::Ja, "nav.me") => Some("アカウント"),
        // Keep old keys as aliases during transition
        (Lang::En, "nav.home") => Some("Library"),
        (Lang::Ja, "nav.home") => Some("ライブラリ"),
        (Lang::En, "nav.albums") => Some("Library"),
        (Lang::Ja, "nav.albums") => Some("ライブラリ"),
        (Lang::En, "nav.upload") => Some("Add"),
        (Lang::Ja, "nav.upload") => Some("追加"),
        (Lang::En, "nav.data") => Some("Me"),
        (Lang::Ja, "nav.data") => Some("アカウント"),
        (Lang::En, "nav.settings") => Some("Me"),
        (Lang::Ja, "nav.settings") => Some("アカウント"),
        (Lang::En, "privacy.you") => Some("Only you"),
        (Lang::Ja, "privacy.you") => Some("あなただけ"),
        (Lang::En, "privacy.shared_link") => Some("Shared by private link"),
        (Lang::Ja, "privacy.shared_link") => Some("リンクで共有中"),
        (Lang::En, "privacy.shared_family") => Some("Shared with Family key"),
        (Lang::Ja, "privacy.shared_family") => Some("家族の鍵で共有中"),
        (Lang::En, "privacy.shared_invited") => Some("Shared with invited viewers"),
        (Lang::Ja, "privacy.shared_invited") => Some("招待した相手と共有中"),
        (Lang::En, "trust.ai_non_use") => Some("Not used for AI training"),
        (Lang::Ja, "trust.ai_non_use") => Some("AI 学習に利用されません"),
        (Lang::En, "trust.private_default") => Some("Private by default"),
        (Lang::Ja, "trust.private_default") => Some("既定は非公開"),
        (Lang::En, "common.back") => Some("Back"),
        (Lang::Ja, "common.back") => Some("戻る"),
        (Lang::En, "common.cancel") => Some("Cancel"),
        (Lang::Ja, "common.cancel") => Some("キャンセル"),
        (Lang::En, "common.continue") => Some("Continue"),
        (Lang::Ja, "common.continue") => Some("次へ"),
        (Lang::En, "common.done") => Some("Done"),
        (Lang::Ja, "common.done") => Some("完了"),
        _ => None,
    }
}
