import Foundation

extension Notification.Name {
    static let appLanguageDidChange = Notification.Name("appLanguageDidChange")
}

enum AppLanguageManager {
    private static let defaultsKey = "settings.appLanguageCode"

    /// UI languages shown in Settings → Language (order preserved).
    private static let supportedLanguageCodes = ["en", "fr", "fr-CA", "es-ES", "es-MX", "de", "sv", "ko", "pt-BR", "ru", "zh-Hans", "zh-Hant"]

    /// `nil` follows the system language; otherwise uses the selected `.lproj` code.
    static var selectedLanguageCode: String? {
        get {
            guard UserDefaults.standard.object(forKey: defaultsKey) != nil else { return nil }
            let value = UserDefaults.standard.string(forKey: defaultsKey) ?? ""
            return value.isEmpty ? nil : value
        }
        set {
            if let code = newValue, code.isEmpty == false {
                UserDefaults.standard.set(code, forKey: defaultsKey)
            } else {
                UserDefaults.standard.removeObject(forKey: defaultsKey)
            }
        }
    }

    static var availableLanguageCodes: [String] {
        supportedLanguageCodes.filter { code in
            Bundle.main.path(forResource: code, ofType: "lproj") != nil
        }
    }

    static var localizationBundle: Bundle {
        guard let code = selectedLanguageCode,
              let path = Bundle.main.path(forResource: code, ofType: "lproj"),
              let bundle = Bundle(path: path) else {
            return .main
        }
        return bundle
    }

    static var formattingLocale: Locale {
        if let code = selectedLanguageCode {
            return Locale(identifier: code)
        }
        return .current
    }

    /// Localized product name (splash, share sheet, etc.).
    static var localizedAppDisplayName: String {
        "AI Companion for Meet".localized
    }

    static func displayName(for languageCode: String) -> String {
        switch languageCode {
        case "en":
            return "English"
        case "fr":
            return "Français"
        case "fr-CA":
            return "Français (Canada)"
        case "es-ES":
            return "Español (España)"
        case "es-MX":
            return "Español (México)"
        case "de":
            return "Deutsch"
        case "sv":
            return "Svenska"
        case "ko":
            return "한국어"
        case "pt-BR":
            return "Português (Brasil)"
        case "ru":
            return "Русский"
        case "zh-Hans":
            return "简体中文"
        case "zh-Hant":
            return "繁體中文"
        default:
            return Locale.current.localizedString(forIdentifier: languageCode) ?? languageCode
        }
    }

    static func setLanguage(code: String?) {
        let normalized = code?.trimmingCharacters(in: .whitespacesAndNewlines)
        let cleaned = (normalized?.isEmpty == false) ? normalized : nil
        guard cleaned != selectedLanguageCode else { return }
        selectedLanguageCode = cleaned
        NotificationCenter.default.post(name: .appLanguageDidChange, object: nil)
    }
}
