type Vars = Record<string, string | number>;

const translations: Record<"ko" | "en", Record<string, string>> = {
  ko: {
    "ui.admin.auth_guard.title": "인증 가드",
    "ui.admin.auth_guard.subtitle":
      "관리자 권한과 ReBAC 관계를 실제 정책 엔진 기준으로 확인합니다.",
    "ui.admin.auth_guard.checker.title": "ReBAC 권한 검증 도구",
    "ui.admin.auth_guard.checker.description":
      "특정 주체(Subject)가 특정 리소스(Object)에 대해 권한이 있는지 Ory Keto를 통해 실시간으로 확인합니다.",
    "ui.admin.auth_guard.checker.namespace.label": "네임스페이스",
    "ui.admin.auth_guard.checker.namespace.tenant": "테넌트",
    "ui.admin.auth_guard.checker.namespace.tenant_group": "테넌트 그룹",
    "ui.admin.auth_guard.checker.namespace.relying_party": "애플리케이션(RP)",
    "ui.admin.auth_guard.checker.namespace.system": "시스템",
    "ui.admin.auth_guard.checker.relation": "관계",
    "ui.admin.auth_guard.checker.object_id": "대상 ID",
    "ui.admin.auth_guard.checker.subject": "주체 (User:ID)",
    "ui.admin.auth_guard.checker.check": "권한 확인 실행",
    "ui.admin.auth_guard.checker.checking": "검증 중...",
    "ui.admin.auth_guard.checker.allowed": "접근 허용",
    "ui.admin.auth_guard.checker.allowed_description":
      "해당 사용자는 요청한 리소스에 대해 권한이 있습니다. (상속 포함)",
    "ui.admin.auth_guard.checker.denied": "접근 거부",
    "ui.admin.auth_guard.checker.denied_description":
      "해당 사용자는 요청한 리소스에 대해 권한이 없습니다.",
    "ui.admin.integrity.check.duplicate_tenant_slugs.title": "중복 테넌트 slug",
    "ui.admin.integrity.section.tenant_integrity": "테넌트 정합성",
    "ui.admin.integrity.section.user_integrity": "사용자 정합성",
    "ui.admin.integrity.title": "데이터 정합성 검증",
    "msg.admin.integrity.subtitle":
      "정합성 상태를 확인하고 데이터 모델 전반의 검증 결과를 살펴봅니다.",
    "msg.admin.integrity.section.tenant_integrity.description":
      "테넌트 slug 중복과 부모 관계 이상을 확인합니다.",
    "msg.admin.integrity.section.user_integrity.description":
      "사용자와 로그인 ID 참조의 고아 레코드를 확인합니다.",
    "msg.admin.audit.registry.description":
      "최근 감사 로그를 검색 조건에 맞춰 필터링하고, 작업 이력을 빠르게 확인합니다.",
    "ui.admin.integrity.recheck.run": "다시 검사",
    "ui.admin.integrity.recheck.running": "검사 중",
    "ui.admin.integrity.status.fail": "실패",
    "ui.admin.integrity.status.pass": "정상",
    "ui.admin.integrity.status.warning": "주의",
    "ui.admin.integrity.orphan_login_ids.title": "유령 로그인 ID 정리",
    "ui.admin.integrity.forbidden.title": "접근 권한이 없습니다",
    "ui.admin.integrity.summary.title": "정합성 최종 검증",
    "ui.admin.integrity.tab_ory_ssot": "Ory SSOT 시스템",
    "ui.admin.ory_ssot.actions.flush_identity_cache": "Redis cache flush",
    "ui.admin.ory_ssot.cache_card.description":
      "Kratos identity 목록 및 조회 작업을 위한 Redis mirror/cache 상태입니다.",
    "ui.admin.ory_ssot.cache_card.title": "Redis identity cache",
    "ui.admin.ory_ssot.forbidden.title": "접근 권한이 없습니다",
    "ui.admin.ory_ssot.loading": "불러오는 중",
    "ui.admin.ory_ssot.projection_card.description":
      "관리자 검색과 통계에서 사용하는 PostgreSQL read model 상태입니다.",
    "ui.admin.ory_ssot.projection_card.title": "Backend 사용자 read model",
    "ui.admin.ory_ssot.status.failed": "실패",
    "ui.admin.ory_ssot.status.not_ready": "준비되지 않음",
    "ui.admin.ory_ssot.status.ready": "준비됨",
    "ui.admin.ory_ssot.summary.cache_keys": "Cache keys",
    "ui.admin.ory_ssot.summary.last_refreshed": "마지막 refresh",
    "ui.admin.ory_ssot.summary.last_synced": "마지막 read-model refresh",
    "ui.admin.ory_ssot.summary.local_users": "Local users",
    "ui.admin.ory_ssot.summary.observed_identities": "관측 identity",
    "ui.admin.ory_ssot.summary.status": "상태",
    "ui.admin.ory_ssot.summary.updated_at": "상태 갱신",
    "ui.admin.ory_ssot.title": "Ory SSOT 시스템",
    "msg.admin.ory_ssot.flush_confirm":
      "Redis identity cache 키만 비우시겠습니까?",
    "msg.admin.ory_ssot.flush_error":
      "Redis identity cache flush에 실패했습니다.",
    "msg.admin.ory_ssot.flush_success":
      "Redis identity cache key {{count}}개를 비웠습니다.",
    "msg.admin.ory_ssot.forbidden.description":
      "이 화면은 super_admin 권한으로만 접근할 수 있습니다.",
    "msg.admin.ory_ssot.load_error":
      "Ory SSOT 시스템 상태를 불러오지 못했습니다.",
    "msg.admin.ory_ssot.subtitle":
      "Kratos 원장과 Redis identity cache 상태를 분리해서 확인합니다.",
    "msg.admin.users.list.subtitle": "시스템 사용자를 조회하고 관리합니다.",
    "msg.admin.users.list.registry.count":
      "총 {{count}}명의 사용자가 등록되어 있습니다.",
    "msg.admin.integrity.check.duplicate_tenant_slugs.description":
      "삭제되지 않은 tenant의 LOWER(TRIM(slug)) 기준 중복을 검사합니다.",
    "msg.admin.integrity.check.orphan_tenant_parents.description":
      "tenants.parent_id가 존재하지 않거나 soft-deleted tenant를 참조하는지 검사합니다.",
    "msg.admin.integrity.check.orphan_user_login_id_tenants.description":
      "user_login_ids.tenant_id가 존재하지 않거나 soft-deleted tenant를 참조하는지 검사합니다.",
    "msg.admin.integrity.check.orphan_user_login_id_users.description":
      "user_login_ids.user_id가 존재하지 않거나 soft-deleted user를 참조하는지 검사합니다.",
    "msg.admin.integrity.check.orphan_user_tenant_memberships.description":
      "users.tenant_id가 존재하지 않거나 soft-deleted tenant를 참조하는지 검사합니다.",
    "msg.admin.integrity.recheck.running": "정합성 검사를 실행 중입니다.",
    "msg.admin.integrity.recheck.success": "검사가 완료되었습니다.",
  },
  en: {
    "ui.admin.auth_guard.title": "Auth Guard",
    "ui.admin.auth_guard.subtitle":
      "Verify admin privileges and ReBAC relationships against the policy engine.",
    "ui.admin.auth_guard.checker.title": "ReBAC permission checker",
    "ui.admin.auth_guard.checker.description":
      "Check in real time whether a subject has access to a resource through Ory Keto.",
    "ui.admin.auth_guard.checker.namespace.label": "Namespace",
    "ui.admin.auth_guard.checker.namespace.tenant": "Tenant",
    "ui.admin.auth_guard.checker.namespace.tenant_group": "TenantGroup",
    "ui.admin.auth_guard.checker.namespace.relying_party": "RelyingParty",
    "ui.admin.auth_guard.checker.namespace.system": "System",
    "ui.admin.auth_guard.checker.relation": "Relation",
    "ui.admin.auth_guard.checker.object_id": "Object ID",
    "ui.admin.auth_guard.checker.subject": "Subject (User:ID)",
    "ui.admin.auth_guard.checker.check": "Check permission",
    "ui.admin.auth_guard.checker.checking": "Checking...",
    "ui.admin.auth_guard.checker.allowed": "Access ALLOWED",
    "ui.admin.auth_guard.checker.allowed_description":
      "The subject has access to the requested resource, including inherited permissions.",
    "ui.admin.auth_guard.checker.denied": "Access DENIED",
    "ui.admin.auth_guard.checker.denied_description":
      "The subject does not have access to the requested resource.",
    "ui.admin.integrity.check.duplicate_tenant_slugs.title":
      "Duplicate tenant slug",
    "ui.admin.integrity.section.tenant_integrity": "Tenant integrity",
    "ui.admin.integrity.section.user_integrity": "User integrity",
    "ui.admin.integrity.title": "Data Integrity Check",
    "msg.admin.integrity.subtitle":
      "Review integrity status and inspect checks across the admin data model.",
    "msg.admin.integrity.section.tenant_integrity.description":
      "Checks duplicate tenant slugs and orphan parent relationships.",
    "msg.admin.integrity.section.user_integrity.description":
      "Checks orphan records for users and login ID references.",
    "msg.admin.audit.registry.description":
      "Filter recent audit logs by search criteria and review action history quickly.",
    "ui.admin.integrity.recheck.run": "Run again",
    "ui.admin.integrity.recheck.running": "Checking",
    "ui.admin.integrity.status.fail": "Failed",
    "ui.admin.integrity.status.pass": "Passed",
    "ui.admin.integrity.status.warning": "Warning",
    "ui.admin.integrity.orphan_login_ids.title": "Orphan Login ID Cleanup",
    "ui.admin.integrity.forbidden.title": "Access denied",
    "ui.admin.integrity.summary.title": "Final integrity check",
    "ui.admin.integrity.tab_ory_ssot": "Ory SSOT System",
    "ui.admin.ory_ssot.actions.flush_identity_cache": "Redis cache flush",
    "ui.admin.ory_ssot.cache_card.description":
      "Redis mirror/cache status for Kratos identity list and lookup operations.",
    "ui.admin.ory_ssot.cache_card.title": "Redis identity cache",
    "ui.admin.ory_ssot.forbidden.title": "Access denied",
    "ui.admin.ory_ssot.loading": "Loading",
    "ui.admin.ory_ssot.projection_card.description":
      "PostgreSQL read model status used by admin search and statistics.",
    "ui.admin.ory_ssot.projection_card.title": "Backend user read model",
    "ui.admin.ory_ssot.status.failed": "failed",
    "ui.admin.ory_ssot.status.not_ready": "not ready",
    "ui.admin.ory_ssot.status.ready": "ready",
    "ui.admin.ory_ssot.summary.cache_keys": "Cache keys",
    "ui.admin.ory_ssot.summary.last_refreshed": "Last refreshed",
    "ui.admin.ory_ssot.summary.last_synced": "Last read-model refresh",
    "ui.admin.ory_ssot.summary.local_users": "Local users",
    "ui.admin.ory_ssot.summary.observed_identities": "Observed identities",
    "ui.admin.ory_ssot.summary.status": "Status",
    "ui.admin.ory_ssot.summary.updated_at": "Updated at",
    "ui.admin.ory_ssot.title": "Ory SSOT System",
    "msg.admin.ory_ssot.flush_confirm": "Flush only Redis identity cache keys?",
    "msg.admin.ory_ssot.flush_error": "Redis identity cache flush failed.",
    "msg.admin.ory_ssot.flush_success":
      "Flushed {{count}} Redis identity cache keys.",
    "msg.admin.ory_ssot.forbidden.description":
      "This screen is only available to super_admin users.",
    "msg.admin.ory_ssot.load_error": "Failed to load Ory SSOT system status.",
    "msg.admin.ory_ssot.subtitle":
      "Review Kratos source-of-truth and Redis identity cache status separately.",
    "msg.admin.users.list.subtitle":
      "Search and manage users registered in the current tenant.",
    "msg.admin.users.list.registry.count": "{{count}} users loaded.",
    "msg.admin.integrity.check.duplicate_tenant_slugs.description":
      "Checks duplicate active tenant slugs using LOWER(TRIM(slug)).",
    "msg.admin.integrity.check.orphan_tenant_parents.description":
      "Checks whether tenants.parent_id points to a missing or soft-deleted tenant.",
    "msg.admin.integrity.check.orphan_user_login_id_tenants.description":
      "Checks whether user_login_ids.tenant_id points to a missing or soft-deleted tenant.",
    "msg.admin.integrity.check.orphan_user_login_id_users.description":
      "Checks whether user_login_ids.user_id points to a missing or soft-deleted user.",
    "msg.admin.integrity.check.orphan_user_tenant_memberships.description":
      "Checks whether users.tenant_id points to a missing or soft-deleted tenant.",
    "msg.admin.integrity.recheck.running": "Running integrity check.",
    "msg.admin.integrity.recheck.success": "Check completed.",
  },
};

function format(template: string, vars?: Vars) {
  if (!vars) {
    return template;
  }
  return template.replace(/\{\{\s*(\w+)\s*\}\}/g, (match, key) => {
    const value = vars[key];
    return value === undefined || value === null ? match : String(value);
  });
}

export function createI18nMock() {
  return {
    t(key: string, fallback?: string, vars?: Vars) {
      const locale =
        window.localStorage.getItem("locale") === "en" ? "en" : "ko";
      const template = translations[locale][key] ?? fallback ?? key;
      return format(template, vars);
    },
  };
}
