export const PRIME_TIERS = ['subtle', 'core', 'apex'] as const
export type PrimeTier = (typeof PRIME_TIERS)[number]

export const PRIME_GRAINS = ['clean', 'gritty', 'filthy'] as const
export type PrimeGrain = (typeof PRIME_GRAINS)[number]

export const PRIME_RAINS = ['none', 'light', 'storm'] as const
export type PrimeRain = (typeof PRIME_RAINS)[number]

export const DEFAULT_AURA_COLOR = 'void-silver'

// Aura color options for UI
export const PRIME_AURA_COLORS = [
  'void-silver',
  'crimson',
  'arctic',
  'ember',
  'phantom',
  'eclipse',
] as const
export type PrimeAuraColor = (typeof PRIME_AURA_COLORS)[number]

// Aura color definitions with noir color palette influence (Layer 3-4 only)
const AURA_COLORS: Record<string, string> = {
  'void-silver': 'Eyes: muted silver-white glow, reflective and cold. Material accents: silver highlights on obsidian. Edge lighting: silver rim light.',
  'crimson': 'Eyes: deep blood-red glow, smoldering intensity. Material accents: subtle crimson undertones in shadows. Edge lighting: crimson rim light.',
  'arctic': 'Eyes: ice-blue glow, freezing calm. Material accents: blue highlights on wet surfaces. Edge lighting: pale blue rim light.',
  'ember': 'Eyes: warm orange glow, controlled burn. Material accents: amber highlights on chrome. Edge lighting: warm orange rim light.',
  'phantom': 'Eyes: pale ghostly white glow, hollow and distant. Material accents: ethereal white highlights. Edge lighting: faint white rim light.',
  'eclipse': 'Eyes: deep violet glow, cosmic and unknowable. Material accents: purple highlights in shadows. Edge lighting: purple-black rim light.',
}

// PRIME_BASE - Image-to-image transformation
const PRIME_BASE = `Transform the subject into PRIME with dramatic stylistic changes while maintaining recognizable identity. Apply noir aesthetic, moody lighting, and atmospheric effects. Enhance contrast, add shadow drama, and create cinematic atmosphere. Allow for stylistic transformation of clothing, environment, and mood while keeping the person recognizable.`

// PRIME_NEGATIVE - Avoid these elements
const PRIME_NEGATIVE = `cartoon, anime, illustration, painting, drawing, deformed, distorted, ugly, bad anatomy, extra limbs, missing limbs, blurry, low quality, jpeg artifacts, signature, watermark, text, username, logo, multiple people, crowd, group photo`

// Aura intensity - rim light energy levels
const AURA_PROMPTS: Record<PrimeTier, string> = {
  subtle: 'Soft rim lighting on edges. Faint glow outline.',
  core: 'Strong rim light tracing body contours. Visible energy crackling along edges. Floating particles.',
  apex: 'Intense blazing rim light on every contour. Massive energy beam from above. Reality-warping power. Debris and particles everywhere.',
}

// Grain toggles
const GRAIN_PROMPTS: Record<PrimeGrain, string> = {
  clean: 'Crisp detail, minimal grain.',
  gritty: 'Moderate film grain, rougher contrast.',
  filthy: 'Heavy film grain, harsh noir contrast.',
}

// Weather as atmospheric pressure
const RAIN_PROMPTS: Record<PrimeRain, string> = {
  none: '',
  light: 'Faint atmospheric haze, subtle wet sheen.',
  storm: 'Heavy atmosphere. Smoke frames face without obscuring.',
}

export function buildPrimePrompt(options: {
  tier: PrimeTier
  auraColor: string
  archetypeName?: string
  archetypeModifier?: string
  grain?: PrimeGrain
  rain?: PrimeRain
}): { prompt: string; negativePrompt: string } {
  const {
    tier,
    auraColor,
    archetypeName,
    archetypeModifier,
    grain = 'gritty',
    rain = 'light',
  } = options

  // Get aura color descriptor
  const auraDesc = AURA_COLORS[auraColor] || AURA_COLORS['void-silver']

  const toggles = [
    `EYES/AURA: ${auraDesc}`,
    AURA_PROMPTS[tier],
    GRAIN_PROMPTS[grain],
    rain !== 'none' ? RAIN_PROMPTS[rain] : '',
  ]
    .filter(Boolean)
    .join(' ')

  const archetypeAddon =
    archetypeName && archetypeModifier
      ? `Archetype: ${archetypeName}. ${archetypeModifier}. Do not change identity.`
      : ''

  const prompt = [PRIME_BASE, toggles, archetypeAddon].filter(Boolean).join('\n\n')

  return {
    prompt,
    negativePrompt: PRIME_NEGATIVE,
  }
}
