/** Flat list price: €10,000 per 1M tokens (aligned with Cost Approval UI). */
export const EUR_PER_MILLION_TOKENS = 10_000;

/**
 * Base tokens consumed per line of source code across the full AI pipeline:
 * migration pass (~30 tok), repair iterations (~25 tok), test generation (~15 tok),
 * scaffold/planning/validation overhead (~10 tok). Total ~80 tok/LOC.
 * No per-language complexity multiplier is applied — pricing is language-agnostic.
 */
export const TOK_PER_LINE = 80;

/**
 * Overhead multiplier applied on top of the base migration estimate to cover
 * multi-pass repair loop, sandbox execution, unit/integration test generation,
 * and deployment scaffold (~65% more tokens on average).
 * Applied only in the pre-migration cost approval screen.
 */
export const PIPELINE_OVERHEAD_MULTIPLIER = 1.65;

export function estimateConversionTokens(input: {
  tokensUsed?: number | null;
  /** effectiveLoc = code lines only (no blanks/comments) — preferred pricing basis. */
  effectiveLoc?: number | null;
  totalLines?: number | null;
  sourceLanguage?: string | null;
  /** estimatedLOC from wizard config JSONB — used before pre-analysis runs */
  estimatedLOC?: number | string | null;
}): number {
  if (input.tokensUsed != null && input.tokensUsed > 0) return input.tokensUsed;
  // Pricing is language-agnostic: no per-language complexity multiplier.
  // Price on code lines (effLOC) when available — excludes blanks/comments.
  if (input.effectiveLoc != null && input.effectiveLoc > 0) {
    return Math.round(input.effectiveLoc * TOK_PER_LINE);
  }
  const lines = input.totalLines ?? 0;
  if (lines > 0) return Math.round(lines * TOK_PER_LINE);
  const wizardLOC = typeof input.estimatedLOC === 'string'
    ? parseInt(input.estimatedLOC, 10)
    : (input.estimatedLOC ?? 0);
  if (wizardLOC > 0) return Math.round(wizardLOC * TOK_PER_LINE);
  return 100_000;
}

export function grossEurCentsFromTokens(tokens: number): number {
  return Math.round((tokens / 1_000_000) * EUR_PER_MILLION_TOKENS * 100);
}

/** Display helper: euros (not cents) for a token amount, at the flat licence rate. */
export function eurFromTokens(tokens: number): number {
  return grossEurCentsFromTokens(tokens) / 100;
}

/** Format an amount in euros (not cents) with the it/de grouping used across the billing UI. */
export function formatEur(eur: number): string {
  return new Intl.NumberFormat('de-DE', {
    style: 'currency',
    currency: 'EUR',
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(eur);
}

/**
 * Apply remaining license credits (as token units) against an estimated usage.
 * Credits reduce € at the flat rate 1M tokens = EUR_PER_MILLION_TOKENS.
 */
export function applyLicenseCreditsToEstimate(
  estimatedTokens: number,
  licenseCreditsRemainingTokens: number,
): { grossEurCents: number; creditsTokensApplied: number; netEurCents: number } {
  const grossEurCents = grossEurCentsFromTokens(estimatedTokens);
  const creditsTokensApplied = Math.min(
    Math.max(0, licenseCreditsRemainingTokens),
    estimatedTokens,
  );
  const appliedEurCents = grossEurCentsFromTokens(creditsTokensApplied);
  const netEurCents = Math.max(0, grossEurCents - appliedEurCents);
  return { grossEurCents, creditsTokensApplied, netEurCents };
}
