// Contacto — form on the left (cols 1–7), channels on the right (cols 8–12).
// Submit "Enviar consulta". Right column rows separated by --line hairlines.
function Contact() {
  const [sent, setSent] = React.useState(false);

  return (
    <section
      id="contacto"
      style={{
        maxWidth: "var(--container-max)",
        margin: "0 auto",
        padding: "var(--section-gap) var(--container-pad)",
      }}
    >
      <SectionHeader number="05" eyebrow="Contacto" title="Hablemos." />

      <div
        style={{
          marginTop: "clamp(2.5rem, 5vw, 4rem)",
          display: "grid",
          gridTemplateColumns: "repeat(12, 1fr)",
          gap: "clamp(2rem, 5vw, 5rem)",
          alignItems: "start",
        }}
      >
        {/* Form — cols 1–7 */}
        <form
          onSubmit={(e) => {
            e.preventDefault();
            setSent(true);
          }}
          style={{
            gridColumn: "1 / span 7",
            display: "flex",
            flexDirection: "column",
            gap: 28,
          }}
        >
          <Field id="nombre" label="Nombre" placeholder="Tu nombre completo" required />
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 28 }}>
            <Field id="email" label="Email" type="email" placeholder="tucorreo@dominio.cl" required />
            <Field id="telefono" label="Teléfono" type="tel" placeholder="+56 9 1234 5678" />
          </div>
          <Field id="mensaje" label="Mensaje" placeholder="Cuéntanos brevemente el asunto. No incluyas información sensible aún." textarea />

          <div style={{ display: "flex", alignItems: "center", gap: 22, marginTop: 8, flexWrap: "wrap" }}>
            <button type="submit" className="btn-primary" style={{ border: 0 }}>
              {sent ? "Recibido — te escribimos en el día hábil" : "Enviar consulta"}
              {!sent && <ArrowRight width={16} height={16} />}
            </button>
            <span style={{ fontFamily: "var(--font-ui)", fontSize: 12, color: "var(--ink-subtle)", maxWidth: "40ch" }}>
              Lo que nos cuentes queda sujeto a secreto profesional.
            </span>
          </div>
        </form>

        {/* Channels — cols 8–12 */}
        <aside
          style={{
            gridColumn: "8 / span 5",
            display: "flex",
            flexDirection: "column",
            borderTop: "1px solid var(--line)",
          }}
        >
          <Channel
            label="Email"
            value="contacto@riverostorresycia.cl"
            href="mailto:contacto@riverostorresycia.cl"
            icon={<Mail />}
          />
          <Channel
            label="WhatsApp"
            value="+56 9 7890 1234"
            note="Número en activación · monitoreado por ambos socios"
            href="https://wa.me/56978901234"
            icon={<MessageCircle />}
            accent
          />
          <Channel
            label="Agenda directa"
            value="Próximamente"
            note="Integración con calendario en preparación."
            disabled
          />
          <div
            style={{
              borderTop: "1px solid var(--line)",
              padding: "clamp(1.5rem, 2.5vw, 1.75rem) 0",
            }}
          >
            <div
              style={{
                fontFamily: "var(--font-ui)",
                fontWeight: 500,
                fontSize: 11,
                letterSpacing: "0.18em",
                textTransform: "uppercase",
                color: "var(--ink-subtle)",
                marginBottom: 14,
              }}
            >
              Redes
            </div>
            <ul style={{ listStyle: "none", margin: 0, padding: 0, display: "flex", flexDirection: "column", gap: 8 }}>
              {[
                ["LinkedIn · Ignacio Riveros", "https://www.linkedin.com/in/ignacio-riveros-mu%C3%B1oz-00751b35/"],
                ["LinkedIn · Macarena Torres", "#"],
                ["Instagram · @riverostorrescia", "#"],
              ].map(([label, href]) => (
                <li key={label}>
                  <a
                    href={href}
                    style={{
                      display: "inline-flex",
                      alignItems: "baseline",
                      gap: 8,
                      color: "var(--ink)",
                      textDecoration: "none",
                      fontFamily: "var(--font-ui)",
                      fontSize: 15,
                      borderBottom: "1px solid transparent",
                      paddingBottom: 1,
                      transition: "color 250ms var(--ease-std), border-color 250ms var(--ease-std)",
                    }}
                    onMouseEnter={(e) => {
                      e.currentTarget.style.color = "var(--accent)";
                      e.currentTarget.style.borderBottomColor = "var(--accent)";
                    }}
                    onMouseLeave={(e) => {
                      e.currentTarget.style.color = "var(--ink)";
                      e.currentTarget.style.borderBottomColor = "transparent";
                    }}
                  >
                    <span>{label}</span>
                    <span>→</span>
                  </a>
                </li>
              ))}
            </ul>
          </div>
        </aside>
      </div>
    </section>
  );
}

function Field({ id, label, type = "text", placeholder, required, textarea }) {
  const Tag = textarea ? "textarea" : "input";
  return (
    <div>
      <label
        htmlFor={id}
        style={{
          display: "block",
          fontFamily: "var(--font-ui)",
          fontWeight: 500,
          fontSize: 11,
          letterSpacing: "0.15em",
          textTransform: "uppercase",
          color: "var(--ink-subtle)",
          marginBottom: 4,
        }}
      >
        {label}
        {!required && (
          <span style={{ textTransform: "none", letterSpacing: 0, color: "var(--ink-subtle)", marginLeft: 8 }}>
            (opcional)
          </span>
        )}
      </label>
      <Tag
        id={id}
        type={textarea ? undefined : type}
        placeholder={placeholder}
        required={required}
        className="input-line"
        rows={textarea ? 3 : undefined}
        style={textarea ? { resize: "vertical", minHeight: 80 } : undefined}
      />
    </div>
  );
}

function Channel({ icon, label, value, note, href, accent, disabled }) {
  const inner = (
    <div
      style={{
        display: "grid",
        gridTemplateColumns: "auto 1fr auto",
        columnGap: 16,
        rowGap: 4,
        alignItems: "center",
        padding: "clamp(1.5rem, 2.5vw, 1.75rem) 0",
        borderBottom: "1px solid var(--line)",
        color: "var(--ink)",
        textDecoration: "none",
      }}
    >
      <span
        style={{
          gridRow: "1 / span 2",
          color: accent ? "var(--accent)" : "var(--ink-muted)",
          opacity: disabled ? 0.5 : 1,
          display: "inline-flex",
          alignItems: "center",
        }}
      >
        {icon || <span style={{ width: 20, height: 20, display: "inline-block" }} />}
      </span>
      <div>
        <div
          style={{
            fontFamily: "var(--font-ui)",
            fontWeight: 500,
            fontSize: 11,
            letterSpacing: "0.15em",
            textTransform: "uppercase",
            color: "var(--ink-subtle)",
            marginBottom: 4,
          }}
        >
          {label}
        </div>
        <div
          style={{
            fontFamily: "var(--font-display)",
            fontSize: "clamp(1.0625rem, 1.4vw, 1.25rem)",
            letterSpacing: "-0.005em",
            color: disabled ? "var(--ink-subtle)" : "var(--ink)",
            lineHeight: 1.25,
          }}
        >
          {value}
        </div>
        {note && (
          <div
            style={{
              fontFamily: "var(--font-ui)",
              fontSize: 12,
              color: "var(--ink-subtle)",
              marginTop: 4,
              letterSpacing: "0.02em",
            }}
          >
            {note}
          </div>
        )}
      </div>
      {!disabled && href && (
        <span style={{ color: "var(--ink-subtle)", fontFamily: "var(--font-ui)", fontSize: 16 }}>→</span>
      )}
    </div>
  );

  if (disabled || !href) {
    return inner;
  }
  return (
    <a
      href={href}
      style={{ textDecoration: "none", color: "inherit" }}
      onMouseEnter={(e) => {
        const v = e.currentTarget.querySelector(".rt-ch-value");
        if (v) v.style.color = "var(--accent)";
      }}
    >
      {inner}
    </a>
  );
}

Object.assign(window, { Contact });
