import type { TranslationKey } from './i18n';

export interface ValidationResult {
  valid: boolean;
  error?: TranslationKey;
  name?: string;
}

const RESERVED_NAMES = ['admin', 'api', 'www', 'root', 'system', 'test', 'demo', 'portal'];

export function validateStackName(name: string): ValidationResult {
  if (!name) {
    return { valid: false, error: 'nameRequired' };
  }

  const trimmedName = name.trim().toLowerCase();

  if (trimmedName.length < 3 || trimmedName.length > 20) {
    return { valid: false, error: 'nameLengthError' };
  }

  if (!/^[a-z0-9-]+$/.test(trimmedName)) {
    return { valid: false, error: 'nameCharsError' };
  }

  if (trimmedName.startsWith('-') || trimmedName.endsWith('-')) {
    return { valid: false, error: 'nameHyphenError' };
  }

  if (RESERVED_NAMES.includes(trimmedName)) {
    return { valid: false, error: 'nameReserved' };
  }

  return { valid: true, name: trimmedName };
}
