/**
 * AI Prompt Builder for Interview Media Prep Content Translation
 *
 * Translates all translatable text fields in an Interview Media Prep session
 * (questions, answers, coaching tips, follow-ups, etc.) from one language to
 * another. Follows the same pattern as bookPrompts.buildTranslateContentPrompt.
 */

// ============================================
// TYPES
// ============================================

export interface TranslatePromptResult {
  systemPrompt: string;
  userPrompt: string;
  maxTokens: number;
}

// ============================================
// CONSTANTS
// ============================================

const JSON_INSTRUCTION = '\n\nCRITICAL OUTPUT FORMAT RULES:\n1. Respond with ONLY a single valid JSON object.\n2. Do NOT wrap in markdown code fences (no ```json``` or ``` blocks).\n3. Do NOT include any text, explanation, or commentary before or after the JSON.\n4. Ensure all strings are properly escaped. Ensure all arrays and objects are properly closed.\n5. If the response is too long, reduce detail per item rather than producing broken JSON.';

// ============================================
// LANGUAGE INSTRUCTION BUILDER
// ============================================

function buildLanguageInstruction(language: string): string {
  const langLower = language.toLowerCase();
  if (langLower === 'hindi') {
    return '\n\nIMPORTANT LANGUAGE REQUIREMENT: Translate ALL text values into Hindi using Devanagari script (हिंदी देवनागरी लिपि). Do NOT use English anywhere except for JSON field names. All translated text values must be natural, fluent Hindi appropriate for interview preparation contexts in India.';
  }
  if (langLower === 'marathi') {
    return '\n\nIMPORTANT LANGUAGE REQUIREMENT: Translate ALL text values into Marathi using Devanagari script (मराठी देवनागरी लिपि). Do NOT use English anywhere except for JSON field names. All translated text values must be natural, fluent Marathi appropriate for interview preparation contexts in Maharashtra, India.';
  }
  if (langLower === 'english') {
    return '\n\nIMPORTANT LANGUAGE REQUIREMENT: Translate ALL text values into natural, fluent English. If the source text is in a non-English language (e.g., Hindi, Marathi), translate it to professional B2B English suitable for interview preparation. Do NOT use any non-English script in the translated values.';
  }
  return `\n\nIMPORTANT LANGUAGE REQUIREMENT: Translate ALL text values into ${language}. Only JSON field names should remain in English.`;
}

// ============================================
// TRANSLATION PROMPT BUILDER
// ============================================

/**
 * Build a translation prompt for Interview Media Prep session content.
 *
 * @param fields  A flat Record<string, string> of translatable text fields,
 *                e.g. { "question_0_question": "What is your strategy?", ... }
 * @param targetLanguage  The language to translate into (e.g. "hindi", "marathi", "english")
 * @returns  System prompt, user prompt, and max tokens for the AI call
 */
export function buildTranslateImpContentPrompt(
  fields: Record<string, string>,
  targetLanguage: string
): TranslatePromptResult {
  const languageInstruction = buildLanguageInstruction(targetLanguage);

  const systemPrompt = `You are a professional translator specializing in interview preparation and B2B communication content. Translate all provided text fields into the specified target language. Preserve the original meaning, tone, formatting, and structure exactly. Do NOT add, remove, or rewrite any content — only translate. Do NOT translate JSON field names — only translate the values.${languageInstruction}${JSON_INSTRUCTION}`;

  const fieldEntries = Object.entries(fields)
    .filter(([, value]) => value && typeof value === 'string' && value.trim())
    .map(([key, value]) => `**${key}:** ${value}`)
    .join('\n');

  const fieldNames = Object.keys(fields).filter(
    k => fields[k] && typeof fields[k] === 'string' && fields[k].trim()
  );

  const userPrompt = `Translate the following interview preparation content fields to ${targetLanguage}. Keep the same meaning and structure — only change the language.\n\n${fieldEntries}\n\nReturn a JSON object with these exact keys and their translated values:\n{ "${fieldNames.join('", "')}" }`;

  return { systemPrompt, userPrompt, maxTokens: 32000 };
}