import { createConfig } from 'ponder';
import type { Abi } from 'viem';
import { KraikenAbi, StakeAbi, LiquidityManagerAbi } from 'kraiken-lib/abis';

// Network configurations keyed by canonical environment name
type NetworkConfig = {
  chainId: number;
  rpc: string;
  disableCache?: boolean;
  contracts: {
    kraiken: string;
    stake: string;
    liquidityManager: string;
    startBlock: number;
  };
};

const networks: Record<string, NetworkConfig> = {
  BASE_SEPOLIA_LOCAL_FORK: {
    chainId: 31337,
    rpc: process.env.PONDER_RPC_URL_BASE_SEPOLIA_LOCAL_FORK || 'http://127.0.0.1:8545',
    disableCache: true,
    contracts: {
      kraiken: process.env.KRAIKEN_ADDRESS || '0x56186c1E64cA8043dEF78d06AfF222212eA5df71',
      stake: process.env.STAKE_ADDRESS || '0x056E4a859558A3975761ABd7385506BC4D8A8E60',
      liquidityManager: process.env.LM_ADDRESS || '0x33d10f2449ffede92b43d4fba562f132ba6a766a',
      startBlock: parseInt(process.env.START_BLOCK || '31425917'),
    },
  },
  BASE_MAINNET_LOCAL_FORK: {
    chainId: 31337,
    rpc: process.env.PONDER_RPC_URL_BASE_MAINNET_LOCAL_FORK || 'http://127.0.0.1:8545',
    disableCache: true,
    contracts: {
      kraiken: process.env.KRAIKEN_ADDRESS || '0x45caa5929f6ee038039984205bdecf968b954820',
      stake: process.env.STAKE_ADDRESS || '0xed70707fab05d973ad41eae8d17e2bcd36192cfc',
      liquidityManager: process.env.LM_ADDRESS || '0x0000000000000000000000000000000000000000',
      startBlock: parseInt(process.env.START_BLOCK || '26038614'),
    },
  },
  BASE_SEPOLIA: {
    chainId: 84532,
    rpc: process.env.PONDER_RPC_URL_BASE_SEPOLIA || 'https://sepolia.base.org',
    contracts: {
      kraiken: '0x22c264Ecf8D4E49D1E3CabD8DD39b7C4Ab51C1B8',
      stake: '0xe28020BCdEeAf2779dd47c670A8eFC2973316EE2',
      liquidityManager: process.env.LM_ADDRESS || '0x0000000000000000000000000000000000000000',
      startBlock: 20940337,
    },
  },
  BASE: {
    chainId: 8453,
    rpc: process.env.PONDER_RPC_URL_BASE || 'https://base.llamarpc.com',
    contracts: {
      kraiken: '0x45caa5929f6ee038039984205bdecf968b954820',
      stake: '0xed70707fab05d973ad41eae8d17e2bcd36192cfc',
      liquidityManager: process.env.LM_ADDRESS || '0x0000000000000000000000000000000000000000',
      startBlock: 26038614,
    },
  },
};

// Select network based on environment variable
const NETWORK = (process.env.PONDER_NETWORK as keyof typeof networks) || 'BASE_SEPOLIA_LOCAL_FORK';
const selectedNetwork = networks[NETWORK as keyof typeof networks];

if (!selectedNetwork) {
  throw new Error(`Invalid network: ${NETWORK}. Valid options: ${Object.keys(networks).join(', ')}`);
}

// Network configuration logged during Ponder startup
// Network=${NETWORK}, chainId=${selectedNetwork.chainId}, startBlock=${selectedNetwork.contracts.startBlock}

export default createConfig({
  // Use PostgreSQL if DATABASE_URL is set, otherwise use PGlite
  database: process.env.DATABASE_URL
    ? {
        kind: 'postgres' as const,
        connectionString: process.env.DATABASE_URL,
        schema: process.env.DATABASE_SCHEMA || 'public',
      }
    : undefined,
  chains: {
    [NETWORK]: {
      id: selectedNetwork.chainId,
      rpc: selectedNetwork.rpc,
      disableCache: selectedNetwork.disableCache ?? false,
    },
  },
  contracts: {
    Kraiken: {
      abi: KraikenAbi satisfies Abi,
      chain: NETWORK,
      address: selectedNetwork.contracts.kraiken as `0x${string}`,
      startBlock: selectedNetwork.contracts.startBlock,
    },
    Stake: {
      abi: StakeAbi satisfies Abi,
      chain: NETWORK,
      address: selectedNetwork.contracts.stake as `0x${string}`,
      startBlock: selectedNetwork.contracts.startBlock,
    },
    LiquidityManager: {
      abi: LiquidityManagerAbi satisfies Abi,
      chain: NETWORK,
      address: selectedNetwork.contracts.liquidityManager as `0x${string}`,
      startBlock: selectedNetwork.contracts.startBlock,
    },
  },
  blocks: {
    StatsBlock: {
      chain: NETWORK,
      interval: 1,
      startBlock: selectedNetwork.contracts.startBlock,
    },
  },
});
