import { invalidV3Input, strictV3Query, unavailableV3Read, V3_ADDRESS } from '../_lib/http';
import { requireV3X402Release } from '../x402/_lib/release';

export function GET(request: Request): Response {
  const closed = requireV3X402Release();
  if (closed) return closed;
  const query = strictV3Query(request, ['contract', 'asset', 'holder']);
  if (!query) return invalidV3Input('invalid_pull_credit_query', 'Exactly one contract, asset, and holder query parameter is required; unknown parameters are rejected');
  const contract = query.get('contract');
  const asset = query.get('asset');
  const holder = query.get('holder');
  if (![contract, asset, holder].every((value) => value !== null && V3_ADDRESS.test(value))) {
    return invalidV3Input('invalid_pull_credit_query', 'contract, asset, and holder must be canonical 20-byte addresses');
  }
  return unavailableV3Read({
    route: '/api/v3/pull-credits',
    resource: `/api/v3/pull-credits?contract=${contract}&asset=${asset}&holder=${holder}`,
    code: 'v3_pull_credit_reader_unconfigured',
    message: 'Canonical V3 pull-credit deployments and the read adapter are not configured',
  });
}
