filepath = "/root/efesto-qa/source/backend/src/services/agentService.js"

with open(filepath, "r") as f:
    content = f.read()

old = """  // Construir mensaje de handoff
  const handoffMessage = `🤝 *NUEVA SOLICITUD DE CITA*

👤 *Prospecto:* ${lead?.name || conversation.remoteName || 'Sin nombre'}
📱 *Teléfono:* ${conversation.remoteNumber}
🏠 *Interés:* ${lead?.qualification?.propertyType || 'Propiedad'} en ${lead?.qualification?.location || 'Aguascalientes'}
💰 *Presupuesto:* ${lead?.qualification?.budget || 'No definido'}
🌡️ *Temperatura:* ${lead?.qualification?.temperature || 'warm'}"""

new = """  // Auto-fill lead value from matching property if not set
  if (lead && !lead.value && lead.qualification?.propertyType) {
    const matchedProp = await prisma.property.findFirst({
      where: { clientId, propertyType: { contains: lead.qualification.propertyType, mode: 'insensitive' }, deactivatedAt: null },
      select: { price: true },
      orderBy: { price: 'desc' }
    });
    if (matchedProp?.price) {
      await prisma.lead.update({ where: { id: lead.id }, data: { value: matchedProp.price } });
      lead.value = matchedProp.price;
    }
  }

  // Construir mensaje de handoff
  const handoffMessage = `🤝 *NUEVA SOLICITUD DE CITA*

👤 *Prospecto:* ${lead?.name || conversation.remoteName || 'Sin nombre'}
📱 *Teléfono:* ${conversation.remoteNumber}
🏠 *Interés:* ${lead?.qualification?.propertyType || 'Propiedad'} en ${lead?.qualification?.location || 'Aguascalientes'}
💰 *Presupuesto:* ${lead?.qualification?.budget || 'No definido'}
💵 *Valor potencial:* ${lead?.value ? '$' + Number(lead.value).toLocaleString() : 'No definido'}
🌡️ *Temperatura:* ${lead?.qualification?.temperature || 'warm'}"""

if old in content:
    content = content.replace(old, new)
    with open(filepath, "w") as f:
        f.write(content)
    print("OK - handoff value added")
else:
    print("ERROR - block not found")
