import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from 'url';
import * as tls from 'tls';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export interface TestCertificates {
  privateKey: string;
  publicKey: string;
}

export function loadTestCertificates(): TestCertificates {
  const certPath = path.join(__dirname, '..', '..', 'assets', 'certs', 'cert.pem');
  const keyPath = path.join(__dirname, '..', '..', 'assets', 'certs', 'key.pem');

  // Read certificates
  const publicKey = fs.readFileSync(certPath, 'utf8');
  const privateKey = fs.readFileSync(keyPath, 'utf8');

  // Validate certificates
  try {
    // Try to create a secure context with the certificates
    tls.createSecureContext({
      cert: publicKey,
      key: privateKey
    });
  } catch (error) {
    throw new Error(`Invalid certificates: ${error.message}`);
  }

  return {
    privateKey,
    publicKey
  };
}