function readNumber(value, fallback) {
  const parsed = Number(value);
  return Number.isFinite(parsed) ? parsed : fallback;
}

const TOKENOMICS_CONFIG = Object.freeze({
  chainName: process.env.AISFOR_CHAIN_NAME || 'PLAI Chain',
  rewardUnitName: process.env.AISFOR_REWARD_UNIT_NAME || 'PLAI Point',
  tokenDisplayName: process.env.AISFOR_TOKEN_DISPLAY_NAME || 'PLAI Coin',
  finalCoinName: process.env.AISFOR_FINAL_COIN_NAME || 'PLAI Coin',
  symbol: process.env.AISFOR_TOKEN_SYMBOL || 'PLAI',
  decimals: readNumber(process.env.AISFOR_TOKEN_DECIMALS, 6),
  maxSupply: readNumber(process.env.AISFOR_TOKEN_MAX_SUPPLY, 10000000000),
  model: process.env.AISFOR_REWARD_MODEL || 'PoSS',
  modelFullName: process.env.AISFOR_REWARD_MODEL_FULL_NAME || 'Proof of Service & Storage',
  phase: process.env.AISFOR_TOKEN_PHASE || 'internal-point',
  rewardMode: process.env.AISFOR_REWARD_MODE || 'simulation',
  testnetResettable: process.env.AISFOR_TESTNET_TOKEN_RESETTABLE !== '0',
  publicIssuance: process.env.AISFOR_PUBLIC_COIN_ISSUANCE || 'disabled'
});

const REWARD_WEIGHTS = Object.freeze({
  service: readNumber(process.env.AISFOR_REWARD_WEIGHT_SERVICE, 1),
  content: readNumber(process.env.AISFOR_REWARD_WEIGHT_CONTENT, 1),
  storage: readNumber(process.env.AISFOR_REWARD_WEIGHT_STORAGE, 1),
  uptime: readNumber(process.env.AISFOR_REWARD_WEIGHT_UPTIME, 1),
  verification: readNumber(process.env.AISFOR_REWARD_WEIGHT_VERIFICATION, 1),
  commerce: readNumber(process.env.AISFOR_REWARD_WEIGHT_COMMERCE, 1),
  emoticon: readNumber(process.env.AISFOR_REWARD_WEIGHT_EMOTICON, 1),
  ecosystem: readNumber(process.env.AISFOR_REWARD_WEIGHT_ECOSYSTEM, 1)
});

const REWARD_LIMITS = Object.freeze({
  dailyUserCap: readNumber(process.env.AISFOR_REWARD_DAILY_USER_CAP, 1000),
  dailyCategoryCap: readNumber(process.env.AISFOR_REWARD_DAILY_CATEGORY_CAP, 100000),
  suspiciousScoreThreshold: readNumber(process.env.AISFOR_REWARD_SUSPICIOUS_SCORE_THRESHOLD, 5000)
});

module.exports = {
  TOKENOMICS_CONFIG,
  REWARD_WEIGHTS,
  REWARD_LIMITS
};
