import { NextResponse } from 'next/server';

/**
 * GET /api/openapi.json
 *
 * Machine-readable OpenAPI 3.1 specification for all Vaultfire Agent Hub APIs.
 * Any agent framework can auto-generate client code from this spec.
 */

const SPEC = {
  openapi: '3.1.0',
  info: {
    title: 'Vaultfire Agent Hub API',
    version: '3.4.0',
    description:
      'The trust infrastructure for AI agents. On-chain identity, verifiable reputation, partnership bonds, task marketplace, cross-chain bridge, and more. All endpoints are public and require no authentication for reads.',
    contact: { url: 'https://theloopbreaker.com' },
    license: { name: 'MIT', url: 'https://opensource.org/licenses/MIT' },
  },
  servers: [{ url: 'https://theloopbreaker.com/api', description: 'Production' }],
  paths: {
    '/health': {
      get: {
        operationId: 'getHealth',
        summary: 'System health check',
        description: 'Chain connectivity, API status, uptime. Call before committing to using the platform.',
        tags: ['System'],
        responses: { '200': { description: 'Health status with per-chain connectivity' } },
      },
    },
    '/hub/stats': {
      get: {
        operationId: 'getHubStats',
        summary: 'Protocol-wide statistics',
        description: 'Total registered agents, active bonds, total value bonded across all chains.',
        tags: ['Protocol'],
        responses: { '200': { description: 'Protocol stats' } },
      },
    },
    '/agent/status': {
      get: {
        operationId: 'getAgentStatus',
        summary: 'Full trust profile for an agent',
        description: 'Identity status, Street Cred score (0-95), on-chain rating, partnership bonds, bond tier.',
        tags: ['Agent'],
        parameters: [
          { name: 'address', in: 'query', required: true, schema: { type: 'string' }, description: 'Wallet address (0x...)' },
        ],
        responses: { '200': { description: 'Trust profile' } },
      },
    },
    '/agent/status/batch': {
      post: {
        operationId: 'batchAgentStatus',
        summary: 'Batch trust profile lookup',
        description: 'Check up to 25 agent addresses in one call.',
        tags: ['Agent'],
        requestBody: {
          required: true,
          content: {
            'application/json': {
              schema: {
                type: 'object',
                required: ['addresses'],
                properties: {
                  addresses: { type: 'array', items: { type: 'string' }, maxItems: 25, description: 'Array of wallet addresses' },
                  chain: { type: 'string', enum: ['base', 'avalanche', 'arbitrum', 'polygon'], default: 'base' },
                },
              },
            },
          },
        },
        responses: { '200': { description: 'Array of trust profiles' } },
      },
    },
    '/agent/register': {
      post: {
        operationId: 'registerAgent',
        summary: 'Build an ERC-8004 identity registration transaction',
        tags: ['Agent'],
        requestBody: {
          required: true,
          content: {
            'application/json': {
              schema: {
                type: 'object',
                required: ['name', 'walletAddress'],
                properties: {
                  name: { type: 'string' },
                  walletAddress: { type: 'string' },
                  chain: { type: 'string', enum: ['base', 'avalanche', 'arbitrum', 'polygon'], default: 'base' },
                },
              },
            },
          },
        },
        responses: { '200': { description: 'Unsigned transaction to submit on-chain' } },
      },
    },
    '/agent/bond': {
      post: {
        operationId: 'createBond',
        summary: 'Build a partnership bond creation transaction',
        tags: ['Bonds'],
        requestBody: {
          required: true,
          content: {
            'application/json': {
              schema: {
                type: 'object',
                required: ['walletAddress', 'agentAddress', 'amountEth'],
                properties: {
                  walletAddress: { type: 'string' },
                  agentAddress: { type: 'string' },
                  amountEth: { type: 'string' },
                  chain: { type: 'string', enum: ['base', 'avalanche', 'arbitrum', 'polygon'], default: 'base' },
                },
              },
            },
          },
        },
        responses: { '200': { description: 'Unsigned bond creation transaction' } },
      },
    },
    '/agent/partnerships': {
      get: {
        operationId: 'getPartnerships',
        summary: 'List all partnership bonds for an address',
        tags: ['Bonds'],
        parameters: [
          { name: 'address', in: 'query', required: true, schema: { type: 'string' } },
        ],
        responses: { '200': { description: 'Bond list' } },
      },
    },
    '/agent/discover': {
      get: {
        operationId: 'discoverAgents',
        summary: 'Agent directory — paginated list of all registered agents',
        tags: ['Discovery'],
        responses: { '200': { description: 'Agent list' } },
      },
    },
    '/agent/route': {
      post: {
        operationId: 'routeTask',
        summary: 'Multi-agent routing — match a task to the best agent',
        tags: ['Routing'],
        requestBody: {
          required: true,
          content: {
            'application/json': {
              schema: {
                type: 'object',
                required: ['task'],
                properties: {
                  task: { type: 'string', description: 'Task description' },
                  requiredCapabilities: { type: 'array', items: { type: 'string' } },
                  preferredChain: { type: 'string', enum: ['base', 'avalanche', 'arbitrum', 'polygon'] },
                },
              },
            },
          },
        },
        responses: { '200': { description: 'Matched agent with score and capabilities' } },
      },
    },
    '/agent/routing': {
      get: {
        operationId: 'getRoutingHistory',
        summary: 'Routing history, stats, and active routes',
        tags: ['Routing'],
        responses: { '200': { description: 'Routing activity' } },
      },
    },
    '/agent/bridge': {
      get: {
        operationId: 'getBridgeState',
        summary: 'Cross-chain bridge state from VaultfireTeleporterBridge',
        tags: ['Bridge'],
        parameters: [
          { name: 'chain', in: 'query', required: true, schema: { type: 'string', enum: ['base', 'avalanche', 'arbitrum', 'polygon'] } },
          { name: 'address', in: 'query', required: false, schema: { type: 'string' }, description: 'Optional agent address for recognition check' },
        ],
        responses: { '200': { description: 'Bridge stats and synced data counts' } },
      },
    },
    '/agent/webhooks': {
      get: {
        operationId: 'listWebhooks',
        summary: 'List registered webhooks or show valid events',
        tags: ['Webhooks'],
        parameters: [
          { name: 'address', in: 'query', required: false, schema: { type: 'string' } },
        ],
        responses: { '200': { description: 'Webhook list or event catalog' } },
      },
      post: {
        operationId: 'registerWebhook',
        summary: 'Register a webhook for event notifications',
        tags: ['Webhooks'],
        requestBody: {
          required: true,
          content: {
            'application/json': {
              schema: {
                type: 'object',
                required: ['address', 'url', 'events'],
                properties: {
                  address: { type: 'string', description: 'Your agent address (0x...)' },
                  url: { type: 'string', description: 'HTTPS webhook URL' },
                  events: {
                    type: 'array',
                    items: { type: 'string', enum: [
                      'bond.created', 'bond.activated', 'bond.completed',
                      'reputation.updated', 'credential.issued', 'credential.revoked',
                      'dispute.filed', 'dispute.resolved', 'bridge.synced',
                      'insurance.claim_filed', 'insurance.payout', 'agent.registered',
                      'vouch.received', 'vouch.slashed', 'task.assigned', 'task.completed',
                    ]},
                  },
                  chain: { type: 'string', default: 'all' },
                },
              },
            },
          },
        },
        responses: { '201': { description: 'Webhook registered with signing secret' } },
      },
      delete: {
        operationId: 'deleteWebhook',
        summary: 'Remove a webhook',
        tags: ['Webhooks'],
        parameters: [
          { name: 'id', in: 'query', required: true, schema: { type: 'string' } },
        ],
        responses: { '200': { description: 'Webhook deleted' } },
      },
    },
    '/agent/analytics': {
      get: {
        operationId: 'getAgentAnalytics',
        summary: 'Agent performance analytics and trends',
        tags: ['Analytics'],
        parameters: [
          { name: 'address', in: 'query', required: true, schema: { type: 'string' } },
          { name: 'chain', in: 'query', schema: { type: 'string', default: 'base' } },
        ],
        responses: { '200': { description: 'Analytics with trust history and activity summary' } },
      },
    },
    '/agent/tasks': {
      get: {
        operationId: 'getTaskStats',
        summary: 'Task escrow stats — total tasks, protocol fee',
        tags: ['Tasks'],
        parameters: [{ name: 'chain', in: 'query', required: true, schema: { type: 'string', enum: ['base', 'avalanche', 'arbitrum', 'polygon'] } }],
        responses: { '200': { description: 'Task escrow stats from VaultfireTaskEscrow' } },
      },
      post: {
        operationId: 'prepareTask',
        summary: 'Prepare a createTask transaction on VaultfireTaskEscrow',
        tags: ['Tasks'],
        requestBody: { required: true, content: { 'application/json': { schema: { type: 'object', required: ['walletAddress', 'usdcAmount'], properties: { walletAddress: { type: 'string' }, usdcAmount: { type: 'number' }, description: { type: 'string' }, deadline: { type: 'number' }, chain: { type: 'string', default: 'base' } } } } } },
        responses: { '200': { description: 'Contract address and ABI for task creation' } },
      },
    },
    '/agent/keys': {
      get: {
        operationId: 'getVKPStatus',
        summary: 'VKP status — session keys, delegations, key wallets',
        tags: ['Keys'],
        parameters: [{ name: 'chain', in: 'query', required: true, schema: { type: 'string', enum: ['base', 'avalanche', 'arbitrum', 'polygon'] } }],
        responses: { '200': { description: 'VKP stats from SessionKeyManager, DelegationRegistry, Factory' } },
      },
      post: {
        operationId: 'prepareVKPAction',
        summary: 'Prepare a VKP transaction — createWallet, createSession, delegate, revoke',
        tags: ['Keys'],
        requestBody: { required: true, content: { 'application/json': { schema: { type: 'object', required: ['action', 'walletAddress'], properties: { action: { type: 'string', enum: ['createWallet', 'createSession', 'delegate', 'revoke', 'revokeSession'] }, walletAddress: { type: 'string' }, chain: { type: 'string', default: 'base' } } } } } },
        responses: { '200': { description: 'Contract address and ABI for VKP action' } },
      },
    },
    '/agent/vns': {
      get: {
        operationId: 'resolveVNS',
        summary: 'Resolve a .vns name or reverse-resolve an address',
        tags: ['Identity'],
        parameters: [
          { name: 'chain', in: 'query', required: true, schema: { type: 'string', enum: ['base', 'arbitrum', 'polygon'] }, description: 'VNS available on Base, Arbitrum, Polygon (not Avalanche)' },
          { name: 'name', in: 'query', required: false, schema: { type: 'string' }, description: 'VNS name to resolve' },
          { name: 'address', in: 'query', required: false, schema: { type: 'string' }, description: 'Address to reverse-resolve' },
        ],
        responses: { '200': { description: 'VNS resolution result' } },
      },
    },
    '/agent/trust': {
      get: {
        operationId: 'getZKTrust',
        summary: 'ZK trust attestation status from VaultfireTrustAttestation',
        tags: ['Trust'],
        parameters: [
          { name: 'chain', in: 'query', required: true, schema: { type: 'string' } },
          { name: 'address', in: 'query', required: false, schema: { type: 'string' } },
        ],
        responses: { '200': { description: 'ZK trust attestation data' } },
      },
    },
    '/agent/mission': {
      get: {
        operationId: 'getMissionCompliance',
        summary: 'Check agent mission compliance from MissionEnforcement',
        tags: ['Compliance'],
        parameters: [
          { name: 'chain', in: 'query', required: true, schema: { type: 'string' } },
          { name: 'address', in: 'query', required: false, schema: { type: 'string' } },
        ],
        responses: { '200': { description: 'Mission enforcement data' } },
      },
    },
    '/agent/privacy': {
      get: {
        operationId: 'getPrivacyStatus',
        summary: 'Privacy + anti-surveillance protections status',
        tags: ['Compliance'],
        parameters: [
          { name: 'chain', in: 'query', required: true, schema: { type: 'string' } },
          { name: 'address', in: 'query', required: false, schema: { type: 'string' } },
        ],
        responses: { '200': { description: 'AntiSurveillance + PrivacyGuarantees contract info' } },
      },
    },
    '/agent/attestation': {
      get: {
        operationId: 'getBeliefAttestation',
        summary: 'Belief attestation — belief-weighted governance, Dilithium post-quantum proofs',
        tags: ['Trust'],
        parameters: [
          { name: 'chain', in: 'query', required: true, schema: { type: 'string' } },
          { name: 'address', in: 'query', required: false, schema: { type: 'string' } },
        ],
        responses: { '200': { description: 'BeliefAttestationVerifier + DilithiumAttestor data' } },
      },
    },
    '/agent/flourishing': {
      get: {
        operationId: 'getFlourishingMetrics',
        summary: 'Human flourishing metrics from FlourishingMetricsOracle',
        tags: ['Trust'],
        parameters: [
          { name: 'chain', in: 'query', required: true, schema: { type: 'string' } },
          { name: 'address', in: 'query', required: false, schema: { type: 'string' } },
        ],
        responses: { '200': { description: 'Flourishing metrics' } },
      },
    },
    '/agent/governance': {
      get: {
        operationId: 'getGovernance',
        summary: 'Multisig governance proposals and status',
        tags: ['Governance'],
        parameters: [{ name: 'chain', in: 'query', required: true, schema: { type: 'string' } }],
        responses: { '200': { description: 'Governance stats and proposal count' } },
      },
    },
    '/agent/validation': {
      get: {
        operationId: 'getValidationStatus',
        summary: 'ERC-8004 validation compliance status',
        tags: ['Compliance'],
        parameters: [
          { name: 'chain', in: 'query', required: true, schema: { type: 'string' } },
          { name: 'address', in: 'query', required: false, schema: { type: 'string' } },
        ],
        responses: { '200': { description: 'ERC8004ValidationRegistry data' } },
      },
    },
    '/agent/adapter': {
      get: {
        operationId: 'getERC8004Adapter',
        summary: 'Unified ERC-8004 adapter — identity, reputation, validation in one call',
        tags: ['Protocol'],
        parameters: [{ name: 'chain', in: 'query', required: true, schema: { type: 'string' } }],
        responses: { '200': { description: 'Adapter with linked registry addresses' } },
      },
    },
    '/agent/vouching': {
      get: {
        operationId: 'getVouchProfile',
        summary: 'Get agent vouching profile — stakes received, given, slash count',
        description: 'Query the VaultfireReputationStaking contract for an agent\'s vouch profile. Returns total vouches received/given, stake amounts, and slash count.',
        tags: ['Trust'],
        parameters: [
          { name: 'address', in: 'query', required: true, schema: { type: 'string' }, description: 'Agent address to look up' },
          { name: 'chain', in: 'query', schema: { type: 'string', default: 'base' }, description: 'Chain to query' },
        ],
        responses: { '200': { description: 'Vouch profile with stakes and slash history' } },
      },
    },
    '/agent/performance': {
      get: {
        operationId: 'getPerformance',
        summary: 'Get agent performance attestations — completion rate, response time, accuracy',
        description: 'Query the VaultfireTrustOracle for verifiable off-chain performance data.',
        tags: ['Trust'],
        parameters: [
          { name: 'address', in: 'query', required: true, schema: { type: 'string' }, description: 'Agent address to look up' },
          { name: 'chain', in: 'query', schema: { type: 'string', default: 'base' }, description: 'Chain to query' },
        ],
        responses: { '200': { description: 'Performance data with completion rate and response time' } },
      },
    },
    '/agent/insurance': {
      get: {
        operationId: 'getInsurance',
        summary: 'Get bond insurance pool stats and agent coverage',
        description: 'Query the VaultfireBondInsurancePool for pool stats and per-agent coverage.',
        tags: ['Insurance'],
        parameters: [
          { name: 'chain', in: 'query', schema: { type: 'string', default: 'base' }, description: 'Chain to query' },
          { name: 'address', in: 'query', schema: { type: 'string' }, description: 'Optional agent address for coverage lookup' },
        ],
        responses: { '200': { description: 'Pool stats and coverage info' } },
      },
    },
    '/agent/credentials': {
      get: {
        operationId: 'getCredentials',
        summary: 'Get on-chain capability credentials for an agent',
        description: 'Query the VaultfireCapabilityCredentials contract for verified skills and certifications.',
        tags: ['Trust'],
        parameters: [
          { name: 'address', in: 'query', required: true, schema: { type: 'string' }, description: 'Agent address to look up' },
          { name: 'chain', in: 'query', schema: { type: 'string', default: 'base' }, description: 'Chain to query' },
        ],
        responses: { '200': { description: 'Agent credentials with skills, issuers, and validity' } },
      },
    },
    '/agent/disputes': {
      get: {
        operationId: 'getDisputes',
        summary: 'Get dispute resolution stats and dispute details',
        description: 'Query the VaultfireDisputeResolution contract for dispute statistics or individual dispute details.',
        tags: ['Disputes'],
        parameters: [
          { name: 'chain', in: 'query', schema: { type: 'string', default: 'base' }, description: 'Chain to query' },
          { name: 'disputeId', in: 'query', schema: { type: 'integer' }, description: 'Optional specific dispute ID to look up' },
        ],
        responses: { '200': { description: 'Dispute stats or individual dispute details' } },
      },
    },
    '/contracts': {
      get: {
        operationId: 'getContracts',
        summary: 'All 134 contract addresses across all 4 chains',
        tags: ['Protocol'],
        responses: { '200': { description: 'Contract registry' } },
      },
    },
  },
  components: {
    securitySchemes: {},
  },
  tags: [
    { name: 'System', description: 'Health and system status' },
    { name: 'Protocol', description: 'Protocol-wide stats and contracts' },
    { name: 'Agent', description: 'Agent identity and trust profiles' },
    { name: 'Bonds', description: 'Partnership and accountability bonds' },
    { name: 'Discovery', description: 'Agent directory and search' },
    { name: 'Routing', description: 'Task routing and matching' },
    { name: 'Bridge', description: 'Cross-chain trust sync' },
    { name: 'Trust', description: 'Reputation, vouching, credentials, oracles' },
    { name: 'Insurance', description: 'Bond insurance pool' },
    { name: 'Disputes', description: 'Dispute resolution' },
    { name: 'Webhooks', description: 'Event subscriptions' },
    { name: 'Analytics', description: 'Agent performance analytics' },
    { name: 'Tasks', description: 'Task marketplace with USDC escrow' },
    { name: 'Keys', description: 'Vaultfire Key Protocol — session keys, delegation, wallets' },
    { name: 'Compliance', description: 'Mission enforcement, privacy, validation' },
    { name: 'Governance', description: 'Multisig governance' },
    { name: 'Identity', description: 'VNS naming service' },
  ],
};

export async function GET() {
  return NextResponse.json(SPEC, {
    headers: {
      'Cache-Control': 'public, max-age=3600',
      'Access-Control-Allow-Origin': '*',
    },
  });
}
