/// 上线配置（密钥由 Jason 回来后填入，切勿把 Secret Key 提交进仓库）
/// © 2026 Jason Li
library;

import 'package:flutter/foundation.dart';

import '../l10n/l10n.dart';
import '../product/tiers.dart';
import '../product/legal_links.dart';

class AppConfig {
  static const appName = 'Key Resonance Destiny Analysis';
  static const appNameZh = '键共振命理';
  /// 笔名
  static const penNameZh = '泽森';
  static const penNameEn = 'Zeisen';
  /// 英文名（产品唯一法律署名；不用中文法定名）
  static const developerLegalName = 'Jason Li';
  static const copyright = '© 2026 Jason Li. All rights reserved.';
  static const supportEmail = 'jasonli1203@icloud.com'; // 可改
  static const websiteUrl = String.fromEnvironment(
    'KR_WEBSITE',
    defaultValue: 'https://keyresonance.com',
  );
  /// 由 [LegalLinks] 解析：Web 用同源 /legal；未配域名时走 assets / 应用内页
  static String get privacyUrl => LegalLinks.privacyUrl;
  static String get termsUrl => LegalLinks.termsUrl;


  /// 作者简介（短）— 多语见 [L10n.authorBio]；以事实为准，不作夸饰
  static const authorCredLineZh =
      '泽森（Jason Li）· 键共振命理作者 · 结构模型 · 管理工程专业作品';
  static const authorCredLineEn =
      'Zeisen (Jason Li) · Author of Key Resonance Destiny · Structural model · '
      'a Management Engineering professional work';

  /// 加注：本软件为作者管理工程专业方向之作品
  static const authorProfessionNoteZh =
      '本软件为作者管理工程专业的作品。';
  static const authorProfessionNoteEn =
      'This software is a professional work of the author\'s Management Engineering background.';

  /// Stripe：仅 Publishable Key 可进客户端；Secret Key 只放后端
  static const _stripePkEnv = String.fromEnvironment(
    'STRIPE_PK',
    defaultValue: '', // 空 = 演示 / 待配置
  );
  static const stripeModeDemo = bool.fromEnvironment(
    'STRIPE_DEMO',
    defaultValue: true,
  );

  /// 作者预览构建：`--dart-define=AUTHOR_PREVIEW=true`
  /// 允许在 Release Web 包中使用演示解锁（仅作者自测隧道，勿上架）。
  static const authorPreviewMode = bool.fromEnvironment(
    'AUTHOR_PREVIEW',
    defaultValue: false,
  );

  /// Web 预览可运行时注入（勿把 sk_ 放这里；仅 pk_）
  static String _stripePkOverride = '';

  static void setStripePublishableKeyOverride(String value) {
    final v = value.trim();
    _stripePkOverride = v.startsWith('pk_') ? v : '';
  }

  static String get stripePublishableKey {
    if (_stripePkOverride.startsWith('pk_')) return _stripePkOverride;
    return _stripePkEnv;
  }

  /// Checkout API 根地址（Cloudflare Worker 路由到 /api/*）
  static const checkoutApiBase = String.fromEnvironment(
    'KR_CHECKOUT_API',
    defaultValue: 'https://keyresonance.com',
  );

  /// 演示解锁：Debug/Profile + STRIPE_DEMO；或作者预览构建（AUTHOR_PREVIEW）。
  /// 正式上架包勿带 AUTHOR_PREVIEW。
  static bool get allowDemoUnlock =>
      stripeModeDemo && (!kReleaseMode || authorPreviewMode);

  /// 真收款就绪：非演示，且已配置 Publishable Key（后端 Secret 不进客户端）。
  static bool get stripeReady =>
      !allowDemoUnlock && stripePublishableKey.startsWith('pk_');

  /// 定价（2026-07-31）：专业板 $4.99 · 键共振买断 $24.99；
  /// 另：月 $9.99 / 年 $79 · 单次判局 $1.99；已购专业板升买断只补差价。
  static const pricesUsdCents = <ProductTier, int>{
    ProductTier.pro: 499, // $4.99
    ProductTier.resonanceMaster: 2499, // $24.99 lifetime
  };

  /// 华语参考价（展示用）
  static const pricesCnyYuan = <ProductTier, int>{
    ProductTier.pro: 38,
    ProductTier.resonanceMaster: 168,
  };

  static const masterMonthlyUsdCents = 999; // $9.99/mo
  static const masterYearlyUsdCents = 7900; // $79/yr
  static const singleJudgmentUsdCents = 199; // $1.99 / 24h
  static const masterMonthlyCnyYuan = 68;
  static const masterYearlyCnyYuan = 528;
  static const singleJudgmentCnyYuan = 15;

  /// 已拥有专业板 → 键共振买断补差（$24.99 − $4.99）
  static int get masterUpgradeFromProUsdCents =>
      pricesUsdCents[ProductTier.resonanceMaster]! -
      pricesUsdCents[ProductTier.pro]!;

  static int get masterUpgradeFromProCnyYuan =>
      pricesCnyYuan[ProductTier.resonanceMaster]! -
      pricesCnyYuan[ProductTier.pro]!;

  static String priceLabel(ProductTier tier) {
    if (tier == ProductTier.free) return 'Free';
    final cents = pricesUsdCents[tier];
    if (cents == null) return 'Free';
    return '\$${(cents / 100).toStringAsFixed(2)}';
  }

  /// 华语界面价签：`$4.99（约¥38）`；其它语种仅 USD。
  static String priceLabelOf(ProductTier tier, AppLang lang) {
    final usd = priceLabel(tier);
    if (tier == ProductTier.free) {
      return switch (lang) {
        AppLang.zh => '免费',
        AppLang.ja => '無料',
        AppLang.es => 'Gratis',
        AppLang.ar => 'مجاني',
        AppLang.en => 'Free',
      };
    }
    if (lang != AppLang.zh) return usd;
    final yuan = pricesCnyYuan[tier];
    if (yuan == null) return usd;
    return '$usd（约¥$yuan）';
  }
}

/// Stripe 产品目录 —— 调价后须在 Dashboard 新建 Price，替换 REPLACE_*。
class StripeCatalog {
  static const priceIds = <ProductTier, String>{
    ProductTier.pro: 'price_1U1XoxBogkBcYEebJZYIA00x',
    ProductTier.resonanceMaster: 'price_1U1Y4dBogkBcYEeb1dKxxjSM',
  };

  static const productIds = <ProductTier, String>{
    ProductTier.pro: 'prod_V1b0sZg9VUCTx3',
    ProductTier.resonanceMaster: 'prod_V1bH1bUhZAh4iK',
  };

  static const productNames = <ProductTier, String>{
    ProductTier.pro: 'Pro Plate',
    ProductTier.resonanceMaster: 'Key Resonance',
  };

  /// 附加 SKU（订阅 / 单次）；Dashboard 建好后写入
  static const addonPriceIds = <String, String>{
    'kr_resonance_mo_999': 'price_1U1Y8xBogkBcYEeble34gE2s',
    'kr_resonance_yr_7900': 'price_1U1YFUBogkBcYEebqkITbvWI',
    'kr_single_judgment_199': 'price_1U1YK5BogkBcYEebmVGR6Ozt',
    'kr_resonance_upgrade_1999': 'price_1U1YMsBogkBcYEeblJcIvI8X',
  };
}
