'use client';

import Link from 'next/link';
import { useState, useRef, useEffect, useCallback } from 'react';

export default function LatestInitiatives() {
  const [idx, setIdx] = useState(0);
  const rowRef = useRef<HTMLDivElement>(null);
  const programmatic = useRef(false);

  const initiatives = [
    {
      icon: 'fa-flask',
      eyebrow: 'Expanding Diagnostic Excellence Across North India',
      title: 'Agrivet Partners with Royal Poultry Laboratory',
      description: 'Faster sample collection, precise testing, and reliable scientific support for the poultry sector.',
      link: '/poultry-lab',
      linkText: 'Explore the Partnership',
      theme: 'ccv2-mint',
    },
    {
      icon: 'fa-handshake',
      eyebrow: 'Centre of Excellence in Poultry (COEP)',
      title: 'Agrivet Collaborates with Agricultural Development Trust, Baramati',
      description: 'Advancing diagnostics, skill development, disease surveillance, and sustainable farming.',
      link: '/coep',
      linkText: 'Explore the Collaboration',
      theme: 'ccv2-gold',
    },
    {
      icon: 'fa-graduation-cap',
      eyebrow: 'Agrivet × JIS University',
      title: 'Empowering Future Innovators',
      description: "Driving research excellence, practical education, and industry-ready skills for tomorrow's agricultural leaders.",
      link: '/future-innovators',
      linkText: 'Explore the Initiative',
      theme: 'ccv2-blue',
    },
  ];

  const total = initiatives.length;

  useEffect(() => {
    const el = rowRef.current;
    if (!el) return;
    programmatic.current = true;
    el.scrollTo({ left: idx * el.offsetWidth, behavior: 'smooth' });
    const t = setTimeout(() => { programmatic.current = false; }, 500);
    return () => clearTimeout(t);
  }, [idx]);

  useEffect(() => {
    const el = rowRef.current;
    if (!el) return;
    const onScroll = () => {
      if (programmatic.current) return;
      setIdx(Math.round(el.scrollLeft / el.offsetWidth));
    };
    el.addEventListener('scroll', onScroll, { passive: true });
    return () => el.removeEventListener('scroll', onScroll);
  }, []);

  const prev = useCallback(() => setIdx((i) => Math.max(0, i - 1)), []);
  const next = useCallback(() => setIdx((i) => Math.min(total - 1, i + 1)), [total]);

  return (
    <section className="collab-section" id="collab">
      <div className="collab-section-bg" aria-hidden="true">
        <div className="cs-blob cs-blob-1" />
        <div className="cs-blob cs-blob-2" />
        <div className="cs-grid-lines" />
      </div>
      <div className="container">

        {/* Header */}
        <div className="section-header center reveal">
          <div className="section-eyebrow">Latest Initiatives</div>
          <h2 className="section-title">Discover Our Latest <em>Initiatives</em></h2>
        </div>

        {/* Cards */}
        <div className="collab-carousel-wrap">
          <button className="collab-arrow collab-arrow-prev" onClick={prev} disabled={idx === 0} aria-label="Previous">&#8249;</button>
          <button className="collab-arrow collab-arrow-next" onClick={next} disabled={idx === total - 1} aria-label="Next">&#8250;</button>
          <div className="collab-cards-row" ref={rowRef}>
            {initiatives.map((ini, i) => (
              <article key={i} className={`collab-card-v2 ${ini.theme} reveal`} style={{ animationDelay: `${i * 0.12}s` }}>
                <div className="ccv2-num">0{i + 1}</div>
                <div className="ccv2-eyebrow">{ini.eyebrow}</div>
                <h3>{ini.title}</h3>
                <Link href={ini.link} className="ccv2-link">
                  {ini.linkText} <i className="fas fa-arrow-right" />
                </Link>
                <div className="ccv2-glow" />
              </article>
            ))}
          </div>
          <div className="collab-dots">
            {initiatives.map((_, i) => (
              <button key={i} className={`collab-dot${i === idx ? ' active' : ''}`} onClick={() => setIdx(i)} aria-label={`Slide ${i + 1}`} />
            ))}
          </div>
        </div>

      </div>
    </section>
  );
}
