// CodeB meet -- React button + iframe wrapper
// Docs: https://www.aloaha.com/meet-embed-cookbook.html
// European Digital Identity Wallet compatible.

import React from "react";

export function MeetButton({
  tenantOrigin = "https://phone.your-tenant.example",
  room,
  dial,
  trunk,
  relay = false,
  label = "Meet us now (video call)"
}) {
  const url = buildMeetUrl(tenantOrigin, { room, dial, trunk, relay });
  return (
    <a href={url} target="_blank" rel="noopener"
       style={{
         display: "inline-block", background: "#f5a524", color: "#0a0d12",
         padding: ".7rem 1.2rem", borderRadius: 8, fontWeight: 600,
         textDecoration: "none"
       }}>
      {label}
    </a>
  );
}

export function MeetIframe({
  tenantOrigin = "https://phone.your-tenant.example",
  room = "sales-demo",
  width = 1180, height = 720
}) {
  const src = `${tenantOrigin}/room.html?room=${encodeURIComponent(room)}`;
  return (
    <iframe
      src={src}
      title="CodeB meet"
      allow="microphone; camera; autoplay; clipboard-write; display-capture"
      width={width} height={height}
      style={{ border: 0, maxWidth: "100%" }}
    />
  );
}

function buildMeetUrl(origin, o = {}) {
  const room = (o.room || randomRoom()).toLowerCase().replace(/[^a-z0-9\-_]/g, "-");
  let u = origin.replace(/\/+$/, "") + "/room.html?room=" + encodeURIComponent(room);
  if (o.dial)  u += "&dial="  + encodeURIComponent(o.dial);
  if (o.trunk) u += "&trunk=" + encodeURIComponent(o.trunk);
  if (o.relay) u += "&relay=1";
  return u;
}
function randomRoom() {
  const c = "abcdefghijklmnopqrstuvwxyz23456789";
  let s = ""; for (let i = 0; i < 8; i++) s += c[Math.floor(Math.random() * c.length)];
  return s;
}
