// Card components used across the marketing site.

const { useState: useState_C } = React;

function CategoryMotif({ motif, active, icon, accent }) {
  const color = active ? 'var(--sa-yellow-mid)' : 'var(--sa-purple)';
  if (motif === 'sparkle') return <Sparkle size={104} color={color} />;
  if (motif === 'pinwheel') return <Pinwheel size={104} color={color} />;
  if (motif === 'burst') return <DiamondBurst size={104} color={color} />;
  return (
    <span className="inline-flex items-center justify-center rounded-full"
          style={{ width: 82, height: 82, background: active ? '#fff' : accent }}>
      <Icon name={icon} size={42} color={active ? 'var(--sa-purple)' : '#fff'} />
    </span>
  );
}

// ── ValueCard — used on Home + About "What We Stand For" ───────────
function ValueCard({ icon, title, body }) {
  return (
    <div className="flex flex-col gap-5 max-w-[340px]">
      <span className="sa-icon-disc" style={{ background: 'var(--sa-lavender-50)', color: 'var(--sa-purple)' }}>
        <Icon name={icon} size={28} />
      </span>
      <div className="flex flex-col gap-2">
        <h4 className="font-bold text-xl text-sa-ink m-0" style={{ lineHeight: 1.25 }}>{title}</h4>
        <p className="text-[15px] leading-relaxed text-sa-slate-2 m-0">{body}</p>
      </div>
    </div>
  );
}

// ── PrincipleCard — used in About "Built On Principles" ─────────
function PrincipleCard({ icon, title, body, active = false, onHover, onLeave }) {
  return (
    <div
      onMouseEnter={onHover}
      onMouseLeave={onLeave}
      className="principle-card rounded-sa-md p-6 md:p-7 flex flex-col gap-4 cursor-pointer card-hover transition-colors"
      style={{
        background: active ? 'var(--sa-purple)' : '#fff',
        color: active ? '#fff' : 'var(--sa-ink)',
      }}>
      <span className="inline-flex items-center justify-center rounded-full"
            style={{ width: 48, height: 48, background: active ? 'rgba(255,255,255,0.18)' : 'var(--sa-lavender-50)' }}>
        <Icon name={icon} size={26} color={active ? '#fff' : 'var(--sa-purple)'} />
      </span>
      <div className="flex flex-col gap-2">
        <h4 className="font-bold text-lg m-0" style={{ color: active ? '#fff' : 'var(--sa-ink)' }}>{title}</h4>
        <p className="text-sm leading-relaxed m-0"
           style={{ color: active ? 'rgba(255,255,255,0.88)' : 'var(--sa-slate-2)' }}>{body}</p>
      </div>
    </div>
  );
}

// ── StepCard — for the "How It Works" purple cards on yellow bg ────
function StepCard({ icon, title, body, num }) {
  return (
    <article className="home-step-card">
      <span className="home-step-card__watermark" aria-hidden="true">{num}</span>
      <div className="home-step-card__content">
        <span className="home-step-card__icon inline-flex items-center justify-center">
          <Icon name={icon} size={34} color="var(--sa-purple)" />
        </span>
        <div className="home-step-card__copy">
          <h4>{title}</h4>
          <p>{body}</p>
        </div>
      </div>
    </article>
  );
}

// ── CategoryCard — Personalised Support trio (Home) ────────────────
function CategoryCard({ icon, title, active = false, accent = '#A181F9', motif, onHover, onLeave, href }) {
  const isPurple = active;
  return (
    <a
      href={href || '#'}
      onMouseEnter={onHover}
      onMouseLeave={onLeave}
      className="home-service-card rounded-sa-md flex flex-col justify-between cursor-pointer card-hover no-underline"
      style={{
        background: isPurple ? 'var(--sa-purple)' : '#fff',
        color: isPurple ? '#fff' : 'var(--sa-ink)',
      }}>
      <h4 className="home-service-card__title font-bold m-0">{title}</h4>

      <div className="home-service-card__bottom flex items-end justify-between">
        <span className="home-service-card__arrow inline-flex items-center justify-center rounded-full"
              style={{
                background: isPurple ? 'rgba(255,255,255,0.15)' : 'var(--sa-lavender-50)',
              }}>
          <Icon name="arrow_forward" size={22} color={isPurple ? '#fff' : 'var(--sa-purple)'} />
        </span>
        <span className="home-service-card__motif" aria-hidden="true">
          <CategoryMotif motif={motif} active={isPurple} icon={icon} accent={accent} />
        </span>
      </div>
    </a>
  );
}

// ── FAQRow — toggleable Q/A row ────────────────────────────────────
function FAQRow({ q, a, defaultOpen, color = 'purple' }) {
  const [open, setOpen] = useState_C(!!defaultOpen);
  const qColor = color === 'purple' ? 'var(--sa-purple)' : '#000';
  return (
    <div className="border-b border-sa-divider">
      <button
        onClick={() => setOpen(!open)}
        className="w-full text-left flex items-start gap-4 py-6 bg-transparent border-0 cursor-pointer">
        <div className="flex-1 font-bold m-0" style={{ font: '700 18px/1.35 var(--sa-font-sans)', color: qColor }}>
          Q: {q}
        </div>
        <span className="flex-shrink-0 inline-flex items-center justify-center"
              style={{ width: 28, height: 28, color: 'var(--sa-purple)', transition: 'transform 0.3s var(--sa-ease)', transform: open ? 'rotate(180deg)' : 'none' }}>
          <Icon name={open ? 'remove' : 'add'} size={28} />
        </span>
      </button>
      <div className={`faq-body ${open ? 'is-open' : ''}`}>
        <div>
          <p className="pb-6 pr-10 m-0" style={{ font: '400 15px/1.65 var(--sa-font-sans)', color: 'var(--sa-faq-text)' }}>
            A: {a}
          </p>
        </div>
      </div>
    </div>
  );
}

// ── ProgramSummaryCard (Services page top section) ────────────────
function ProgramSummaryCard({ image, title, subtitle, body, sessions, level, audience, mode, onView }) {
  return (
    <div className="program-summary-card grid gap-6 items-start">
      <div className="program-summary-card__image rounded-sa-md overflow-hidden bg-sa-lavender">
        {image ? (
          <img src={image} alt={title} className="w-full h-full object-cover" />
        ) : (
          <div className="w-full h-full flex items-center justify-center text-sa-slate-3">
            <Icon name="image" size={48} />
          </div>
        )}
      </div>
      <div className="program-summary-card__content flex flex-col gap-3">
        <h3 className="font-bold m-0" style={{ font: '700 22px/1.25 var(--sa-font-sans)', color: 'var(--sa-ink)' }}>{title}</h3>
        <p className="m-0" style={{ font: '400 15px/1.55 var(--sa-font-sans)', color: 'var(--sa-slate-2)' }}>{body}</p>
        <div className="program-summary-card__meta flex flex-wrap gap-x-5 gap-y-2 text-sa-slate-2 text-sm">
          <span className="inline-flex items-center gap-1.5"><Icon name="menu_book" size={16} color="var(--sa-purple)"/>{sessions}</span>
          <span className="inline-flex items-center gap-1.5"><Icon name="signal_cellular_alt" size={16} color="var(--sa-purple)"/>{level}</span>
          <span className="inline-flex items-center gap-1.5"><Icon name="school" size={16} color="var(--sa-purple)"/>{audience}</span>
          <span className="inline-flex items-center gap-1.5"><Icon name="person" size={16} color="var(--sa-purple)"/>{mode}</span>
        </div>
        <div className="program-summary-card__actions">
          <Btn variant="primary" size="sm" onClick={onView}>View Details</Btn>
          <Btn variant="purple" size="sm" href={DISCOVERY_CALL_HREF}>Register your Interest</Btn>
        </div>
      </div>
    </div>
  );
}

// ── CourseCard — for Services page service grids ──────────────────
function CourseCard({ image, title, price, body, points = [], duration, audience, mode, cta = 'Book a Session', onClick, ctaHref }) {
  return (
    <article className="flex flex-col gap-3 card-hover rounded-sa-md p-3 bg-white border border-sa-border">
      <div className="service-card__image rounded-[14px] overflow-hidden bg-sa-cream">
        {image
          ? <img src={image} alt={title} className="w-full h-full object-cover" />
          : <div className="w-full h-full flex items-center justify-center text-sa-slate-3"><Icon name="image" size={40} /></div>}
      </div>
      <div className="px-1 pt-1 flex flex-col gap-2.5">
        <div className="flex items-start justify-between gap-3">
          <h4 className="font-bold m-0 text-sa-ink" style={{ font: '700 18px/1.3 var(--sa-font-sans)' }}>{title}</h4>
          <span className="font-bold text-sa-purple whitespace-nowrap" style={{ fontSize: 18 }}>{price}</span>
        </div>
        <p className="m-0 text-sm leading-relaxed text-sa-slate-2">{body}</p>
        {points.length > 0 && (
          <ul className="m-0 p-0 list-none flex flex-col gap-1.5 mt-1">
            {points.map((p, i) => (
              <li key={i} className="flex items-start gap-2 text-sm text-sa-slate-2">
                <span className="text-sa-purple font-bold mt-0.5">✓</span>
                <span>{p}</span>
              </li>
            ))}
          </ul>
        )}
        <div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-[13px] text-sa-slate-2 mt-1">
          <span className="inline-flex items-center gap-1"><Icon name="schedule" size={15} color="var(--sa-purple)" />{duration}</span>
          <span className="inline-flex items-center gap-1"><Icon name="school" size={15} color="var(--sa-purple)" />{audience}</span>
          <span className="inline-flex items-center gap-1"><Icon name="person" size={15} color="var(--sa-purple)" />{mode}</span>
        </div>
        <div className="pt-2">
          <Btn variant="primary" size="sm" href={ctaHref} onClick={onClick} fullWidth>{cta}</Btn>
        </div>
      </div>
    </article>
  );
}

// ── ServiceCategoryCard — used on Services proofreading section ───
function ProofreadingCard({ image, title, body, points = [], audience, support }) {
  return (
    <article className="flex flex-col gap-3 card-hover">
      <div className="service-card__image rounded-sa-md overflow-hidden bg-sa-cream">
        {image
          ? <img src={image} alt={title} className="w-full h-full object-cover" />
          : <div className="w-full h-full flex items-center justify-center text-sa-slate-3"><Icon name="image" size={40} /></div>}
      </div>
      <div className="flex flex-col gap-2.5 px-1">
        <h4 className="font-bold m-0 text-sa-ink" style={{ font: '700 18px/1.3 var(--sa-font-sans)' }}>{title}</h4>
        <p className="m-0 text-sm leading-relaxed text-sa-slate-2">{body}</p>
        <ul className="m-0 p-0 list-none flex flex-col gap-1.5">
          {points.map((p, i) => (
            <li key={i} className="flex items-start gap-2 text-sm text-sa-slate-2">
              <span className="text-sa-purple font-bold mt-0.5">✓</span>
              <span>{p}</span>
            </li>
          ))}
        </ul>
        <div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-[13px] text-sa-slate-2 mt-1">
          <span className="inline-flex items-center gap-1"><Icon name="school" size={15} color="var(--sa-purple)" />{audience}</span>
          <span className="inline-flex items-center gap-1"><Icon name="chat_bubble_outline" size={15} color="var(--sa-purple)" />{support}</span>
        </div>
        <Btn variant="primary" size="sm" href="/contact">Get in Touch</Btn>
      </div>
    </article>
  );
}

// ── StatChartCard — gauge arc (animated) ───────────────────────────
function StatChartCard({ percent = 100, label = 'Satisfaction Rate' }) {
  const [ref, vis] = useInView();
  const [draw, setDraw] = useState_C(0);
  React.useEffect(() => {
    if (!vis) return;
    let t0 = performance.now();
    const dur = 1200;
    let raf;
    const tick = (t) => {
      const p = Math.min(1, (t - t0) / dur);
      setDraw(p);
      if (p < 1) raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [vis]);

  const total = 300; // approx arc length
  return (
    <div ref={ref} className="home-stat-card bg-white border border-sa-border rounded-2xl flex flex-col items-center justify-center relative">
      <svg viewBox="0 0 220 130" className="home-stat-card__gauge absolute left-1/2 -translate-x-1/2 overflow-visible" aria-hidden="true">
        <path d="M 14 120 A 96 96 0 0 1 206 120"
              fill="none" stroke="#F3EFFE" strokeWidth="14" strokeLinecap="round" />
        <path d="M 14 120 A 96 96 0 0 1 206 120"
              fill="none" stroke="#C6F264" strokeWidth="14" strokeLinecap="round"
              strokeDasharray={total} strokeDashoffset={total * (1 - draw)} />
      </svg>
      <div className="font-bold text-sa-ink mt-7" style={{ fontSize: 32, lineHeight: 1 }}>
        <CountUp to={percent} suffix="%" />
      </div>
      <div className="text-sa-slate-4 mt-2" style={{ font: '400 12px/1.4 var(--sa-font-sans)' }}>{label}</div>
    </div>
  );
}

// ── UsersCard — stacked avatars + caption ──────────────────────────
function UsersCard({ caption = 'More than 80+ students mentored around the world' }) {
  const avatars = [
    { img: HOME_AVATARS.stat1, bg: '#E4E7EC', color: '#98A2B3' },
    { img: HOME_AVATARS.stat2, bg: '#D6BBFB', color: '#fff' },
    { img: HOME_AVATARS.stat3, bg: '#FECDD3', color: '#fff' },
  ];
  return (
    <div className="home-users-card bg-white border border-sa-border rounded-2xl flex flex-col justify-center gap-3">
      <div className="home-users-card__avatars flex">
        {avatars.map((a, i) => (
          <span key={i} className="inline-flex items-center justify-center rounded-full border-[3px] border-white"
                style={{
                  width: 40,
                  height: 40,
                  background: a.bg,
                  backgroundImage: a.img ? `url(${a.img})` : undefined,
                  backgroundSize: 'cover',
                  backgroundPosition: 'center',
                  color: a.color,
                  marginLeft: i === 0 ? 0 : -8,
                  boxShadow: '0 1px 2px rgba(16,24,40,.06)',
                }}>
            {a.img ? null : <Icon name="person" size={22} />}
          </span>
        ))}
        <span className="inline-flex items-center justify-center rounded-full border-[3px] border-white"
              style={{ width: 40, height: 40, background: 'var(--sa-lime)', color: '#fff', marginLeft: -8, boxShadow: '0 1px 2px rgba(16,24,40,.06)' }}>
          <Icon name="public" size={22} />
        </span>
      </div>
      <p className="m-0 text-sa-slate-2" style={{ font: '400 13px/1.4 var(--sa-font-sans)' }}>{caption}</p>
    </div>
  );
}

// ── Avatar bubble (for "Still Have Questions" CTAs) ───────────────
function AvatarBubble({ image, size = 64, style }) {
  return (
    <span className="rounded-full bg-white inline-flex items-center justify-center"
          style={{
            width: size, height: size,
            border: '3px solid #fff',
            backgroundImage: image ? `url(${image})` : undefined,
            backgroundSize: 'cover', backgroundPosition: 'center',
            boxShadow: '0 8px 24px -8px rgba(16,24,40,0.18)',
            ...style,
          }} />
  );
}

Object.assign(window, {
  ValueCard, PrincipleCard, StepCard, CategoryCard, FAQRow,
  ProgramSummaryCard, CourseCard, ProofreadingCard,
  StatChartCard, UsersCard, AvatarBubble,
});
