export type UserTier = 'starter' | 'professional' | 'enterprise';

export interface PlanCapabilities {
  tier: UserTier;
  supportSla: '48h' | '24h' | '8h';
  hasTokenDashboard: boolean;
  hasQualityReport: boolean;
  hasSso: boolean;
  hasSoc2Iso27001: boolean;
  hasEuAiAct: boolean;
  hasGranularRbac: boolean;
  hasOnPrem: boolean;
  hasDedicatedChannel: boolean;
  hasAccountManager: boolean;
  complianceOptions: string[];
  /** Free tokens included with the plan (resets on licence renewal). */
  includedCreditsTokens: number;
  /** Price in € to purchase one additional conversion slot beyond the plan limit. */
  extraConversionPriceEur: number;
}

const CAPABILITIES: Record<UserTier, PlanCapabilities> = {
  starter: {
    tier: 'starter',
    supportSla: '48h',
    hasTokenDashboard: true,
    hasQualityReport: false,
    hasSso: false,
    hasSoc2Iso27001: false,
    hasEuAiAct: false,
    hasGranularRbac: false,
    hasOnPrem: false,
    hasDedicatedChannel: false,
    hasAccountManager: false,
    complianceOptions: ['gdpr'],
    includedCreditsTokens: 5_000_000,
    extraConversionPriceEur: 5_000,
  },
  professional: {
    tier: 'professional',
    supportSla: '24h',
    hasTokenDashboard: true,
    hasQualityReport: true,
    hasSso: true,
    hasSoc2Iso27001: true,
    hasEuAiAct: true,
    hasGranularRbac: false,
    hasOnPrem: false,
    hasDedicatedChannel: false,
    hasAccountManager: false,
    complianceOptions: ['gdpr', 'sox', 'pci', 'hipaa', 'iso27001'],
    includedCreditsTokens: 10_000_000,
    extraConversionPriceEur: 4_000,
  },
  enterprise: {
    tier: 'enterprise',
    supportSla: '8h',
    hasTokenDashboard: true,
    hasQualityReport: true,
    hasSso: true,
    hasSoc2Iso27001: true,
    hasEuAiAct: true,
    hasGranularRbac: true,
    hasOnPrem: true,
    hasDedicatedChannel: true,
    hasAccountManager: true,
    complianceOptions: ['gdpr', 'sox', 'pci', 'hipaa', 'iso27001'],
    includedCreditsTokens: 15_000_000,
    extraConversionPriceEur: 2_500,
  },
};

export function normalizeTier(value?: string | null): UserTier {
  const lower = (value ?? '').toLowerCase();
  if (lower === 'professional' || lower === 'enterprise') return lower;
  return 'starter';
}

export function getPlanCapabilities(value?: string | null): PlanCapabilities {
  return CAPABILITIES[normalizeTier(value)];
}

const MODERN_LANGUAGES = new Set([
  'python', 'typescript', 'java', 'csharp', 'c#', 'go', 'rust', 'kotlin', 'swift', 'php', 'ruby', 'cpp', 'c++', 'dart',
  'javascript', 'c', 'scala', 'vbnet', 'objc', 'elixir', 'r', 'lua', 'perl', 'matlab',
]);
const MAINFRAME_PLUS = new Set(['assembler-zos', 'assembler', 'assembly', 'cics', 'bms', 'cl400', 'jcl', 'mumps', 'm']);

function normalizeLanguageId(language?: string | null): string {
  return (language ?? '').toLowerCase().trim().replace(/\s+/g, '-');
}

export function getLanguageComplexityMultiplier(language?: string | null): number {
  const normalized = normalizeLanguageId(language);
  if (!normalized) return 1;
  if (MODERN_LANGUAGES.has(normalized)) return 1;
  if (MAINFRAME_PLUS.has(normalized)) return 3;
  return 2.5;
}

/** Returns true for any language that requires Professional tier or above. */
export function isLegacyLanguage(languageId?: string | null): boolean {
  const normalized = normalizeLanguageId(languageId);
  if (!normalized) return false;
  return !MODERN_LANGUAGES.has(normalized);
}

