'use client';

import { useState, useEffect, useRef } from 'react';
import styles from './Testimonials.module.css';

export default function Testimonials() {
    const [activeIndex, setActiveIndex] = useState(0);
    const [isResetting, setIsResetting] = useState(false);
    const timeoutRef = useRef<NodeJS.Timeout | null>(null);

    const testimonials = [
        {
            name: 'Rahul Mehta',
            role: 'Growth Manager',
            reviewTitle:'Highly Impressive Results!',
            text: 'The performance insights are extremely clear. We can now adjust campaigns quickly and make confident marketing decisions.',
            image: '👩‍💼'
        },
        {
            name: 'Emily Carter',
            role: 'Content Strategist',
            reviewTitle:'Smooth and Reliable Platform!',
            text: 'Managing posts feels effortless now. Planning content ahead of time has significantly improved our publishing consistency.',
            image: '👨‍💻'
        },
        {
            name: 'Arjun Nair',
            role: 'Digital Consultant',
            reviewTitle:'Excellent Tool for Teams!',
            text: 'Collaboration is seamless and approvals are faster. It has simplified how we handle multiple client accounts.',
            image: '👩‍🎨'
        },
        {
            name: 'Sophia Williams',
            role: 'Brand Manager',
            reviewTitle:'Well Designed and Effective!',
            text: 'The interface is clean and easy to navigate. Tracking engagement across platforms has never been this simple.',
            image: '👨‍💼'
        },
        {
            name: 'Daniel Rodrigues',
            role: ' Marketing Lead',
            reviewTitle:'Strong Value for Growth!',
            text: 'The reporting features save us hours every week. Insights are easy to understand and useful for strategy planning.',
            image: '👩‍🦰'
        },
    ];

    // Clone the first few items to create the infinite illusion
    // We need enough clones to fill the visible area. 4 is safe.
    const extendedTestimonials = [...testimonials, ...testimonials.slice(0, 4)];
    const totalOriginal = testimonials.length;

    useEffect(() => {
        const interval = setInterval(() => {
            handleNext();
        }, 3000);

        return () => clearInterval(interval);
    }, [activeIndex, isResetting]); // Re-create interval if state changes to avoid stale closures, though functional update handles it.

    const handleNext = () => {
        if (isResetting) return;

        setActiveIndex((prev) => {
            // If we are at the end of the original list (showing the first clone)
            // We animate TO the clone.
            return prev + 1;
        });
    };

    // Handle seamless reset
    useEffect(() => {
        if (activeIndex === totalOriginal) {
            // We have just slid TO the first cloned item (visually identical to index 0)
            // Wait for transition to finish, then snap back to real 0
            timeoutRef.current = setTimeout(() => {
                setIsResetting(true); // Disable transition
                setActiveIndex(0);    // Jump to 0

                // Re-enable transition after a brief moment to allow DOM update
                requestAnimationFrame(() => {
                    requestAnimationFrame(() => {
                        setIsResetting(false);
                    });
                });
            }, 500); // Must match CSS transition duration
        }

        return () => {
            if (timeoutRef.current) clearTimeout(timeoutRef.current);
        };
    }, [activeIndex, totalOriginal]);

    const slideNext = () => {
        if (isResetting) return;
        setActiveIndex(prev => prev + 1);
    };

    const slidePrev = () => {
        if (isResetting) return;
        if (activeIndex === 0) {
            // Jump to end clone without transition, then slide back
            setIsResetting(true);
            setActiveIndex(totalOriginal);
            requestAnimationFrame(() => {
                requestAnimationFrame(() => {
                    setIsResetting(false);
                    setActiveIndex(totalOriginal - 1);
                });
            });
        } else {
            setActiveIndex(prev => prev - 1);
        }
    };

    return (
        <section className={styles.testimonialSection} id="testimonials">
            <div className={styles.layoutContainer}>
                {/* Left Side Static Card - High Z-Index */}
                <div className={styles.leftCard}>
                    <div className={styles.cardContent}>
                        <h2 className={styles.cardTitle}>
                            Unlock Your Growth Opportunities
                        </h2>
                        <p className={styles.cardDescription}>
                            Connect with a global community of marketers and creators who have elevated their digital presence using Social Buddy. Real progress, real outcomes - getting started takes only a moment.
                        </p>

                        <div className={styles.miniStats}>
                            <div className={styles.statBadge}>
                                <span className={styles.statIcon}>👥</span>
                                <div>
                                    <strong>10,000+</strong>
                                    <span>Active Members</span>
                                </div>
                            </div>
                            <div className={styles.statBadge}>
                                <span className={styles.statIcon}>⭐</span>
                                <div>
                                    <strong>4.9/5</strong>
                                    <span>User Satisfaction</span>
                                </div>
                            </div>
                        </div>

                        <button className={styles.readMoreBtn}>
                            View Customer Stories
                        </button>
                    </div>
                </div>

                {/* Right Side Slider - Lower Z-Index, moves behind Left Card */}
                <div className={styles.sliderContainer}>
                    <div
                        className={styles.sliderTrack}
                        style={{
                            transform: `translateX(calc(-${activeIndex * 352}px))`,
                            transition: isResetting ? 'none' : 'transform 0.5s cubic-bezier(0.2, 0.8, 0.2, 1)'
                        }}
                    >
                        {extendedTestimonials.map((t, i) => (
                            <div key={i} className={styles.sliderCard}>
                                <div className={styles.cardHeader}>
                                    <div className={styles.userImage}>{t.image}</div>
                                    <div className={styles.userInfo}>
                                        <h4>{t.name}</h4>
                                        <span className={styles.userCompany}>{t.role}</span>
                                    </div>
                                    <div className={styles.rating}>⭐⭐⭐⭐⭐</div>
                                </div>
                                <h5 className={styles.reviewTitle}>{t.reviewTitle}</h5>
                                <p className={styles.reviewText}>{t.text}</p>
                            </div>
                        ))}
                    </div>

                    {/* Navigation Arrows */}
                    <div className={styles.sliderNav}>
                        <button
                            className={styles.navBtn}
                            onClick={slidePrev}
                            aria-label="Previous testimonial"
                        >
                            ←
                        </button>
                        <button
                            className={styles.navBtn}
                            onClick={slideNext}
                            aria-label="Next testimonial"
                        >
                            →
                        </button>
                    </div>
                </div>
            </div>
        </section>
    );
}
