import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync, existsSync } from 'node:fs';
import { resolve } from 'node:path';

/** Paths used by HttpHybxClient (must match placeholder OpenAPI). */
const HTTP_CLIENT_PATHS = [
  { method: 'POST', path: '/v1/payments' },
  { method: 'GET', pathPrefix: '/v1/payments/' },
  { method: 'GET', path: '/v1/settlement-events' },
];

function repoRootFromTestDir(): string {
  // dist/hybx → proxmox root (5 levels up)
  return resolve(__dirname, '../../../../..');
}

function loadPlaceholderOpenApi(): string {
  const openApiPath = resolve(repoRootFromTestDir(), 'docs/api/openapi-hybx-integration-placeholder.yaml');
  assert.ok(existsSync(openApiPath), `placeholder OpenAPI missing: ${openApiPath}`);
  return readFileSync(openApiPath, 'utf8');
}

describe('HYBX placeholder OpenAPI contract', () => {
  it('declares HttpHybxClient payment and settlement paths', () => {
    const spec = loadPlaceholderOpenApi();
    for (const entry of HTTP_CLIENT_PATHS) {
      if (entry.path) {
        assert.match(spec, new RegExp(`^\\s+${entry.path.replace(/\//g, '\\/')}:\\s*$`, 'm'));
      }
      if (entry.pathPrefix) {
        assert.match(spec, /\/v1\/payments\/\{paymentId\}:/);
      }
    }
  });

  it('defines initiatePayment and getPaymentStatus operationIds', () => {
    const spec = loadPlaceholderOpenApi();
    assert.match(spec, /operationId:\s*initiatePayment/);
    assert.match(spec, /operationId:\s*getPaymentStatus/);
    assert.match(spec, /operationId:\s*listSettlementEvents/);
  });

  it('requires HybxPaymentRequest core fields', () => {
    const spec = loadPlaceholderOpenApi();
    for (const field of ['idempotencyKey', 'entityId', 'tenantId', 'amount', 'currency']) {
      assert.match(spec, new RegExp(`${field}:\\s*\\{`));
    }
  });
});
