/**
 * GET /api/x402/trust/contracts
 *
 * x402 payment-gated contract registry — $0.05 USDC per query.
 * Payments routed through CDP facilitator for Bazaar discovery.
 *
 * Returns the full registry of 134 verified smart contracts
 * across 4 mainnet chains with addresses.
 *
 * Proxies to the real hub API at /api/contracts.
 */

import { NextRequest, NextResponse } from 'next/server';
import { withX402MultiNet } from '../_lib/with-x402-multinet';

const handler = async (req: NextRequest) => {
  try {
    const hubUrl = `${req.nextUrl.origin}/api/contracts`;
    const hubRes = await fetch(hubUrl);
    const data = await hubRes.json();

    return NextResponse.json({
      ...data,
      _x402: {
        paid: true,
        price: '$0.05 USDC',
        service: 'vaultfire-trust',
        facilitator: 'cdp',
      },
    }, {
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Cache-Control': 'no-store',
      },
    });
  } catch {
    return NextResponse.json(
      { error: 'Failed to query Vaultfire hub' },
      { status: 502 }
    );
  }
};

// Multi-network paywall: accepts USDC on Base AND Solana mainnet.
// Bazaar discovery picks up both networks from the 402 response's `accepts` array.
export const GET = withX402MultiNet(handler, {
  price: '$0.05',
  description: 'Full contract registry — 134 verified smart contracts across 4 mainnet chains (Base, Avalanche, Arbitrum, Polygon) with addresses and deployment info.',
      mimeType: 'application/json',
      outputSchema: {
        type: 'object',
        properties: {
          totalContracts: { type: 'number', description: 'Total verified smart contracts' },
          chains: { type: 'number', description: 'Number of chains' },
          contracts: {
            type: 'object',
            description: 'Contracts grouped by chain',
          },
        },
      }
}) as (req: NextRequest) => Promise<Response>;
