import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";

export type Lang = "bn" | "en";

const STORAGE_KEY = "cv-lang";

type Dict = Record<string, string>;

const bn: Dict = {
  "brand.name": "কার্ডিং ভাই টপ আপ",
  "brand.tag": "ফ্রি ফায়ার ডায়মন্ড টপ আপ",
  "badge.manual": "ম্যানুয়াল",
  "hero.title.a": "ফ্রি ফায়ার",
  "hero.title.b": "ডায়মন্ড টপ আপ",
  "hero.title.c": "ইউআইডি দিয়ে",
  "hero.sub":
    "প্যাকেজ বাছুন, বিকাশ বা নগদে পেমেন্ট করুন, তারপর ট্রানজেকশন আইডি ও ইউআইডি দিন। কোনো অ্যাকাউন্ট লাগবে না।",
  "chip.review": "ম্যানুয়াল রিভিউ",
  "chip.packs": "৫০টি প্যাকেজ",
  "chip.track": "অর্ডার ট্র্যাকিং আইডি",
  "step1.title": "আপনার প্যাকেজ বাছুন",
  "step1.hint": "মূল্য টাকায়",
  "step2.title": "পেমেন্ট পাঠান",
  "step2.hint": "নম্বর কপি করে পেমেন্ট করুন",
  "step3.title": "অর্ডার নিশ্চিত করুন",
  "step3.hint": "ট্রানজেকশন আইডি ও ফ্রি ফায়ার ইউআইডি দিতে হবে",
  "packs.heading": "ডায়মন্ড প্যাকেজ",
  "packs.diamonds": "ডায়মন্ড",
  "packs.group": "ডায়মন্ড প্যাকেজসমূহ",
  "pay.group": "পেমেন্ট মাধ্যম",
  "pay.sendMoney": "সেন্ড মানি",
  "pay.personal": "পার্সোনাল",
  "pay.sendTo": "এই {name} নম্বরে সেন্ড মানি করুন",
  "pay.copy": "কপি",
  "pay.copied": "কপি হয়েছে",
  "pay.copyAria": "{name} নম্বর কপি করুন",
  "pay.copiedLive": "নম্বর কপি হয়েছে",
  "pay.sendMoneyAlt": "মোবাইল ওয়ালেট অ্যাপে সেন্ড মানি অপশন",
  "pay.exact": "ঠিক {amount} পাঠান — অন্য পরিমাণে অর্ডার দেরি হতে পারে।",
  "pay.step1": "অ্যাপ খুলে সেন্ড মানি অপশনে যান।",
  "pay.step2": "উপরের নম্বরে সঠিক পরিমাণ পাঠান।",
  "pay.step3": "কনফার্মেশন থেকে ট্রানজেকশন আইডি (TrxID) কপি করুন।",
  "field.trx": "ট্রানজেকশন আইডি (TrxID)",
  "field.trx.hint": "পেমেন্ট কনফার্মেশনের ৬–২০ অক্ষর ও সংখ্যা।",
  "field.uid": "ফ্রি ফায়ার ইউআইডি",
  "field.uid.hint": "৬–১২ সংখ্যা, গেম প্রোফাইলে দেখা যায়।",
  "summary.title": "অর্ডার সারসংক্ষেপ",
  "summary.package": "প্যাকেজ",
  "summary.payment": "পেমেন্ট",
  "summary.uid": "ইউআইডি",
  "summary.total": "সর্বমোট",
  "summary.none": "নির্বাচন করা হয়নি",
  "submit": "অর্ডার সাবমিট করুন",
  "submitting": "সাবমিট হচ্ছে…",
  "submit.note": "সব অর্ডার ম্যানুয়ালি রিভিউ হয় এবং রিভিউ পর্যন্ত পেন্ডিং থাকে।",
  "err.pack": "আগে একটি প্যাকেজ বাছুন।",
  "err.trx.required": "ট্রানজেকশন আইডি দিতে হবে।",
  "err.trx.chars": "ট্রানজেকশন আইডিতে শুধু অক্ষর ও সংখ্যা থাকবে।",
  "err.trx.len": "ট্রানজেকশন আইডি ৬–২০ অক্ষরের হতে হবে।",
  "err.uid.required": "ফ্রি ফায়ার ইউআইডি দিতে হবে।",
  "err.uid.digits": "ইউআইডিতে শুধু সংখ্যা থাকবে।",
  "err.uid.len": "ইউআইডি ৬–১২ সংখ্যার হতে হবে।",
  "ok.title": "অর্ডার পেয়েছি — পেন্ডিং",
  "ok.sub":
    "আপনার অর্ডার ম্যানুয়াল রিভিউয়ের জন্য জমা হয়েছে। আমাদের টিম পেমেন্ট যাচাই করা পর্যন্ত এটি পেন্ডিং থাকবে। ফলো-আপের জন্য অর্ডার আইডি রাখুন।",
  "ok.orderId": "অর্ডার আইডি",
  "ok.copyAria": "অর্ডার আইডি কপি করুন",
  "ok.paidWith": "পেমেন্ট করা হয়েছে",
  "ok.status": "স্ট্যাটাস",
  "ok.statusValue": "রিভিউ পেন্ডিং",
  "ok.note":
    "এই কনফার্মেশন শুধু আপনি যা জমা দিয়েছেন তা দেখায়। এটি ডেলিভারি বা পেমেন্ট নিশ্চিতকরণ নয়।",
  "ok.again": "আরেকটি অর্ডার করুন",
  "support.title": "সাপোর্ট",
  "support.hint": "যেকোনো সমস্যায় টেলিগ্রামে যোগাযোগ করুন",
  "support.channel": "টেলিগ্রাম চ্যানেল",
  "support.channel.sub": "অফার ও আপডেট",
  "support.admin": "টেলিগ্রাম সাপোর্ট",
  "support.admin.sub": "অর্ডার সংক্রান্ত সহায়তা",
  "support.pending": "লিংক এখনো যোগ করা হয়নি",
  "lang.aria": "ভাষা",
  "footer.a": "কার্ডিং ভাই টপ আপ — ম্যানুয়াল ফ্রি ফায়ার টপ-আপ সার্ভিস।",
  "footer.b": "গারেনা বা ফ্রি ফায়ারের সাথে সম্পর্কিত নয়।",
  "account.title": "আমার অ্যাকাউন্ট",
  "account.aria": "অ্যাকাউন্ট ও ব্যালেন্স",
  "account.close": "বন্ধ করুন",
  "account.guest": "স্বয়ংক্রিয় অ্যাকাউন্ট",
  "account.balance": "ব্যালেন্স",
  "account.signInShort": "অ্যাকাউন্ট",
  "account.id": "অ্যাকাউন্ট আইডি",
  "account.autoNote":
    "আপনার অ্যাকাউন্ট স্বয়ংক্রিয়ভাবে তৈরি হয়েছে। টেলিগ্রাম যুক্ত করলে ব্যালেন্স ও অর্ডার সবসময় একই অ্যাকাউন্টে থাকবে।",
  "account.tab.profile": "প্রোফাইল",
  "account.tab.balance": "ব্যালেন্স",
  "account.tab.orders": "অর্ডার",
  "account.tab.requests": "রিকোয়েস্ট",
  "account.tab.money": "জমা ও খরচ",
  "account.tab.bonus": "বোনাস",
  "account.tg.hint": "আপনার টেলিগ্রাম ইউজারনেম দিন — পাসওয়ার্ড বা ওটিপি কখনো চাওয়া হবে না।",
  "account.tg.label": "টেলিগ্রাম ইউজারনেম",
  "account.tg.save": "সংযুক্ত করুন",
  "account.tg.saved": "টেলিগ্রাম সংযুক্ত হয়েছে",
  "account.tg.taken": "এই টেলিগ্রাম ইউজারনেম অন্য অ্যাকাউন্টে যুক্ত আছে।",
  "account.tg.connected": "সংযুক্ত:",
  "account.tg.notConnected": "টেলিগ্রাম এখনো সংযুক্ত নয়",
  "account.deposits": "জমা ও বোনাস",
  "account.spending": "খরচ",
  "account.empty.tx": "এখনো কোনো লেনদেন নেই।",
  "account.empty.orders": "এখনো কোনো অর্ডার নেই।",
  "account.empty.requests": "কোনো পেন্ডিং রিকোয়েস্ট নেই।",
  "account.empty.deposits": "এখনো কোনো জমা নেই।",
  "account.empty.spending": "এখনো কোনো খরচ নেই।",
  "account.empty.bonus": "এখনো কোনো বোনাস নেই।",
  "account.type.DAILY_BONUS": "দৈনিক বোনাস",
  "account.type.DEPOSIT": "জমা",
  "account.type.ORDER": "অর্ডার",
  "account.type.SPEND": "খরচ",
  "account.status.PENDING": "পেন্ডিং",
  "account.status.PROCESSING": "প্রসেসিং",
  "account.status.COMPLETED": "সম্পন্ন",
  "account.status.CANCELLED": "বাতিল",
  "account.ledger.before": "আগে",
  "account.ledger.after": "পরে",
  "auth.hint": "চাইলে ইমেইল যোগ করে অ্যাকাউন্টটি স্থায়ীভাবে সুরক্ষিত করুন।",
  "auth.email": "ইমেইল",
  "auth.password": "পাসওয়ার্ড",
  "auth.signIn": "সাইন ইন",
  "auth.register": "অ্যাকাউন্ট তৈরি করুন",
  "auth.welcome": "স্বাগতম",
  "auth.welcomeSub": "আপনার কার্ডিং ভাই অ্যাকাউন্টে সাইন ইন করুন",
  "auth.registerSub": "আপনার কার্ডিং ভাই অ্যাকাউন্ট তৈরি করুন",
  "auth.name": "আপনার নাম লিখুন",
  "auth.identifier": "ইমেইল / অ্যাকাউন্ট",
  "auth.newPassword": "নতুন পাসওয়ার্ড দিন",
  "auth.confirmPassword": "পাসওয়ার্ড আবার দিন",
  "auth.referral": "রেফারেল কোড",
  "auth.showPass": "পাসওয়ার্ড দেখুন",
  "auth.hidePass": "পাসওয়ার্ড লুকান",
  "auth.err.email": "ইমেইল দিতে হবে।",
  "auth.err.password": "পাসওয়ার্ড কমপক্ষে ৬ অক্ষরের হতে হবে।",
  "auth.err.name": "আপনার নাম দিতে হবে।",
  "auth.err.match": "দুইটি পাসওয়ার্ড মিলছে না।",
  "auth.created.title": "অ্যাকাউন্ট সফলভাবে তৈরি হয়েছে",
  "auth.continue": "চালিয়ে যান",
  "auth.signUp": "ইমেইল যোগ করুন",
  "auth.toSignUp": "অ্যাকাউন্ট নেই? অ্যাকাউন্ট তৈরি করুন",
  "auth.toSignIn": "আগের অ্যাকাউন্টে সাইন ইন করুন",
  "auth.confirm": "ইমেইল চেক করে কনফার্ম লিংকে ক্লিক করুন।",
  "auth.signOut": "লগ আউট",
  "account.stat.orders": "মোট অর্ডার",
  "account.stat.pending": "পেন্ডিং",
  "account.stat.completed": "সম্পন্ন",
  "account.stat.spending": "মোট খরচ",
  "account.stat.deposits": "মোট জমা",
  "account.stat.bonus": "বোনাস",
  "account.stat.transactions": "মোট লেনদেন",
  "account.details": "অ্যাকাউন্টের তথ্য",
  "account.fullName": "পূর্ণ নাম",
  "account.regDate": "রেজিস্ট্রেশনের তারিখ",
  "account.lastLogin": "সর্বশেষ লগইন",
  "account.balanceAria": "ব্যালেন্স ও অ্যাকাউন্ট খুলুন",
  "account.bonus.txid": "ট্রানজেকশন আইডি",
  "account.status.CREDITED": "যোগ হয়েছে",
  "account.type.ADMIN_ADD": "অ্যাডমিন যোগ",
  "account.type.ADMIN_DEDUCT": "অ্যাডমিন কর্তন",
  "account.type.ADMIN_ADJUST": "অ্যাডমিন সমন্বয়",
  "bonus.aria": "দৈনিক বোনাস প্রজাপতি ধরুন",
  "bonus.win.title": "অভিনন্দন! বোনাস পেয়েছেন",
  "bonus.win.sub": "আপনার ব্যালেন্সে যোগ হয়েছে। আগামীকাল আবার প্রজাপতি আসবে।",
  "bonus.win.ok": "দারুণ!",
};

const en: Dict = {
  "brand.name": "CARDING VAI TOP UP",
  "brand.tag": "Free Fire Diamond Top Up",
  "badge.manual": "Manual",
  "hero.title.a": "Free Fire",
  "hero.title.b": "Diamond Top Up",
  "hero.title.c": "by UID",
  "hero.sub":
    "Pick a package, pay with bKash or Nagad, then submit your transaction ID and UID. No account needed.",
  "chip.review": "Manual review",
  "chip.packs": "50 packages",
  "chip.track": "Order tracking ID",
  "step1.title": "Choose your package",
  "step1.hint": "Prices in BDT",
  "step2.title": "Send the payment",
  "step2.hint": "Copy the number and pay",
  "step3.title": "Confirm your order",
  "step3.hint": "Transaction ID and Free Fire UID are required",
  "packs.heading": "Diamond packages",
  "packs.diamonds": "Diamonds",
  "packs.group": "Diamond packages",
  "pay.group": "Payment method",
  "pay.sendMoney": "Send Money",
  "pay.personal": "Personal",
  "pay.sendTo": "Send Money to this {name} number",
  "pay.copy": "Copy",
  "pay.copied": "Copied",
  "pay.copyAria": "Copy {name} number",
  "pay.copiedLive": "Number copied to clipboard",
  "pay.sendMoneyAlt": "Send Money option in the mobile wallet app",
  "pay.exact": "Send exactly {amount} — a different amount can delay your order.",
  "pay.step1": "Open the app and tap Send Money.",
  "pay.step2": "Send the exact amount to the number above.",
  "pay.step3": "Copy the Transaction ID (TrxID) from the confirmation.",
  "field.trx": "Transaction ID (TrxID)",
  "field.trx.hint": "6–20 letters and numbers from your payment confirmation.",
  "field.uid": "Free Fire UID",
  "field.uid.hint": "6–12 digits, shown on your in-game profile.",
  "summary.title": "Order summary",
  "summary.package": "Package",
  "summary.payment": "Payment",
  "summary.uid": "UID",
  "summary.total": "Total",
  "summary.none": "Not selected",
  submit: "Submit order",
  submitting: "Submitting…",
  "submit.note": "Orders are reviewed manually and stay pending until checked.",
  "err.pack": "Choose a package first.",
  "err.trx.required": "Transaction ID is required.",
  "err.trx.chars": "Transaction ID must be letters and numbers only.",
  "err.trx.len": "Transaction ID must be 6–20 characters.",
  "err.uid.required": "Free Fire UID is required.",
  "err.uid.digits": "UID must contain digits only.",
  "err.uid.len": "UID must be 6–12 digits.",
  "ok.title": "Order received — pending",
  "ok.sub":
    "Your order is queued for manual review. It stays pending until our team checks your payment. Keep your order ID for follow-up.",
  "ok.orderId": "Order ID",
  "ok.copyAria": "Copy order ID",
  "ok.paidWith": "Paid with",
  "ok.status": "Status",
  "ok.statusValue": "Pending review",
  "ok.note":
    "This confirmation records what you submitted. It is not a delivery or payment confirmation.",
  "ok.again": "Place another order",
  "support.title": "Support",
  "support.hint": "Reach us on Telegram for any issue",
  "support.channel": "Telegram Channel",
  "support.channel.sub": "Offers and updates",
  "support.admin": "Telegram Support",
  "support.admin.sub": "Help with your order",
  "support.pending": "Link not added yet",
  "lang.aria": "Language",
  "footer.a": "CARDING VAI TOP UP — manual Free Fire top-up service.",
  "footer.b": "Not affiliated with Garena or Free Fire.",
  "account.title": "My account",
  "account.aria": "Account and balance",
  "account.close": "Close",
  "account.guest": "Automatic account",
  "account.balance": "Balance",
  "account.signInShort": "Account",
  "account.id": "Account ID",
  "account.autoNote":
    "Your account was created automatically. Connect Telegram so your balance and orders always stay on the same account.",
  "account.tab.profile": "Profile",
  "account.tab.balance": "Balance",
  "account.tab.orders": "Orders",
  "account.tab.requests": "Requests",
  "account.tab.money": "Deposits & spending",
  "account.tab.bonus": "Bonus",
  "account.tg.hint": "Add your Telegram username — we never ask for a password or 2FA code.",
  "account.tg.label": "Telegram username",
  "account.tg.save": "Connect",
  "account.tg.saved": "Telegram connected",
  "account.tg.taken": "This Telegram username is linked to another account.",
  "account.tg.connected": "Connected:",
  "account.tg.notConnected": "Telegram not connected yet",
  "account.deposits": "Deposits & bonuses",
  "account.spending": "Spending",
  "account.empty.tx": "No transactions yet.",
  "account.empty.orders": "No orders yet.",
  "account.empty.requests": "No pending requests.",
  "account.empty.deposits": "No deposits yet.",
  "account.empty.spending": "No spending yet.",
  "account.empty.bonus": "No bonuses yet.",
  "account.type.DAILY_BONUS": "Daily bonus",
  "account.type.DEPOSIT": "Deposit",
  "account.type.ORDER": "Order",
  "account.type.SPEND": "Spending",
  "account.status.PENDING": "PENDING",
  "account.status.PROCESSING": "PROCESSING",
  "account.status.COMPLETED": "COMPLETED",
  "account.status.CANCELLED": "CANCELLED",
  "account.ledger.before": "Before",
  "account.ledger.after": "After",
  "auth.hint": "Optionally add an email to secure this account permanently.",
  "auth.email": "Email",
  "auth.password": "Password",
  "auth.signIn": "Sign in",
  "auth.register": "Create account",
  "auth.welcome": "Welcome back",
  "auth.welcomeSub": "Sign in to your CARDING VAI account",
  "auth.registerSub": "Create your CARDING VAI account",
  "auth.name": "Enter your name",
  "auth.identifier": "Email / Account",
  "auth.newPassword": "Create a new password",
  "auth.confirmPassword": "Confirm your password",
  "auth.referral": "Referral code",
  "auth.showPass": "Show password",
  "auth.hidePass": "Hide password",
  "auth.err.email": "Email is required.",
  "auth.err.password": "Password must be at least 6 characters.",
  "auth.err.name": "Your name is required.",
  "auth.err.match": "Passwords do not match.",
  "auth.created.title": "Account created successfully",
  "auth.continue": "Continue",
  "auth.signUp": "Add email",
  "auth.toSignUp": "Don't have an account? Create account",
  "auth.toSignIn": "Already have an account? Sign in",
  "auth.confirm": "Check your email and click the confirmation link.",
  "auth.signOut": "Log out",
  "account.stat.orders": "Total orders",
  "account.stat.pending": "Pending",
  "account.stat.completed": "Completed",
  "account.stat.spending": "Total spending",
  "account.stat.deposits": "Total deposits",
  "account.stat.bonus": "Bonuses received",
  "account.stat.transactions": "Total transactions",
  "account.details": "Account details",
  "account.fullName": "Full name",
  "account.regDate": "Registration date",
  "account.lastLogin": "Last login",
  "account.balanceAria": "Open balance and account",
  "account.bonus.txid": "Transaction ID",
  "account.status.CREDITED": "Credited",
  "account.type.ADMIN_ADD": "Admin credit",
  "account.type.ADMIN_DEDUCT": "Admin deduction",
  "account.type.ADMIN_ADJUST": "Admin adjustment",
  "bonus.aria": "Catch the daily bonus butterfly",
  "bonus.win.title": "Congratulations! Bonus claimed",
  "bonus.win.sub": "It has been added to your balance. The butterfly returns tomorrow.",
  "bonus.win.ok": "Awesome!",
};

const dicts: Record<Lang, Dict> = { bn, en };

export type Translate = (key: string, vars?: Record<string, string>) => string;

const I18nContext = createContext<{ lang: Lang; setLang: (l: Lang) => void; t: Translate }>({
  lang: "bn",
  setLang: () => {},
  t: (k) => k,
});

export function I18nProvider({ children }: { children: React.ReactNode }) {
  const [lang, setLangState] = useState<Lang>("bn");

  useEffect(() => {
    const stored = window.localStorage.getItem(STORAGE_KEY);
    if (stored === "bn" || stored === "en") setLangState(stored);
  }, []);

  const setLang = useCallback((next: Lang) => {
    setLangState(next);
    try {
      window.localStorage.setItem(STORAGE_KEY, next);
    } catch {
      /* storage unavailable */
    }
  }, []);

  const t = useCallback<Translate>(
    (key, vars) => {
      const raw = dicts[lang][key] ?? dicts.en[key] ?? key;
      if (!vars) return raw;
      return Object.entries(vars).reduce((s, [k, v]) => s.replaceAll(`{${k}}`, v), raw);
    },
    [lang],
  );

  const value = useMemo(() => ({ lang, setLang, t }), [lang, setLang, t]);
  return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;
}

export function useI18n() {
  return useContext(I18nContext);
}
