// app/network.jsx
// Single source of truth for per-network config.
// Pure data + tiny helpers. NO side effects on module load
// (no localStorage writes, no DOM access). NetworkProvider in main.jsx
// handles persistence; api.jsx + data.jsx consume cfg objects.

const NETWORK_CONFIGS = {
  testnet: {
    id:                 "testnet",
    label:              "Testnet",
    apiHost:            "https://api.testnet.hiro.so",
    explorerChain:      "testnet",
    deployer:           "ST2ABWV7JE5SFV1A1BDS8HARP2QY7QRPGC9KJJYWE",
    suffix:             "v6",
    deployFeeMicroStx:  500000,         // 0.5 STX, matches today's hardcode
    color:              "orange",       // var(--orange)
    addressPrefix:      "ST",
  },
  mainnet: {
    id:                 "mainnet",
    label:              "Mainnet",
    apiHost:            "https://api.hiro.so",
    explorerChain:      "mainnet",
    deployer:           "SP2GF0C5YV1HFVGVF7GK9FFVD8FANPVFP8R4BYDP5",
    suffix:             "v6-mn-demo",
    deployFeeMicroStx:  2000000,        // 2.0 STX headroom, see design doc
    color:              "green",        // var(--green)
    addressPrefix:      "SP",
  },
};

function buildContractIds(cfg) {
  const sfx = cfg.suffix;
  return {
    traitName:          `restricted-ft-trait-${sfx}`,
    singletonName:      `lp-singleton-${sfx}`,
    tokenTemplateName:  `restricted-token-template-${sfx}`,
    demoTokenName:      `strategy-tst-${sfx}`,
    trait:              `${cfg.deployer}.restricted-ft-trait-${sfx}`,
    singleton:          `${cfg.deployer}.lp-singleton-${sfx}`,
    tokenTemplate:      `${cfg.deployer}.restricted-token-template-${sfx}`,
    demoTokenPrincipal: `${cfg.deployer}.strategy-tst-${sfx}`,
  };
}

function getNetworkConfig(id) {
  const base = NETWORK_CONFIGS[id] || NETWORK_CONFIGS.testnet;
  return { ...base, ...buildContractIds(base) };
}

window.NetworkConfig = {
  list:        ["testnet", "mainnet"],
  get:         getNetworkConfig,
  STORAGE_KEY: "stacks-strategy:network",
  DEFAULT:     "testnet",
};
