// Testimonials.jsx

Object.assign(window, { Testimonials });

const COMMENTS = [
  {
    num: 31, name: "María García", time: "Hace 3 minutos",
    avatar: "assets/images/comments/ava-sofia.png",
    text: <>Llevo 3 semanas usando <a href="#registration" onClick={e => { e.preventDefault(); const r = document.getElementById("registration"); if (r) r.scrollIntoView({ behavior: "smooth", block: "center" }); }} style={{ color: "#1a73e8", textDecoration: "none", fontWeight: 600 }}>Exacto Creditant</a>. Al principio era muy escéptica, pero esta mañana he retirado mis primeros 2.300 €. ¡No me lo puedo creer! Por primera vez en mucho tiempo veo por fin una solución a mis problemas financieros. Gracias por este artículo que me ha abierto los ojos.</>,
  },
  {
    num: 30, name: "Javier Martínez", time: "Hace 24 minutos",
    avatar: "assets/images/comments/ava-javier.png",
    text: "Al principio tenía exactamente las mismas dudas: 250 € no es poco dinero para mí. Pero después de 2 semanas ya he generado 1.450 €. Mi estrés financiero ha desaparecido por completo. No me arrepiento en absoluto de haberlo probado.",
  },
  {
    num: 29, name: "Carmen López", time: "Hace 36 minutos",
    avatar: "assets/images/comments/ava-amanda.png",
    text: "Vi el programa en La Sexta anoche. Me inscribí inmediatamente. Esta mañana: primera ganancia de 320 €. ¡Estoy totalmente en shock! Mi marido no se lo cree. Deberíamos haber empezado mucho antes.",
  },
  {
    num: 28, name: "Miguel Rodríguez", time: "Hace 92 minutos",
    avatar: "assets/images/comments/ava-marcos.png",
    text: <>Comparto mi experiencia con honestidad. Uso <a href="#registration" onClick={e => { e.preventDefault(); const r = document.getElementById("registration"); if (r) r.scrollIntoView({ behavior: "smooth", block: "center" }); }} style={{ color: "#1a73e8", textDecoration: "none", fontWeight: 600 }}>Exacto Creditant</a> desde hace 2 meses. He depositado 2.000 € en total, y ya he retirado 7.550 €. Las transacciones son siempre rápidas y precisas, nunca falta ni un céntimo. Aquí está la prueba de mi cuenta bancaria.</>,
    photo: "assets/images/comments/comment-31.png",
  },
  {
    num: 27, name: "Carlos Fernández", time: "Hace 1 hora",
    avatar: "assets/images/comments/ava-esteban.png",
    text: "Al principio tenía miedo de que fuera complicado con gráficos y todo eso. ¡Pero no! Todo es automático. Solo deposité 250 € y el sistema lo hace todo. Después de 2 semanas: 760 €. Es realmente para todo el mundo.",
  },
  {
    num: 26, name: "David Sánchez", time: "Hace 1 hora",
    avatar: "assets/images/comments/ava-oscar.png",
    text: <>Mi compañero me habló de <a href="#registration" onClick={e => { e.preventDefault(); const r = document.getElementById("registration"); if (r) r.scrollIntoView({ behavior: "smooth", block: "center" }); }} style={{ color: "#1a73e8", textDecoration: "none", fontWeight: 600 }}>Exacto Creditant</a> hace un mes. Dudé, pero lo intenté. Resultado: 2.100 € en 3 semanas. ¡Increíble! Acabo de saldar mi descubierto bancario. Lo recomiendo a todo el mundo.</>,
  },
  {
    num: 24, name: "Laura Moreno", time: "Hace 1 hora",
    avatar: "assets/images/comments/ava-maria.png",
    text: <>Vi el programa en La Sexta. Seguí el consejo y me inscribí hace 3 días. Primera ganancia: 180 €. ¡Es increíble lo rápido que es! Gracias por compartir este artículo, va a cambiar mi vida.</>,
  },
  {
    num: 23, name: "Roberto Gil", time: "Hace 1 hora",
    avatar: "assets/images/comments/ava-erik.png",
    text: "Primer hito alcanzado: 3.200 € generados en 5 semanas. No me lo creía al principio, pero es real. El sistema es realmente fácil de usar. Todo es automático. Lo recomiendo sin dudar.",
  },
  {
    num: 22, name: "Pablo Torres", time: "Hace 2 horas",
    avatar: "assets/images/comments/ava-luis.png",
    text: <>Dudé mucho con el Bitcoin y el trading. Demasiado complicado para mí. Pero <a href="#registration" onClick={e => { e.preventDefault(); const r = document.getElementById("registration"); if (r) r.scrollIntoView({ behavior: "smooth", block: "center" }); }} style={{ color: "#1a73e8", textDecoration: "none", fontWeight: 600 }}>Exacto Creditant</a> lo cambia todo: es automático. Después de 2 semanas: 670 €. ¡Por fin una solución simple que funciona de verdad!</>,
  },
  {
    num: 19, name: "Isabel Ruiz", time: "Hace 2 horas",
    avatar: "assets/images/avatars/women6.webp.png",
    text: <>Mi marido usa <a href="#registration" onClick={e => { e.preventDefault(); const r = document.getElementById("registration"); if (r) r.scrollIntoView({ behavior: "smooth", block: "center" }); }} style={{ color: "#1a73e8", textDecoration: "none", fontWeight: 600 }}>Exacto Creditant</a> desde hace 3 semanas. Ya he generado 1.430 €. Se ha convertido en un complemento de ingresos increíble para nuestra familia. Por fin podemos respirar financieramente.</>,
  },
  {
    num: 18, name: "Andrés Jiménez", time: "Hace 2 horas",
    avatar: "assets/images/avatars/ava-23.webp.png",
    text: "Empecé con precaución con 300 €. Después de un mes, reinvertí 200 € de mis ganancias. Hoy mi cuenta muestra 2.100 €. El sistema funciona, pero hay que ser paciente y dejar que la IA trabaje.",
  },
  {
    num: 16, name: "Mark Fadlevich", time: "Hace 5 horas",
    avatar: "assets/images/avatars/women9.webp.png",
    text: "Empecé con precaución con 300 €. Después de un mes, reinvertí 200 € de mis ganancias. Hoy mi cuenta muestra 2.100 €. El sistema funciona, pero hay que ser paciente y dejar que la IA trabaje.",
  },
];

const AVATAR_COLORS = ["#e8a87c","#85c1e9","#82e0aa","#f1948a","#bb8fce","#76d7c4","#f7dc6f","#aab7b8"];

function Avatar({ src, name, initials, size = 40 }) {
  const [err, setErr] = React.useState(false);
  const letter = initials ? initials[0] : (name ? name[0] : "?");
  const color  = AVATAR_COLORS[(name || "").charCodeAt(0) % AVATAR_COLORS.length];

  if (!src || err) {
    return (
      <div style={{ width: size, height: size, borderRadius: "50%", background: color, display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
        <span style={{ fontFamily: "Arial, sans-serif", fontWeight: 700, fontSize: size * 0.38, color: "#fff", textTransform: "uppercase" }}>{letter}</span>
      </div>
    );
  }
  return (
    <img src={src} alt={name} onError={() => setErr(true)}
      style={{ width: size, height: size, borderRadius: "50%", objectFit: "cover", flexShrink: 0 }} />
  );
}

function Testimonials() {
  const scrollToReg = () => {
    const reg = document.getElementById("registration");
    if (reg) {
      reg.scrollIntoView({ behavior: "smooth", block: "center" });
    } else {
      const btn = document.querySelector(".cta-btn");
      if (btn) {
        btn.click();
        setTimeout(() => {
          const reg2 = document.getElementById("registration");
          if (reg2) reg2.scrollIntoView({ behavior: "smooth", block: "center" });
        }, 350);
      }
    }
  };

  return (
    <div style={{ width: "100%", marginTop: 36, paddingTop: 8 }}>
      <h2 style={{ fontFamily: "'Times New Roman', serif", fontWeight: 700, fontSize: 22, color: "#111", marginBottom: 20 }}>
        Comentarios
      </h2>

      {COMMENTS.map((c, i) => (
        <div key={i} style={{ marginBottom: 20, paddingBottom: 20, borderBottom: "1px solid #f0f0f0" }}>
          <div style={{ display: "flex", alignItems: "flex-start", gap: 10 }}>
            <Avatar src={c.avatar} name={c.name} initials={c.initials} size={40} />
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ display: "flex", alignItems: "baseline", justifyContent: "space-between", marginBottom: 2 }}>
                <span style={{ fontFamily: "Arial, sans-serif", fontWeight: 700, fontSize: 14, color: "#111" }}>{c.name}</span>
                <span style={{ fontFamily: "Arial, sans-serif", fontWeight: 700, fontSize: 14, color: "#bbb", flexShrink: 0, marginLeft: 8 }}>#{c.num}</span>
              </div>
              <div style={{ fontFamily: "Arial, sans-serif", fontSize: 12, color: "#999", marginBottom: 7 }}>{c.time}</div>
              <p style={{ fontFamily: "'Times New Roman', serif", fontSize: 16, color: "#202020", lineHeight: 1.6, margin: 0 }}>
                {c.text}
              </p>
              {c.photo && (
                <img src={c.photo} alt="Extracto bancario"
                  style={{ maxWidth: 320, width: "100%", height: "auto", borderRadius: 8, marginTop: 10, display: "block" }}
                  onError={e => e.target.style.display = "none"} />
              )}
            </div>
          </div>
        </div>
      ))}

      {/* Bottom CTA */}
      <div style={{ background: "#f9f9f9", border: "1px solid #e8e8e8", borderRadius: 8, padding: "20px 20px 22px", marginTop: 8, textAlign: "center" }}>
        <p style={{ fontFamily: "'Times New Roman', serif", fontSize: 16, color: "#333", lineHeight: 1.65, marginBottom: 18 }}>
          Tras la emisión del programa en La Sexta, la demanda de inscripciones ha aumentado considerablemente. Para garantizar un acompañamiento personalizado de calidad, las nuevas inscripciones están temporalmente limitadas a <strong>250 plazas por día</strong>.
        </p>
        <button onClick={scrollToReg} style={{
          display: "inline-flex", alignItems: "center", justifyContent: "center", gap: 8,
          background: "#1a3a6e", color: "#fff", border: "none", cursor: "pointer",
          fontFamily: "Arial, sans-serif", fontWeight: 700, fontSize: 15,
          padding: "14px 32px", borderRadius: 6, textTransform: "uppercase",
          letterSpacing: 0.5,
        }}>
          → ME INSCRIBO EN LA PLATAFORMA
        </button>
      </div>
    </div>
  );
}
