import { execSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { logger } from './src/logger.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const certsDir = path.join(__dirname, 'certs');
const keyPath = path.join(certsDir, 'key.pem');
const certPath = path.join(certsDir, 'cert.pem');

fs.mkdirSync(certsDir, { recursive: true });

const subject = '/CN=localhost/O=JTL POS Sync/C=DE';
const san = 'subjectAltName=DNS:localhost,IP:127.0.0.1,IP:0.0.0.0';

execSync(
  `openssl req -x509 -newkey rsa:2048 -nodes -keyout "${keyPath}" -out "${certPath}" -days 3650 -subj "${subject}" -addext "${san}"`,
  { stdio: 'inherit' }
);

logger.success(`Wrote ${keyPath}`);
logger.success(`Wrote ${certPath}`);
