import { PlannerConstraints, RoutingPolicy } from '../services/planner-v2-types';
import {
  getCommodityIntermediateAddresses,
  getDefaultIntermediateAddresses,
} from './routing-assets';

const CHAIN_138 = 138;

export function resolveRoutingPolicy(
  chainId: number,
  constraints: PlannerConstraints = {}
): RoutingPolicy {
  const requestedProfile = constraints.complianceProfile || 'standard';
  const defaultIntermediates = getDefaultIntermediateAddresses(chainId);
  const commodityIntermediates = getCommodityIntermediateAddresses(chainId);

  const baseStandard: RoutingPolicy = {
    profile: 'standard',
    allowedProviders: ['dodo', 'dodo_v3', 'uniswap_v3', 'uniswap_v2', 'sushiswap', 'balancer', 'curve', 'one_inch'],
    defaultIntermediateAddresses: defaultIntermediates,
    allowBridge: constraints.allowBridge !== false,
    allowedBridgeLabels: ['GRUTransport', 'CCIPStableBridge', 'CCIPWETH9Bridge', 'UniversalCCIPBridge', 'AlltraAdapter'],
    maxLegs: Math.min(Math.max(constraints.maxLegs || 3, 1), 3),
    allowCommodityIntermediates: constraints.allowCommodityIntermediates === true,
    notes: ['Standard policy allows live Chain 138 venues, including the DODO v3 pilot, but still requires explicit opt-in for commodity intermediates.'],
  };

  const baseInstitutional: RoutingPolicy = {
    profile: 'institutional',
    allowedProviders: ['dodo'],
    defaultIntermediateAddresses: defaultIntermediates,
    allowBridge: constraints.allowBridge !== false,
    allowedBridgeLabels: ['GRUTransport', 'CCIPStableBridge', 'UniversalCCIPBridge', 'AlltraAdapter'],
    maxLegs: Math.min(Math.max(constraints.maxLegs || 3, 1), 3),
    allowCommodityIntermediates: false,
    notes: ['Institutional policy restricts execution to canonical compliant assets and approved bridge rails.'],
  };

  const basePolicy = requestedProfile === 'institutional' ? baseInstitutional : baseStandard;
  const allowedProviders = constraints.allowedProviders?.length
    ? basePolicy.allowedProviders.filter((provider) => constraints.allowedProviders?.includes(provider))
    : basePolicy.allowedProviders;

  const allowedIntermediates = constraints.allowedIntermediates?.length
    ? constraints.allowedIntermediates.map((value) => value.toLowerCase())
    : basePolicy.defaultIntermediateAddresses.slice();

  if (basePolicy.allowCommodityIntermediates) {
    for (const commodity of commodityIntermediates) {
      if (!allowedIntermediates.includes(commodity)) {
        allowedIntermediates.push(commodity);
      }
    }
  }

  return {
    ...basePolicy,
    allowedProviders,
    defaultIntermediateAddresses: allowedIntermediates,
    allowedBridgeLabels: constraints.preferredBridges?.length
      ? basePolicy.allowedBridgeLabels.filter((bridge) => constraints.preferredBridges?.includes(bridge))
      : basePolicy.allowedBridgeLabels,
    notes: [
      ...basePolicy.notes,
      ...(chainId === CHAIN_138 ? ['Chain 138 routing defaults to WETH, USDT, USDC, cUSDT, and cUSDC intermediates.'] : []),
    ],
  };
}
