'use strict';

const crypto = require('crypto');

/**
 * Generate a self-hosted license key.
 * Format: TV-<tierCode>-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX (hex, upper-case).
 */
function generateLicenseKey(tier) {
  const prefix = 'TV';
  // 'free' → F so a 30-day trial key is identifiable on sight; without it the
  // fallback would mislabel trial keys as starter ('S').
  const tierCode = { free: 'F', starter: 'S', professional: 'P', business: 'B', enterprise: 'E', managed_services: 'M' }[tier] || 'S';
  const random = crypto.randomBytes(12).toString('hex').toUpperCase();
  // Split into 4-char groups: 24 hex chars -> 6 groups of 4
  const parts = random.match(/.{4}/g);
  return `${prefix}-${tierCode}-${parts.join('-')}`;
}

module.exports = { generateLicenseKey };
