// Hero — World-Class Dynamic Hero with Video, Particles & Animated Text
const { useState, useEffect, useRef, useCallback, useMemo } = React;

const slides = [
  {
    titleLine1: 'WELCOME TO',
    titleHighlight: 'AEQ SAFETY',
    subtitle: 'Your One-Stop Safety & Engineering Solution Provider',
    accent: '#fbbf24'
  },
  {
    titleLine1: 'SME-DELIVERED',
    titleHighlight: 'TRAINING',
    subtitle: 'Industry Specific Training Modules Delivered by Subject Matter Experts ( SMEs ) with Practical Sessions',
    accent: '#06b6d4'
  },
  {
    titleLine1: 'NICHE INDUSTRY',
    titleHighlight: 'PROGRAMS',
    subtitle: 'HAZOP | Process Safety | Heavy Machinery for Mining, Quarry & Stockpiles | Agricultural Machineries for Plantation & Dairy Farms',
    accent: '#10b981'
  },
  {
    titleLine1: 'CIMAH REPORTING',
    titleHighlight: '20+ YEARS',
    subtitle: 'Control of Industrial Major Accident Hazards Reporting with Over 20 Years Experience from Various Chemical Industries',
    accent: '#8b5cf6'
  },
  {
    titleLine1: 'REGIONAL',
    titleHighlight: 'COVERAGE',
    subtitle: 'Operating Across Malaysia, Indonesia & Singapore',
    accent: '#f59e0b'
  }
];

function Hero({ t }) {
  const [current, setCurrent] = useState(0);
  const [isPaused, setIsPaused] = useState(false);
  const [isLoaded, setIsLoaded] = useState(false);
  const [mousePos, setMousePos] = useState({ x: 0.5, y: 0.5 });
  const heroRef = useRef(null);

  // Floating particles
  const particles = useMemo(() => {
    return Array.from({ length: 40 }, (_, i) => ({
      id: i,
      size: Math.random() * 4 + 2,
      left: Math.random() * 100,
      top: Math.random() * 100,
      duration: Math.random() * 25 + 20,
      delay: Math.random() * 15,
      opacity: Math.random() * 0.4 + 0.1
    }));
  }, []);

  // Auto-advance slides - FAST (3 seconds)
  useEffect(() => {
    if (isPaused) return;
    const timeout = setTimeout(() => {
      setCurrent((prev) => (prev + 1) % slides.length);
    }, 3000);
    return () => clearTimeout(timeout);
  }, [current, isPaused]);

  // Set loaded after initial render
  useEffect(() => {
    setTimeout(() => setIsLoaded(true), 100);
  }, []);

  // Mouse parallax
  const handleMouseMove = useCallback((e) => {
    if (!heroRef.current) return;
    const rect = heroRef.current.getBoundingClientRect();
    setMousePos({
      x: (e.clientX - rect.left) / rect.width,
      y: (e.clientY - rect.top) / rect.height
    });
  }, []);

  // Single video plays continuously
  const videoRef = useRef(null);
  useEffect(() => {
    if (videoRef.current) {
      videoRef.current.play().catch(() => {});
    }
  }, []);

  const goTo = useCallback((index) => setCurrent(index), []);
  const next = useCallback(() => setCurrent((prev) => (prev + 1) % slides.length), []);
  const prev = useCallback(() => setCurrent((prev) => (prev - 1 + slides.length) % slides.length), []);

  const currentSlide = slides[current];

  return (
    <section
      ref={heroRef}
      className={`hero-ultra ${isLoaded ? 'loaded' : ''}`}
      id="top"
      onMouseEnter={() => setIsPaused(true)}
      onMouseLeave={() => setIsPaused(false)}
      onMouseMove={handleMouseMove}
    >
      {/* Single Video Background */}
      <div className="hero-videos">
        <div className="hero-video-wrap active">
          <video
            ref={videoRef}
            className="hero-video"
            autoPlay
            muted
            loop
            playsInline
            preload="auto"
          >
            <source src="assets/hero-video.mp4" type="video/mp4" />
          </video>
        </div>
        <div className="hero-overlay" style={{ '--accent': currentSlide.accent }} />
      </div>

      {/* Animated Color Morphs */}
      <div className="hero-morphs">
        <div
          className="morph-blob m1"
          style={{
            transform: `translate(${(mousePos.x - 0.5) * 30}px, ${(mousePos.y - 0.5) * 30}px)`,
            '--accent': currentSlide.accent
          }}
        />
        <div
          className="morph-blob m2"
          style={{
            transform: `translate(${(mousePos.x - 0.5) * -20}px, ${(mousePos.y - 0.5) * -20}px)`,
            '--accent': currentSlide.accent
          }}
        />
        <div
          className="morph-blob m3"
          style={{
            transform: `translate(${(mousePos.x - 0.5) * 25}px, ${(mousePos.y - 0.5) * 25}px)`,
            '--accent': currentSlide.accent
          }}
        />
      </div>


      {/* Floating Particles */}
      <div className="hero-particles">
        {particles.map(p => (
          <div
            key={p.id}
            className="hero-particle"
            style={{
              '--size': `${p.size}px`,
              '--left': `${p.left}%`,
              '--top': `${p.top}%`,
              '--duration': `${p.duration}s`,
              '--delay': `${p.delay}s`,
              '--opacity': p.opacity
            }}
          />
        ))}
      </div>

      {/* Content */}
      <div className="hero-content">
        <div className="hero-text-container" key={current}>
          {/* Animated Label */}
          <div className="hero-label">
            <span className="label-line" />
            <span className="label-text">SAFETY EXCELLENCE SINCE 2013</span>
            <span className="label-line" />
          </div>

          {/* Main Title with Word Animation */}
          <h1 className="hero-title">
            <span className="title-line1">
              {currentSlide.titleLine1.split(' ').map((word, i) => (
                <span key={i} className="word-wrap">
                  <span className="word" style={{ '--delay': `${0.1 + i * 0.08}s` }}>{word}</span>
                  {' '}
                </span>
              ))}
            </span>
            <span className="title-highlight" style={{ '--accent': currentSlide.accent }}>
              {currentSlide.titleHighlight.split(' ').map((word, i) => (
                <span key={i} className="word-wrap">
                  <span className="word highlight-word" style={{ '--delay': `${0.4 + i * 0.1}s` }}>{word}</span>
                  {' '}
                </span>
              ))}
            </span>
          </h1>

          {/* Subtitle */}
          <p className="hero-subtitle">
            {currentSlide.subtitle.split(' ').map((word, i) => (
              <span key={i} className="word-wrap">
                <span className="word sub-word" style={{ '--delay': `${0.7 + i * 0.04}s` }}>{word}</span>
                {' '}
              </span>
            ))}
          </p>

          {/* CTA Buttons */}
          <div className="hero-cta">
            <a href="training.html" className="cta-primary" style={{ '--accent': currentSlide.accent }}>
              <span>Explore Training</span>
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                <path d="M5 12h14M12 5l7 7-7 7"/>
              </svg>
            </a>
            <a href="contact.html" className="cta-secondary">
              <span>Get in Touch</span>
            </a>
          </div>
        </div>

        {/* Slide Counter */}
        <div className="hero-counter">
          <span className="counter-current">0{current + 1}</span>
          <span className="counter-sep">/</span>
          <span className="counter-total">0{slides.length}</span>
        </div>
      </div>

      {/* Navigation Arrows */}
      <button className="hero-arrow arrow-left" onClick={prev} aria-label="Previous">
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
          <path d="M15 18l-6-6 6-6" />
        </svg>
      </button>
      <button className="hero-arrow arrow-right" onClick={next} aria-label="Next">
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
          <path d="M9 18l6-6-6-6" />
        </svg>
      </button>

      {/* Progress Indicators */}
      <div className="hero-indicators">
        {slides.map((slide, index) => (
          <button
            key={index}
            className={`hero-indicator ${index === current ? 'active' : ''}`}
            onClick={() => goTo(index)}
            style={{ '--accent': slide.accent }}
            aria-label={`Go to slide ${index + 1}`}
          >
            <span className="indicator-fill" />
          </button>
        ))}
      </div>

      {/* Scroll Indicator */}
      <div className="hero-scroll">
        <span className="scroll-text">SCROLL</span>
        <div className="scroll-line">
          <div className="scroll-dot" />
        </div>
      </div>

      {/* Decorative Elements */}
      <div className="hero-grid-lines">
        <div className="grid-line gl1" />
        <div className="grid-line gl2" />
        <div className="grid-line gl3" />
      </div>

      <style>{`
        .hero-ultra {
          position: relative;
          height: 100vh;
          min-height: 700px;
          overflow: hidden;
          background: #030712;
          padding-top: 200px;
          box-sizing: border-box;
        }

        /* Video Background */
        .hero-videos {
          position: absolute;
          inset: 0;
        }

        .hero-video-wrap {
          position: absolute;
          inset: 0;
          opacity: 0;
          transform: scale(1.1);
          transition: opacity 1.2s ease, transform 1.5s ease;
        }

        .hero-video-wrap.active {
          opacity: 1;
          transform: scale(1);
        }

        .hero-video {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }

        .hero-overlay {
          position: absolute;
          inset: 0;
          background:
            linear-gradient(135deg, rgba(3, 7, 18, 0.85) 0%, rgba(15, 23, 42, 0.7) 50%, rgba(3, 7, 18, 0.8) 100%),
            radial-gradient(ellipse at 30% 20%, color-mix(in srgb, var(--accent) 15%, transparent) 0%, transparent 50%);
          transition: background 1s ease;
        }

        /* Color Morphs */
        .hero-morphs {
          position: absolute;
          inset: 0;
          overflow: hidden;
          pointer-events: none;
        }

        .morph-blob {
          position: absolute;
          border-radius: 50%;
          filter: blur(100px);
          opacity: 0.4;
          transition: transform 0.3s ease-out;
          animation: morphFloat 20s ease-in-out infinite;
        }

        .m1 {
          width: 600px;
          height: 600px;
          background: radial-gradient(circle, var(--accent) 0%, transparent 70%);
          top: -20%;
          left: -10%;
          animation-delay: 0s;
        }

        .m2 {
          width: 500px;
          height: 500px;
          background: radial-gradient(circle, color-mix(in srgb, var(--accent) 70%, #8b5cf6) 0%, transparent 70%);
          bottom: -20%;
          right: -10%;
          animation-delay: -7s;
        }

        .m3 {
          width: 400px;
          height: 400px;
          background: radial-gradient(circle, color-mix(in srgb, var(--accent) 50%, #06b6d4) 0%, transparent 70%);
          top: 50%;
          left: 60%;
          animation-delay: -14s;
        }

        @keyframes morphFloat {
          0%, 100% { transform: translate(0, 0) scale(1); }
          33% { transform: translate(50px, -30px) scale(1.1); }
          66% { transform: translate(-30px, 50px) scale(0.9); }
        }

        /* Logo Background for Slide 1 */
        .hero-logo-bg {
          position: absolute;
          inset: 0;
          background: linear-gradient(135deg, #030712 0%, #0f172a 50%, #030712 100%);
          overflow: hidden;
        }

        .logo-bg-container {
          position: absolute;
          inset: 0;
        }

        .bg-logo {
          position: absolute;
          opacity: 0.12;
          filter: brightness(1.5);
        }

        .bg-logo.main-logo {
          width: 45%;
          max-width: 500px;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          opacity: 0.15;
          animation: mainLogoMove 15s ease-in-out infinite;
        }

        .bg-logo.logo-1 {
          width: 20%;
          max-width: 200px;
          top: 10%;
          left: 8%;
          animation: logoFloat1 20s ease-in-out infinite;
        }

        .bg-logo.logo-2 {
          width: 18%;
          max-width: 180px;
          top: 15%;
          right: 10%;
          animation: logoFloat2 25s ease-in-out infinite;
        }

        .bg-logo.logo-3 {
          width: 15%;
          max-width: 150px;
          bottom: 20%;
          left: 12%;
          animation: logoFloat3 18s ease-in-out infinite;
        }

        .bg-logo.logo-4 {
          width: 22%;
          max-width: 220px;
          bottom: 15%;
          right: 8%;
          animation: logoFloat4 22s ease-in-out infinite;
        }

        @keyframes mainLogoMove {
          0%, 100% { transform: translate(-50%, -50%) scale(1); }
          50% { transform: translate(-50%, -50%) scale(1.08); }
        }

        @keyframes logoFloat1 {
          0%, 100% { transform: translate(0, 0) rotate(0deg); }
          25% { transform: translate(40px, 30px) rotate(5deg); }
          50% { transform: translate(-20px, 60px) rotate(-3deg); }
          75% { transform: translate(50px, 20px) rotate(8deg); }
        }

        @keyframes logoFloat2 {
          0%, 100% { transform: translate(0, 0) rotate(0deg); }
          33% { transform: translate(-50px, 40px) rotate(-5deg); }
          66% { transform: translate(30px, -30px) rotate(5deg); }
        }

        @keyframes logoFloat3 {
          0%, 100% { transform: translate(0, 0) rotate(0deg); }
          50% { transform: translate(60px, -50px) rotate(10deg); }
        }

        @keyframes logoFloat4 {
          0%, 100% { transform: translate(0, 0) rotate(0deg); }
          33% { transform: translate(-40px, -30px) rotate(-8deg); }
          66% { transform: translate(30px, 40px) rotate(6deg); }
        }

        /* Particles */
        .hero-particles {
          position: absolute;
          inset: 0;
          overflow: hidden;
          pointer-events: none;
        }

        .hero-particle {
          position: absolute;
          width: var(--size);
          height: var(--size);
          background: rgba(255, 255, 255, var(--opacity));
          border-radius: 50%;
          left: var(--left);
          top: var(--top);
          animation: particleRise var(--duration) linear infinite;
          animation-delay: var(--delay);
        }

        @keyframes particleRise {
          0% {
            transform: translateY(0) rotate(0deg);
            opacity: 0;
          }
          10% {
            opacity: var(--opacity);
          }
          90% {
            opacity: var(--opacity);
          }
          100% {
            transform: translateY(-100vh) rotate(360deg);
            opacity: 0;
          }
        }

        /* Content */
        .hero-content {
          position: relative;
          z-index: 10;
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 0 5%;
        }

        .hero-text-container {
          text-align: center;
          max-width: 1100px;
        }

        /* Label */
        .hero-label {
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 16px;
          margin-bottom: 35px;
          opacity: 0;
          animation: fadeIn 0.8s ease 0.3s forwards;
        }

        .label-line {
          width: 40px;
          height: 1px;
          background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent);
        }

        .label-text {
          font-size: 10px;
          font-weight: 600;
          color: rgba(255, 255, 255, 0.6);
          letter-spacing: 0.3em;
          word-spacing: 0.4em;
          white-space: nowrap;
        }

        /* Title */
        .hero-title {
          font-size: clamp(28px, 5.5vw, 65px);
          font-weight: 700;
          line-height: 1.2;
          margin: 0 0 30px;
          letter-spacing: -0.01em;
        }

        .title-line1 {
          display: block;
          color: #ffffff;
          letter-spacing: 0.15em;
          word-spacing: 0.3em;
        }

        .title-highlight {
          display: block;
          color: var(--accent);
          text-shadow: 0 0 60px color-mix(in srgb, var(--accent) 50%, transparent);
          letter-spacing: 0.12em;
          word-spacing: 0.3em;
        }

        .word-wrap {
          display: inline-block;
          overflow: hidden;
          vertical-align: top;
          margin-right: 0.35em;
        }

        .word {
          display: inline-block;
          opacity: 0;
          transform: translateY(100%);
          animation: wordReveal 0.6s cubic-bezier(0.4, 0, 0.2, 1) forwards;
          animation-delay: var(--delay);
        }

        @keyframes wordReveal {
          to {
            opacity: 1;
            transform: translateY(0);
          }
        }

        /* Subtitle */
        .hero-subtitle {
          font-size: clamp(12px, 1.3vw, 15px);
          color: rgba(255, 255, 255, 0.85);
          margin: 0 auto 45px;
          line-height: 1.9;
          letter-spacing: 0.05em;
          max-width: 750px;
          padding: 0 20px;
        }

        .sub-word {
          animation-delay: var(--delay);
        }

        /* CTA */
        .hero-cta {
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 16px;
          opacity: 0;
          animation: fadeInUp 0.8s ease 1.2s forwards;
        }

        .cta-primary {
          display: inline-flex;
          align-items: center;
          gap: 10px;
          padding: 14px 28px;
          background: var(--accent);
          color: #030712;
          font-size: 13px;
          font-weight: 600;
          text-decoration: none;
          border-radius: 60px;
          transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
          box-shadow: 0 4px 30px color-mix(in srgb, var(--accent) 40%, transparent);
        }

        .cta-primary:hover {
          transform: translateY(-3px) scale(1.02);
          box-shadow: 0 10px 40px color-mix(in srgb, var(--accent) 50%, transparent);
        }

        .cta-primary svg {
          transition: transform 0.3s ease;
        }

        .cta-primary:hover svg {
          transform: translateX(5px);
        }

        .cta-secondary {
          display: inline-flex;
          align-items: center;
          padding: 14px 28px;
          background: transparent;
          color: #fff;
          font-size: 13px;
          font-weight: 600;
          text-decoration: none;
          border: 1.5px solid rgba(255, 255, 255, 0.3);
          border-radius: 60px;
          transition: all 0.4s ease;
        }

        .cta-secondary:hover {
          background: rgba(255, 255, 255, 0.1);
          border-color: rgba(255, 255, 255, 0.6);
        }

        @keyframes fadeIn {
          to { opacity: 1; }
        }

        @keyframes fadeInUp {
          from { opacity: 0; transform: translateY(30px); }
          to { opacity: 1; transform: translateY(0); }
        }

        /* Counter */
        .hero-counter {
          position: absolute;
          right: 5%;
          bottom: 100px;
          font-size: 14px;
          font-weight: 600;
          color: rgba(255, 255, 255, 0.6);
          letter-spacing: 0.1em;
        }

        .counter-current {
          font-size: 32px;
          color: #fff;
        }

        .counter-sep {
          margin: 0 8px;
        }

        /* Arrows */
        .hero-arrow {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          z-index: 20;
          width: 60px;
          height: 60px;
          border: 2px solid rgba(255, 255, 255, 0.2);
          background: rgba(255, 255, 255, 0.05);
          backdrop-filter: blur(10px);
          color: rgba(255, 255, 255, 0.8);
          cursor: pointer;
          display: flex;
          align-items: center;
          justify-content: center;
          transition: all 0.4s ease;
          border-radius: 50%;
        }

        .hero-arrow:hover {
          background: rgba(255, 255, 255, 0.15);
          border-color: rgba(255, 255, 255, 0.5);
          transform: translateY(-50%) scale(1.1);
        }

        .arrow-left { left: 40px; }
        .arrow-right { right: 40px; }

        /* Indicators */
        .hero-indicators {
          position: absolute;
          left: 40px;
          bottom: 100px;
          display: flex;
          flex-direction: column;
          gap: 12px;
          z-index: 20;
        }

        .hero-indicator {
          width: 40px;
          height: 4px;
          background: rgba(255, 255, 255, 0.2);
          border: none;
          cursor: pointer;
          padding: 0;
          position: relative;
          overflow: hidden;
          border-radius: 2px;
        }

        .indicator-fill {
          position: absolute;
          inset: 0;
          background: var(--accent);
          transform: scaleX(0);
          transform-origin: left;
          transition: transform 0.3s ease;
        }

        .hero-indicator.active .indicator-fill {
          transform: scaleX(1);
          animation: indicatorProgress 7s linear forwards;
        }

        @keyframes indicatorProgress {
          from { transform: scaleX(0); }
          to { transform: scaleX(1); }
        }

        /* Scroll Indicator */
        .hero-scroll {
          position: absolute;
          bottom: 40px;
          left: 50%;
          transform: translateX(-50%);
          display: flex;
          flex-direction: column;
          align-items: center;
          gap: 12px;
          z-index: 20;
        }

        .scroll-text {
          font-size: 10px;
          font-weight: 600;
          color: rgba(255, 255, 255, 0.5);
          letter-spacing: 0.2em;
        }

        .scroll-line {
          width: 1px;
          height: 50px;
          background: rgba(255, 255, 255, 0.2);
          position: relative;
          overflow: hidden;
        }

        .scroll-dot {
          position: absolute;
          top: 0;
          width: 3px;
          height: 10px;
          background: #fbbf24;
          border-radius: 3px;
          left: -1px;
          animation: scrollDown 2s ease-in-out infinite;
        }

        @keyframes scrollDown {
          0% { top: 0; opacity: 0; }
          30% { opacity: 1; }
          70% { opacity: 1; }
          100% { top: 100%; opacity: 0; }
        }

        /* Grid Lines */
        .hero-grid-lines {
          position: absolute;
          inset: 0;
          pointer-events: none;
          overflow: hidden;
        }

        .grid-line {
          position: absolute;
          background: rgba(255, 255, 255, 0.03);
        }

        .gl1 {
          left: 25%;
          top: 0;
          bottom: 0;
          width: 1px;
        }

        .gl2 {
          left: 50%;
          top: 0;
          bottom: 0;
          width: 1px;
        }

        .gl3 {
          left: 75%;
          top: 0;
          bottom: 0;
          width: 1px;
        }

        /* Responsive */
        @media (max-width: 900px) {
          .hero-arrow {
            width: 50px;
            height: 50px;
          }

          .arrow-left { left: 20px; }
          .arrow-right { right: 20px; }

          .hero-indicators {
            left: 20px;
            bottom: 80px;
          }

          .hero-counter {
            right: 20px;
            bottom: 80px;
          }

          .hero-cta {
            flex-direction: column;
          }

          .hero-grid-lines {
            display: none;
          }
        }

        @media (max-width: 600px) {
          .hero-title {
            font-size: clamp(24px, 8vw, 38px);
            margin-bottom: 20px;
          }

          .hero-arrow {
            display: none;
          }

          .hero-indicators {
            flex-direction: row;
            left: 50%;
            transform: translateX(-50%);
            bottom: 30px;
          }

          .hero-counter {
            display: none;
          }

          .hero-label {
            gap: 10px;
            margin-bottom: 25px;
          }

          .label-line {
            width: 25px;
          }

          .label-text {
            font-size: 8px;
            letter-spacing: 0.15em;
          }

          .cta-primary, .cta-secondary {
            padding: 14px 24px;
            font-size: 13px;
          }

          .hero-subtitle {
            font-size: 11px;
            padding: 0 25px;
            line-height: 1.8;
            margin-bottom: 30px;
            letter-spacing: 0.03em;
          }

          .hero-cta {
            gap: 12px;
          }
        }
      `}</style>
    </section>
  );
}

window.Hero = Hero;
