/**
 * F5ADMINUX-013: Human-readable labels for audit log action strings.
 * Raw action strings (e.g. 'admin.impersonation_started') are dot-namespaced
 * internal codes. This map converts them to operator-facing labels.
 * The raw string is preserved in title/tooltip for forensic precision.
 */
export const AUDIT_ACTION_LABELS: Record<string, string> = {
  // Auth
  'auth.login': 'User logged in',
  'auth.logout': 'User logged out',
  'auth.failed': 'Login failed',
  'auth.password_reset': 'Password reset',
  'auth.email_verified': 'Email verified',
  'auth.token_created': 'API token created',
  'auth.token_deleted': 'API token deleted',

  // Admin
  'admin.impersonation_started': 'Impersonation started',
  'admin.impersonation_stopped': 'Impersonation stopped',
  'admin.user_created': 'User created (admin)',
  'admin.user_updated': 'User updated (admin)',
  'admin.user_deleted': 'User deleted (admin)',
  'admin.user_banned': 'User banned',
  'admin.user_unbanned': 'User unbanned',
  'admin.role_assigned': 'Admin role assigned',
  'admin.role_removed': 'Admin role removed',

  // Sites
  'site.created': 'Site created',
  'site.updated': 'Site updated',
  'site.deleted': 'Site deleted',
  'site.activated': 'Site activated',
  'site.deactivated': 'Site deactivated',
  'site.bulk_activate': 'Sites bulk activated',
  'site.bulk_deactivate': 'Sites bulk deactivated',

  // GSC
  'gsc.connection_created': 'GSC connection added',
  'gsc.connection_deleted': 'GSC connection removed',
  'gsc.sync_triggered': 'GSC sync triggered',

  // WP
  'wp.connection_created': 'WP connection added',
  'wp.connection_deleted': 'WP connection removed',
  'wp.publish': 'Content published to WP',

  // Analysis
  'analysis.run_started': 'Analysis started',
  'analysis.run_completed': 'Analysis completed',
  'analysis.run_failed': 'Analysis failed',

  // AI / Drafts
  'ai.draft_generated': 'AI draft generated',
  'ai.draft_published': 'AI draft published',

  // Billing
  'billing.subscription_created': 'Subscription created',
  'billing.subscription_cancelled': 'Subscription cancelled',
  'billing.subscription_upgraded': 'Subscription upgraded',
  'billing.payment_failed': 'Payment failed',

  // Settings
  'settings.updated': 'Settings updated',
  'settings.api_key_updated': 'API key updated',
  'settings.branding_updated': 'Branding updated',
};

/**
 * Returns a human-readable label for an action string.
 * Falls back to a title-cased version of the raw action if no entry found.
 */
export function getAuditLabel(action: string | null | undefined): string {
  if (!action) return '—';
  return AUDIT_ACTION_LABELS[action] ?? titleCase(action);
}

function titleCase(action: string): string {
  // 'admin.impersonation_started' → 'Admin Impersonation Started'
  return action
    .replace(/[._]/g, ' ')
    .replace(/\b\w/g, (c) => c.toUpperCase());
}
