// Vaultfire protocol constants — live Base mainnet addresses
// Source: https://theloopbreaker.com/api/contracts (verified live)

export const VAULTFIRE_API = 'https://theloopbreaker.com';

export const BASE_CHAIN_ID = 8453;

export const CONTRACTS = {
  IdentityRegistry: '0xa7BD20bf5De63df949cA5Be2F20835978eCba81A',
  PartnershipBonds: '0x01C479F0c039fEC40c0Cf1c5C921bab457d57441',
  AccountabilityBonds: '0x6750D28865434344e04e1D0a6044394b726C3dfE',
  VNS: '0xA9e6c2c0a731F1f56F6720Dfac2eB1440Ab9453a',
  ReputationRegistry: '0x98afd1440B2238D73c1394720277a6d031fCbbD0',
  // EIP-7702 batch onboarder — stateless delegate contract. Deployed via CREATE2.
  // Lets an EOA register itself as an agent AND create a partnership bond in ONE
  // signature via EIP-7702 Type-4 transactions.
  // Verified: https://basescan.org/address/0x82c6a0dd34f5cf57f8b98ee118e911b977618890#code
  BatchOnboarder: '0x82c6a0dd34F5cF57F8B98Ee118e911B977618890',
} as const;

// ERC-8004 Identity Registry
export const IDENTITY_REGISTRY_ABI = [
  {
    type: 'function',
    name: 'registerAgent',
    stateMutability: 'nonpayable',
    inputs: [
      { name: 'agentURI', type: 'string' },
      { name: 'agentType', type: 'string' },
      { name: 'capabilitiesHash', type: 'bytes32' },
    ],
    outputs: [],
  },
  {
    type: 'function',
    name: 'getAgent',
    stateMutability: 'view',
    inputs: [{ name: 'agent', type: 'address' }],
    outputs: [
      { name: 'agentURI', type: 'string' },
      { name: 'active', type: 'bool' },
      { name: 'agentType', type: 'string' },
      { name: 'registeredAt', type: 'uint256' },
    ],
  },
  {
    type: 'function',
    name: 'getTotalAgents',
    stateMutability: 'view',
    inputs: [],
    outputs: [{ type: 'uint256' }],
  },
] as const;

// AI Partnership Bonds V2
export const PARTNERSHIP_BONDS_ABI = [
  {
    type: 'function',
    name: 'createBond',
    stateMutability: 'payable',
    inputs: [
      { name: 'aiAgent', type: 'address' },
      { name: 'partnershipType', type: 'string' },
    ],
    outputs: [{ type: 'uint256' }],
  },
  {
    type: 'function',
    name: 'nextBondId',
    stateMutability: 'view',
    inputs: [],
    outputs: [{ type: 'uint256' }],
  },
] as const;

// VaultfireBatchOnboarder (EIP-7702 one-shot onboard + bond)
export const BATCH_ONBOARDER_ABI = [
  {
    type: 'function',
    name: 'onboardAndBond',
    stateMutability: 'payable',
    inputs: [
      { name: 'agentURI', type: 'string' },
      { name: 'agentType', type: 'string' },
      { name: 'capabilitiesHash', type: 'bytes32' },
      { name: 'aiAgent', type: 'address' },
      { name: 'partnershipType', type: 'string' },
    ],
    outputs: [{ name: 'bondId', type: 'uint256' }],
  },
  {
    type: 'function',
    name: 'targets',
    stateMutability: 'view',
    inputs: [],
    outputs: [
      { name: 'identityRegistry', type: 'address' },
      { name: 'partnershipBonds', type: 'address' },
      { name: 'minStakeWei', type: 'uint256' },
      { name: 'baseChainId', type: 'uint256' },
    ],
  },
  {
    type: 'event',
    name: 'OnboardedWithBond',
    inputs: [
      { name: 'onboardedEoa', type: 'address', indexed: true },
      { name: 'aiAgent', type: 'address', indexed: true },
      { name: 'bondId', type: 'uint256', indexed: true },
      { name: 'stakeAmount', type: 'uint256', indexed: false },
      { name: 'agentType', type: 'string', indexed: false },
      { name: 'partnershipType', type: 'string', indexed: false },
    ],
  },
] as const;

/**
 * Generate a stable, human-friendly .vns-style name from a wallet address.
 * E.g. 0xfA15Ee28...813C → "agent-fa15.vns"
 */
export function nameForAddress(addr: string): string {
  const short = addr.toLowerCase().replace(/^0x/, '').slice(0, 4);
  return `agent-${short}.vns`;
}

/**
 * Build the agentURI JSON payload that gets stored on-chain.
 */
export function buildAgentURI(opts: {
  address: string;
  description?: string;
  capabilities?: string[];
}): string {
  const name = nameForAddress(opts.address);
  return JSON.stringify({
    name,
    description: opts.description ?? 'Vaultfire agent registered via mobile',
    identityType: 'human-operated',
    capabilities: opts.capabilities ?? ['trust', 'verification', 'identity'],
    version: 'v1',
    protocol: 'vaultfire-kya',
    registeredVia: 'vaultfire-mini-app',
  });
}

/**
 * Compute the capabilities hash the contract expects.
 * ERC-8004 style: keccak256 of the capabilities string list joined.
 */
export async function computeCapabilitiesHash(
  capabilities: string[]
): Promise<`0x${string}`> {
  const { keccak256, toBytes } = await import('viem');
  const joined = capabilities.sort().join(',');
  return keccak256(toBytes(joined));
}

// Bond Insurance Pool (Sprint 3 underlying) and ERC-4626 view adapter
export const BIP_POOL_ADDRESS = '0xa074e372d5221b605E9762e41B86EbdBEBF1a978' as const;
export const BIP_VAULT_ADDRESS = '0x4f7E997C85bC1207485BaB8A0041Ca1ae0Fc9061' as const;
export const BIP_MIN_CONTRIBUTION_WEI = 1_000_000_000_000_000n; // 0.001 ETH

export const BIP_POOL_ABI = [
  {
    type: 'function',
    name: 'contribute',
    stateMutability: 'payable',
    inputs: [],
    outputs: [],
  },
  {
    type: 'function',
    name: 'totalPoolBalance',
    stateMutability: 'view',
    inputs: [],
    outputs: [{ type: 'uint256' }],
  },
  {
    type: 'function',
    name: 'MIN_CONTRIBUTION',
    stateMutability: 'view',
    inputs: [],
    outputs: [{ type: 'uint256' }],
  },
] as const;

// ERC-4626 view-only adapter over BIP pool. All write paths revert.
export const BIP_VAULT_ABI = [
  { type: 'function', name: 'name', stateMutability: 'view', inputs: [], outputs: [{ type: 'string' }] },
  { type: 'function', name: 'symbol', stateMutability: 'view', inputs: [], outputs: [{ type: 'string' }] },
  { type: 'function', name: 'decimals', stateMutability: 'view', inputs: [], outputs: [{ type: 'uint8' }] },
  { type: 'function', name: 'asset', stateMutability: 'view', inputs: [], outputs: [{ type: 'address' }] },
  { type: 'function', name: 'pool', stateMutability: 'view', inputs: [], outputs: [{ type: 'address' }] },
  { type: 'function', name: 'totalAssets', stateMutability: 'view', inputs: [], outputs: [{ type: 'uint256' }] },
  { type: 'function', name: 'totalSupply', stateMutability: 'view', inputs: [], outputs: [{ type: 'uint256' }] },
  {
    type: 'function',
    name: 'balanceOf',
    stateMutability: 'view',
    inputs: [{ name: 'account', type: 'address' }],
    outputs: [{ type: 'uint256' }],
  },
  {
    type: 'function',
    name: 'convertToShares',
    stateMutability: 'view',
    inputs: [{ name: 'assets', type: 'uint256' }],
    outputs: [{ type: 'uint256' }],
  },
  {
    type: 'function',
    name: 'convertToAssets',
    stateMutability: 'view',
    inputs: [{ name: 'shares', type: 'uint256' }],
    outputs: [{ type: 'uint256' }],
  },
] as const;
