import type { CharacterDNA, MasterJSONPrompt } from '../../types/character.js';

/**
 * Builds a subject description string from CharacterDNA.
 * Includes physical features: skin HEX, hair details, body type, face anatomy.
 */
export function buildSubjectDescription(dna: CharacterDNA): string {
  const { identity, physicalMeasurements, skinMap, faceAnatomy, hairDNA, bodyBiometrics, expressionMotion } = dna;

  const parts: string[] = [
    `${identity.age} year old ${identity.nationality} ${identity.occupation}`,
    `${physicalMeasurements.height_cm}cm tall`,
    `${physicalMeasurements.body_type} ${bodyBiometrics.body_type_detail} body`,
    `skin tone ${skinMap.base} with shadow ${skinMap.shadow} and highlight ${skinMap.highlight}`,
    `${faceAnatomy.eye_shape} ${faceAnatomy.eye_color} eyes`,
    `${faceAnatomy.eyebrow_shape} eyebrows`,
    `${faceAnatomy.nose_type} nose`,
    `${faceAnatomy.lip_shape} lips`,
    `${faceAnatomy.cheekbones} cheekbones`,
    `${faceAnatomy.jawline} jawline`,
    `${hairDNA.length} ${hairDNA.texture} ${hairDNA.style} hair colored ${hairDNA.root_color} to ${hairDNA.mid_color} with ${hairDNA.highlight_color} highlights`,
    `${expressionMotion.default_expression} expression with ${expressionMotion.smile_type} smile`,
    `${expressionMotion.gaze_style} gaze`,
  ];

  if (faceAnatomy.distinctive_marks.length > 0) {
    parts.push(`distinctive marks: ${faceAnatomy.distinctive_marks.join(', ')}`);
  }

  return parts.join(', ');
}

/**
 * Builds camera settings string from CharacterDNA technical settings.
 * Includes camera body, portrait lens, full body lens, and film stock.
 */
export function buildCameraSettings(dna: CharacterDNA): string {
  const { camera_body, portrait_lens, full_body_lens, film_stock } = dna.technicalSettings;
  return `shot on ${camera_body}, ${portrait_lens} portrait lens, ${full_body_lens} full body lens, ${film_stock} film stock`;
}

/**
 * Builds a complete MasterJSONPrompt from CharacterDNA.
 */
export function buildMasterPrompt(dna: CharacterDNA): MasterJSONPrompt {
  const now = new Date().toISOString();

  return {
    characterId: dna.id,
    subject: buildSubjectDescription(dna),
    clothing: buildClothingDescription(dna),
    environment: buildEnvironmentDescription(dna),
    lighting: buildLightingDescription(dna),
    camera: buildCameraSettings(dna),
    aesthetic: buildAestheticDescription(dna),
    negative_prompt: buildNegativePrompt(),
    createdAt: now,
    updatedAt: now,
  };
}

function buildClothingDescription(dna: CharacterDNA): string {
  const { fashionStyle } = dna;
  const colorList = fashionStyle.main_colors.join(', ');
  const pieces = fashionStyle.signature_pieces.join(', ');
  return `${fashionStyle.aesthetic} style, wearing ${pieces}, main colors ${colorList}`;
}

function buildEnvironmentDescription(dna: CharacterDNA): string {
  const { profileView } = dna;
  return `${profileView.best_angle} angle, ${profileView.signature_pose} pose, professional studio setting`;
}

function buildLightingDescription(dna: CharacterDNA): string {
  const { makeupStyle } = dna;
  return `soft natural lighting, ${makeupStyle.skin_finish} skin finish, makeup colors ${makeupStyle.colors.primary} ${makeupStyle.colors.secondary} ${makeupStyle.colors.accent}`;
}

function buildAestheticDescription(dna: CharacterDNA): string {
  const { makeupStyle, fashionStyle } = dna;
  return `${makeupStyle.philosophy} makeup, ${fashionStyle.aesthetic} fashion aesthetic`;
}

function buildNegativePrompt(): string {
  return 'blurry, low quality, distorted, deformed, ugly, bad anatomy, bad proportions, extra limbs, disfigured, watermark, text, signature';
}
