'use client';

import { motion } from 'framer-motion';
import { useTranslator } from '@/contexts/TranslatorContext';
import { useCurrency } from '@/contexts/CurrencyContext';
import {
  LEFT_CUBE_SVG,
  RIGHT_CUBE_SVG,
  SERVICES_ITEMS,
} from '@/constants/homePageConsts';

const sectionVariants = {
  hidden: { opacity: 0, y: 50 },
  visible: { opacity: 1, y: 0, transition: { duration: 0.6 } },
};

const cardVariants = {
  hidden: { opacity: 0, y: 30 },
  visible: i => ({
    opacity: 1,
    y: 0,
    transition: { delay: i * 0.2, duration: 0.5 },
  }),
};

export default function ServicesSection() {
  const { translations, lang, dir } = useTranslator();
  const { symbol, priceFor } = useCurrency();

  // السعر حسب العملة المختارة: 45 ﷼ أساساً، ويتحول تلقائياً لبقية العملات
  const formatPrice = price => {
    const usdBase = Math.round((price / 3.75) * 100) / 100;
    return `${priceFor({ sa: price, en: usdBase })} ${symbol}`;
  };

  const basePrice = 45;

  return (
    <section
      className='relative w-full pb-4 sm:pb-6 lg:pb-8 bg-white overflow-hidden'
      dir={dir}>
      <motion.section
        className='pb-4 sm:pb-6 lg:pb-8 bg-white pt-16'
        initial='hidden'
        whileInView='visible'
        viewport={{ once: true }}
        variants={sectionVariants}>
        <div className='max-w-7xl mx-auto px-4 sm:px-6 lg:px-8'>
          <div className='pointer-events-none select-none absolute top-16 sm:top-20 right-0 w-[200px] sm:w-[250px] md:w-[300px] lg:w-[350px] opacity-40'>
            <img
              src={RIGHT_CUBE_SVG}
              alt=''
              aria-hidden='true'
              className='w-full h-auto animate-float animate-float-delay-1'
            />
          </div>
          <div className='pointer-events-none select-none absolute top-16 left-0 w-[200px] sm:w-[250px] md:w-[300px] lg:w-[350px] opacity-40'>
            <img
              src={LEFT_CUBE_SVG}
              alt=''
              aria-hidden='true'
              className='w-full h-auto animate-float animate-float-delay-2'
            />
          </div>

          {/* Header */}
          <motion.div
            className='text-center mb-10 sm:mb-12 lg:mb-16'
            initial={{ opacity: 0, y: 30 }}
            whileInView={{ opacity: 1, y: 0, transition: { duration: 0.6, ease: 'easeOut' } }}
            viewport={{ once: true }}>
            <motion.h2 
              className='text-center w-fit mx-auto bg-[#092346] text-white font-bold text-2xl p-2 rounded-xl mb-4'
              initial={{ opacity: 0, scale: 0.9 }}
              whileInView={{ opacity: 1, scale: 1, transition: { duration: 0.5, delay: 0.1 } }}
              viewport={{ once: true }}>
              {translations['services.header'] || 'Our Services'}
            </motion.h2>
            <motion.p 
              className='lg:text-4xl text-xl mb-6 text-center'
              initial={{ opacity: 0, y: 20 }}
              whileInView={{ opacity: 1, y: 0, transition: { duration: 0.6, delay: 0.2 } }}
              viewport={{ once: true }}>
              {translations['services.description'] ||
                'Discover the services provided to you'}
            </motion.p>
          </motion.div>

          {/* Cards Grid */}
          <div className='mt-16 grid lg:grid-cols-3 md:grid-cols-2 grid-cols-1 gap-16'>
            {SERVICES_ITEMS.map((service, index) => (
              <motion.div
                key={index}
                className='rounded-xl shadow-lg hover:shadow-2xl border border-gray-200 overflow-hidden flex flex-col bg-white transition-all duration-300 hover:-translate-y-1'
                variants={cardVariants}
                custom={index}
                initial='hidden'
                whileInView='visible'
                viewport={{ once: true }}
                whileHover={{ scale: 1.02, transition: { duration: 0.3 } }}>
                {/* Colored Top Border - خط مع أنيميشن تلميع */}
                <div className={`w-full h-2 ${service.topColor} relative overflow-hidden rounded-t-xl`}>
                  <span className='absolute inset-0 animate-section-line block pointer-events-none' aria-hidden='true' />
                </div>

                {/* Image Container with Curved Background */}
                <div className='relative w-full'>
                  {/* Image Over the Background */}
                  <div className='flex items-center justify-center h-72 z-10 relative p-1'>
                    <motion.img
                      src={service.image}
                      alt={
                        translations[`services.${service.key}.title`] ||
                        service.key
                      }
                      className='w-full h-full object-contain'
                      initial={{ opacity: 0, scale: 0.9 }}
                      whileInView={{
                        opacity: 1,
                        scale: 1,
                        transition: { duration: 0.5 },
                      }}
                      viewport={{ once: true }}
                    />
                  </div>
                </div>

                {/* Content */}
                <motion.div
                  className={`px-4 mt-4 ${
                    dir === 'rtl' ? 'text-right' : 'text-left'
                  }`}
                  initial={{ opacity: 0, y: 20 }}
                  whileInView={{ opacity: 1, y: 0, transition: { duration: 0.5, delay: index * 0.1 + 0.3 } }}
                  viewport={{ once: true }}>
                  <motion.h1 
                    className='text-2xl rtl:text-xl font-semibold'
                    initial={{ opacity: 0, y: 10 }}
                    whileInView={{ opacity: 1, y: 0, transition: { duration: 0.4, delay: index * 0.1 + 0.4 } }}
                    viewport={{ once: true }}>
                    {translations[`services.${service.key}.title`] ||
                      service.key}
                  </motion.h1>
                  <motion.p 
                    className='font-thin mb-2'
                    initial={{ opacity: 0, y: 10 }}
                    whileInView={{ opacity: 1, y: 0, transition: { duration: 0.4, delay: index * 0.1 + 0.5 } }}
                    viewport={{ once: true }}>
                    {translations[`services.${service.key}.description`] ||
                      'Host your website with ease, convenience, and cost-effective pricing!'}
                  </motion.p>
                </motion.div>

                {/* Price Button */}
                <div className='mt-8 mx-auto'>
                  <div className='bg-[#2B1F51] hover:bg-transparent transition hover:text-[#092346] hover:border-2 hover:border-[#092346] text-white rounded-md px-12 py-2 w-fit mx-auto cursor-pointer py-3 px-6 mb-6 text-lg font-semibold'>
                    {translations[
                      `services.${service.key}.price_label`
                    ]?.replace('{price}', formatPrice(basePrice)) ||
                      `Starting from ${formatPrice(basePrice)} / month`}
                  </div>
                </div>
              </motion.div>
            ))}
          </div>
        </div>
      </motion.section>
    </section>
  );
}
