/* global React */

const TEAM_ROLES = [
  {
    no: "01", role: "Photographer",
    accent: "var(--peacock-bright)",
    duties: [
      "Coverage of daily activities, public meetings and events",
      "Campaign visuals and on-ground photography",
      "Building a strong visual record of the candidate's journey"
    ]
  },
  {
    no: "02", role: "Videographer",
    accent: "var(--gold)",
    duties: [
      "Recording campaign events, speeches and public interactions",
      "Producing reels and short-form video content",
      "Full promotional video coverage across the campaign"
    ]
  },
  {
    no: "03", role: "Graphic Designer",
    accent: "var(--peacock-bright)",
    duties: [
      "Creation of campaign graphics, banners and posters",
      "Social media posts and digital creatives",
      "Consistent visual identity across all campaign materials"
    ]
  },
  {
    no: "04", role: "Video Editor",
    accent: "var(--gold)",
    duties: [
      "Editing footage from campaign events, speeches and interactions",
      "Producing reels, promotional videos and public interaction cuts",
      "Post-production delivery at campaign pace"
    ]
  },
  {
    no: "05", role: "Social Media Handler",
    accent: "var(--peacock-bright)",
    duties: [
      "Managing daily posts, analytics and platform engagement",
      "DMs, comments and public interaction across all channels",
      "Maintaining a consistent and responsive online presence"
    ]
  },
  {
    no: "06", role: "Social Media Strategist",
    accent: "var(--gold)",
    duties: [
      "Planning content strategy and campaign narratives",
      "Posting schedules, audience targeting and A/B testing",
      "Performance optimisation and weekly reporting"
    ]
  },
  {
    no: "07", role: "Content Writer",
    accent: "var(--peacock-bright)",
    duties: [
      "Scriptwriting, captions and speech drafts",
      "Press notes, manifesto content and manifestos",
      "Message drafts tailored for every platform and audience"
    ]
  },
  {
    no: "08", role: "Campaign Manager",
    accent: "var(--gold-bright)",
    duties: [
      "Overall coordination of all campaign activities",
      "Team management, strategy execution and escalation",
      "Ground coordination, daily briefings and reporting"
    ]
  },
  {
    no: "09", role: "Ads & Boosting Executive",
    accent: "var(--peacock-bright)",
    duties: [
      "Running paid advertisements and boosting targeted posts",
      "Audience segmentation, budget management and bid strategy",
      "Performance tracking, optimisation and ROI reporting"
    ]
  },
  {
    no: "10", role: "Campaign Associate",
    accent: "var(--gold)",
    duties: [
      "Supporting day-to-day campaign operations on ground",
      "Coordinating with field teams, booth workers and local units",
      "Execution support, feedback collection and status reports"
    ]
  }
];

const SLIDE_DURATION = 5000;

const TeamStructure = () => {
  const [active, setActive]   = React.useState(0);
  const [visible, setVisible] = React.useState(true);
  const [progress, setProgress] = React.useState(0);
  const autoRef     = React.useRef(null);
  const rafRef      = React.useRef(null);
  const progStart   = React.useRef(null);

  const goTo = (idx) => {
    clearTimeout(autoRef.current);
    cancelAnimationFrame(rafRef.current);
    setVisible(false);
    setTimeout(() => {
      setActive(idx);
      setVisible(true);
      setProgress(0);
    }, 200);
  };

  /* ── Auto-advance ── */
  React.useEffect(() => {
    autoRef.current = setTimeout(() => {
      goTo((active + 1) % TEAM_ROLES.length);
    }, SLIDE_DURATION);
    return () => clearTimeout(autoRef.current);
  }, [active]);

  /* ── Progress bar ── */
  React.useEffect(() => {
    setProgress(0);
    progStart.current = Date.now();
    const tick = () => {
      const p = Math.min(((Date.now() - progStart.current) / SLIDE_DURATION) * 100, 100);
      setProgress(p);
      if (p < 100) rafRef.current = requestAnimationFrame(tick);
    };
    rafRef.current = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(rafRef.current);
  }, [active]);

  const role = TEAM_ROLES[active];

  return (
    <section id="team-structure" style={{
      background: "var(--ink)",
      borderTop: "1px solid var(--hairline-dark)",
      padding: "128px 32px"
    }}>
      <div style={{ maxWidth: 1280, margin: "0 auto" }}>

        {/* ── Header ── */}
        <div style={{ marginBottom: 64 }}>
          <div style={{
            fontFamily: "var(--font-condensed)",
            fontSize: "clamp(32px, 5vw, 52px)",
            fontWeight: 900,
            letterSpacing: "0.12em",
            textTransform: "uppercase",
            color: "var(--gold)",
            display: "inline-flex",
            alignItems: "center",
            gap: 16,
            textShadow: "0 0 40px rgba(212,175,55,0.35)"
          }}>
            <span style={{ display: "inline-block", width: 40, height: 3, background: "var(--gold)", borderRadius: 1 }}/>
            Campaign Team Structure
            <span style={{ display: "inline-block", width: 40, height: 3, background: "var(--gold)", borderRadius: 1 }}/>
          </div>
          <h2 style={{
            fontFamily: "var(--font-display)", fontWeight: 600,
            fontSize: "clamp(40px, 5vw, 64px)", lineHeight: 1.05,
            letterSpacing: "-0.03em", margin: "12px 0 16px",
            color: "var(--cream)"
          }}>
            Your complete team,<br/>deployed for every candidate.
          </h2>
          <p style={{
            color: "rgba(240,233,216,0.55)", fontSize: 16, lineHeight: 1.65,
            maxWidth: 520, fontFamily: "var(--font-body)"
          }}>
            A dedicated 10-member team is assigned to each campaign — covering strategy, content, ground operations and digital execution end to end.
          </p>
        </div>

        {/* ── Slide card ── */}
        <div style={{
          background: "var(--ink-raised)",
          border: "1px solid rgba(255,255,255,0.07)",
          borderRadius: 2,
          overflow: "hidden",
          position: "relative"
        }}>

          {/* Progress bar */}
          <div style={{ height: 3, background: "rgba(255,255,255,0.05)" }}>
            <div style={{
              height: "100%",
              width: `${progress}%`,
              background: role.accent,
              transition: "width 120ms linear"
            }}/>
          </div>

          {/* Slide content */}
          <div
            className="team-slide-grid"
            style={{
              padding: "clamp(28px, 5vw, 64px)",
              display: "grid",
              gridTemplateColumns: "160px 1fr",
              gap: "clamp(24px, 4vw, 56px)",
              alignItems: "center",
              minHeight: 260,
              opacity: visible ? 1 : 0,
              transform: visible ? "translateY(0)" : "translateY(14px)",
              transition: "opacity 200ms ease, transform 200ms ease"
            }}
          >
            {/* Left: big number */}
            <div style={{ flexShrink: 0 }}>
              <div style={{
                fontFamily: "var(--font-condensed)",
                fontWeight: 800,
                fontSize: "clamp(72px, 10vw, 116px)",
                lineHeight: 0.85,
                letterSpacing: "-0.05em",
                color: role.accent,
                opacity: 0.88
              }}>
                {role.no}
              </div>
              <div style={{
                fontFamily: "var(--font-condensed)",
                fontWeight: 700,
                fontSize: 10,
                letterSpacing: "0.2em",
                textTransform: "uppercase",
                color: "rgba(240,233,216,0.3)",
                marginTop: 10
              }}>
                of {TEAM_ROLES.length} roles
              </div>
            </div>

            {/* Right: role name + duties */}
            <div>
              <div style={{
                fontFamily: "var(--font-condensed)",
                fontWeight: 800,
                fontSize: "clamp(36px, 6vw, 66px)",
                lineHeight: 1,
                letterSpacing: "-0.02em",
                color: "var(--cream)",
                marginBottom: 20
              }}>
                {role.role}
              </div>

              <div style={{
                width: 36, height: 2,
                background: role.accent,
                borderRadius: 1,
                marginBottom: 20
              }}/>

              <ul style={{ listStyle: "none", display: "flex", flexDirection: "column", gap: 10 }}>
                {role.duties.map((d, i) => (
                  <li key={i} style={{ display: "flex", gap: 12, alignItems: "flex-start" }}>
                    <span style={{
                      display: "inline-block",
                      width: 5, height: 5, borderRadius: "50%",
                      background: role.accent,
                      flexShrink: 0,
                      marginTop: 8
                    }}/>
                    <span style={{
                      color: "rgba(240,233,216,0.7)",
                      fontSize: 15,
                      lineHeight: 1.6,
                      fontFamily: "var(--font-body)"
                    }}>{d}</span>
                  </li>
                ))}
              </ul>
            </div>
          </div>

          {/* Bottom nav bar */}
          <div className="team-nav-pad" style={{
            borderTop: "1px solid rgba(255,255,255,0.06)",
            padding: "18px clamp(24px, 5vw, 64px)",
            display: "flex",
            alignItems: "center",
            justifyContent: "space-between",
            gap: 16,
            flexWrap: "wrap"
          }}>
            {/* Pill dots */}
            <div style={{ display: "flex", gap: 5, flexWrap: "wrap", alignItems: "center" }}>
              {TEAM_ROLES.map((_, idx) => (
                <button
                  key={idx}
                  onClick={() => goTo(idx)}
                  title={TEAM_ROLES[idx].role}
                  aria-label={TEAM_ROLES[idx].role}
                  style={{
                    width: idx === active ? 22 : 7,
                    height: 7,
                    borderRadius: 4,
                    border: "none",
                    background: idx === active ? role.accent : "rgba(255,255,255,0.14)",
                    cursor: "pointer",
                    padding: 0,
                    transition: "width 320ms var(--ease-out), background 300ms ease"
                  }}
                />
              ))}
            </div>

            {/* Arrow controls */}
            <div style={{ display: "flex", gap: 8 }}>
              <button
                onClick={() => goTo((active - 1 + TEAM_ROLES.length) % TEAM_ROLES.length)}
                aria-label="Previous role"
                style={{
                  width: 40, height: 40,
                  border: "1px solid rgba(244,239,224,0.18)",
                  background: "transparent",
                  color: "var(--cream)",
                  borderRadius: 2,
                  cursor: "pointer",
                  fontSize: 17,
                  display: "flex", alignItems: "center", justifyContent: "center",
                  transition: "border-color 180ms ease"
                }}
                onMouseEnter={e => e.currentTarget.style.borderColor = "rgba(244,239,224,0.5)"}
                onMouseLeave={e => e.currentTarget.style.borderColor = "rgba(244,239,224,0.18)"}
              >←</button>
              <button
                onClick={() => goTo((active + 1) % TEAM_ROLES.length)}
                aria-label="Next role"
                style={{
                  width: 40, height: 40,
                  border: "1px solid rgba(244,239,224,0.18)",
                  background: "transparent",
                  color: "var(--cream)",
                  borderRadius: 2,
                  cursor: "pointer",
                  fontSize: 17,
                  display: "flex", alignItems: "center", justifyContent: "center",
                  transition: "border-color 180ms ease"
                }}
                onMouseEnter={e => e.currentTarget.style.borderColor = "rgba(244,239,224,0.5)"}
                onMouseLeave={e => e.currentTarget.style.borderColor = "rgba(244,239,224,0.18)"}
              >→</button>
            </div>
          </div>
        </div>

        {/* ── Role chips overview ── */}
        <div style={{ marginTop: 28, display: "flex", gap: 8, flexWrap: "wrap" }}>
          {TEAM_ROLES.map((r, idx) => (
            <button
              key={idx}
              onClick={() => goTo(idx)}
              style={{
                fontFamily: "var(--font-mono)",
                fontSize: 10,
                padding: "5px 10px",
                borderRadius: 2,
                background: idx === active ? role.accent : "transparent",
                color: idx === active ? "var(--ink)" : "rgba(244,239,224,0.4)",
                border: `1px solid ${idx === active ? role.accent : "rgba(244,239,224,0.12)"}`,
                cursor: "pointer",
                letterSpacing: "0.06em",
                fontWeight: 600,
                textTransform: "uppercase",
                transition: "all 200ms ease"
              }}
              onMouseEnter={e => {
                if (idx !== active) {
                  e.currentTarget.style.color = "rgba(244,239,224,0.75)";
                  e.currentTarget.style.borderColor = "rgba(244,239,224,0.3)";
                }
              }}
              onMouseLeave={e => {
                if (idx !== active) {
                  e.currentTarget.style.color = "rgba(244,239,224,0.4)";
                  e.currentTarget.style.borderColor = "rgba(244,239,224,0.12)";
                }
              }}
            >
              {r.no} {r.role}
            </button>
          ))}
        </div>

      </div>
    </section>
  );
};

window.TeamStructure = TeamStructure;
