'use client';

import Image from 'next/image';
import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
} from '@/components/ui/accordion';
import { motion } from 'framer-motion';
import { useTranslator } from '@/contexts/TranslatorContext';
import { dashboardFeaturesConsts } from '@/constants/homePageConsts';

const DashboardFeatures = ({ translationPrefix }) => {
  const { translations, dir } = useTranslator();

  const content = {
    ...dashboardFeaturesConsts,
    subtitle:
      (translationPrefix && translations?.[`${translationPrefix}.dashboard_subtitle`]) ||
      translations['dashboard_features.subtitle'] ||
      dashboardFeaturesConsts.subtitle,
    header:
      (translationPrefix && translations?.[`${translationPrefix}.dashboard_header`]) ||
      translations['dashboard_features.header'] ||
      dashboardFeaturesConsts.header,
    description:
      (translationPrefix && translations?.[`${translationPrefix}.dashboard_description`]) ||
      translations['dashboard_features.description'] ||
      dashboardFeaturesConsts.description,
    accordionItems:
      translations['dashboard_features.accordion'] ||
      dashboardFeaturesConsts.accordionItems,
    imageAlt:
      translations['dashboard_features.image_alt_dashboard'] ||
      dashboardFeaturesConsts.imageAlt,
  };

  return (
    <section
      className='mt-16 sm:mt-20 md:mt-24 lg:mt-32'
      dir={dir}>
      <div className='container mx-auto px-6 max-w-7xl'>
        <div className='grid gap-12 lg:grid-cols-2 items-center'>
          {/* Left: Text + Accordion */}
          <motion.div
            initial={{ opacity: 0, x: dir === 'rtl' ? 100 : -100 }}
            whileInView={{
              opacity: 1,
              x: 0,
              transition: { duration: 0.4, ease: 'easeOut' },
            }}
            className={`flex flex-col space-y-6 w-full ${
              dir === 'rtl' ? 'items-end text-right' : 'items-start text-left'
            }`}>
            <div className='space-y-2 w-full'>
              <motion.p 
                className={`text-sm font-medium text-muted-foreground w-full ${
                  dir === 'rtl' ? 'text-right' : 'text-left'
                }`}
                initial={{ opacity: 0, y: 20 }}
                whileInView={{ opacity: 1, y: 0, transition: { duration: 0.5, delay: 0.1 } }}
                viewport={{ once: true }}>
                {content.subtitle}
              </motion.p>
              <motion.h2 
                className={`text-xl md:text-2xl font-semibold w-full ${
                  dir === 'rtl' ? 'text-right' : 'text-left'
                }`}
                initial={{ opacity: 0, y: 20 }}
                whileInView={{ opacity: 1, y: 0, transition: { duration: 0.6, delay: 0.2 } }}
                viewport={{ once: true }}>
                {content.header}
              </motion.h2>
              <motion.p 
                className={`mt-2 text-gray-700 text-base md:text-lg leading-relaxed w-full ${
                  dir === 'rtl' ? 'text-right' : 'text-left'
                }`}
                initial={{ opacity: 0, y: 20 }}
                whileInView={{ opacity: 1, y: 0, transition: { duration: 0.6, delay: 0.3 } }}
                viewport={{ once: true }}>
                {content.description}
              </motion.p>
            </div>

            <Accordion
              type='single'
              collapsible
              className={`w-full flex flex-col space-y-4 text-blue-600 ${
                dir === 'rtl' ? 'text-right' : 'text-left'
              }`}>
              {content.accordionItems.map((item, index) => (
                <AccordionItem
                  key={index}
                  value={`item-${index + 1}`}
                  className='border rounded-lg border-gray-200'>
                  <AccordionTrigger
                    className={`text-lg font-semibold flex justify-between items-center px-4 py-4 w-full ${
                      dir === 'rtl' ? 'text-right' : 'text-left'
                    }`}>
                    {item.trigger}
                  </AccordionTrigger>
                  <AccordionContent className={`text-muted-foreground px-4 pb-4 ${
                    dir === 'rtl' ? 'text-right' : 'text-left'
                  }`}>
                    {item.content}
                  </AccordionContent>
                </AccordionItem>
              ))}
            </Accordion>
          </motion.div>

          {/* Right: Image */}
          <motion.div
            initial={{ opacity: 0, x: dir === 'rtl' ? -100 : 100 }}
            whileInView={{
              opacity: 1,
              x: 0,
              transition: { duration: 0.4, ease: 'easeOut' },
            }}
            className={`flex justify-center ${
              dir === 'rtl' ? 'lg:justify-end' : 'lg:justify-start'
            }`}>
            <Image
              src={content.imageSrc}
              alt={content.imageAlt}
              width={600}
              height={400}
              className='rounded-xl shadow-lg object-cover'
              priority
            />
          </motion.div>
        </div>
      </div>
    </section>
  );
};

export default DashboardFeatures;
