'use client';

import React from 'react';
import { useTranslator } from '@/contexts/TranslatorContext';
import { useCurrency } from '@/contexts/CurrencyContext';
import { useLivePrices } from '@/hooks/useLivePrices';
import { cloudServersPlans } from '@/constants/servicePlansConsts';
import { motion } from 'framer-motion';
import { Cpu, HardDrive, MemoryStick } from 'lucide-react';

const CloudPricing = ({ translationKey }) => {
  const { translations, dir } = useTranslator();
  const { symbol, priceFor } = useCurrency();

  const planName = translationKey
    ? translations?.[`${translationKey}.plan_name`]
    : translations?.pricing?.plan_name;
  const period = translationKey
    ? translations?.[`${translationKey}.period`]
    : 'د.ل/ربع سنوي';
  const orderNow = translationKey
    ? translations?.[`${translationKey}.pricing_order_now`]
    : translations?.pricing?.order_now;

  const featuresConfig = [
    { IconComponent: HardDrive, value: '20GB', labelKey: 'pricing.nvme_storage', labelFallback: 'تخزين SSD' },
    { IconComponent: MemoryStick, value: '2GB', labelKey: 'pricing.ram', labelFallback: 'الذاكرة' },
    { IconComponent: Cpu, value: '1', labelKey: 'pricing.cpu', labelFallback: 'vCPU' },
  ];

  const livePlans = useLivePrices(cloudServersPlans);
  const plans = livePlans.map((p) => ({
    ...p,
    name: planName || 'أوبيليونز ناتيف',
    period,
    features: featuresConfig.map((f) => ({
      ...f,
      label: translations?.[f.labelKey] || f.labelFallback,
    })),
  }));

  return (
    <section className='pt-0 pb-20 bg-white relative overflow-hidden -mt-20 z-[1] border-0' dir={dir}>
      {/* Background image behind the content */}
      <img 
        src='/images/bg.svg' 
        alt='' 
        className='absolute inset-0 w-full h-full object-cover pointer-events-none select-none -z-10 opacity-60'
        aria-hidden
      />

      {/* Background Graphic on Sides - Pulled up to be opposite the title */}
      <div className='absolute inset-x-0 top-[-10%] h-[800px] pointer-events-none z-0'>
        <img 
          src='/images/right-black-graphic1.svg' 
          alt='bg-left' 
          className='absolute left-0 top-0 h-full w-auto opacity-40 transform scale-x-[-1]'
        />
        <img 
          src='/images/right-black-graphic1.svg' 
          alt='bg-right' 
          className='absolute right-0 top-0 h-full w-auto opacity-40'
        />
      </div>

      <div className='container mx-auto px-4 relative z-10 border-0'>
        {/* Heading & Description */}
        <div className='text-center mb-16 pt-32'>
          <h2 className='text-[34px] font-bold text-[#092346]'>
            {(translationKey && translations?.[`${translationKey}.pricing_title`]) || translations?.pricing?.title || translations?.['cloud_hosting.plan.title'] || 'اختر الخطة المناسبة'}
          </h2>
        </div>

        {/* Pricing Grid */}
        <motion.div 
          initial={{ opacity: 0, y: 100 }}
          whileInView={{
            opacity: 1,
            y: 0,
            transition: { duration: 0.6, ease: 'easeOut' },
          }}
          viewport={{ once: true }}
          className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 max-w-6xl mx-auto'>
          {plans.map((plan, index) => (
            <div key={index} className='bg-white rounded-xl shadow-[0_10px_40px_rgba(0,0,0,0.04)] overflow-hidden hover:shadow-2xl transition-all duration-500 flex flex-col border-none'>
              {/* Colored Top Bar - Thick as in the image */}
              <div 
                className='h-2.5 w-full' 
                style={{ backgroundColor: plan.color }}
              ></div>
              
              <div className='p-6 text-center flex-grow'>
                <h3 className='text-[18px] font-bold text-[#092346] mb-6'>
                  {plan.name}
                </h3>
                
                <div className='mb-6'>
                  <div className='text-[48px] font-bold text-[#092346] tracking-tighter leading-none'>
                    {priceFor(plan.prices)} <span className='text-[22px]'>{symbol}</span>
                  </div>
                  <div className='text-gray-400 text-[11px] mt-2 font-medium'>
                    {plan.period}
                  </div>
                </div>

                {/* Features Icons with Labels */}
                <div className='flex justify-center items-start gap-6 mb-6'>
                  {plan.features.map((feature, idx) => {
                    const Icon = feature.IconComponent;
                    return (
                      <div key={idx} className='flex flex-col items-center gap-1.5'>
                        <div className='w-10 h-10 rounded-full flex items-center justify-center text-[#3B82F6] bg-transparent overflow-hidden'>
                          <Icon className='w-5 h-5' strokeWidth={1.8} />
                        </div>
                        <div className='text-[13px] font-bold text-[#092346]'>{feature.value}</div>
                        <div className='text-[9px] text-gray-400 whitespace-nowrap'>{feature.label}</div>
                      </div>
                    );
                  })}
                </div>

                {/* Blue/Gray Indicator Bar */}
                <div className='flex justify-center gap-2 mb-6'>
                  <div className='w-10 h-1 bg-gray-100 rounded-full'></div>
                  <div className='w-10 h-1 bg-[#3B82F6] rounded-full'></div>
                </div>

                {/* Order Button - Brand Dark Blue */}
                {plan.link ? (
                  <a
                    href={plan.link}
                    target='_blank'
                    rel='noopener noreferrer'
                    className='block w-36 text-center mx-auto bg-[#2B1F51] text-white text-[14px] font-bold py-3 px-6 rounded-lg hover:bg-[#1a1435] transition-all shadow-lg active:scale-95'
                  >
                    {orderNow || 'اطلب الان'}
                  </a>
                ) : (
                  <button className='w-36 mx-auto bg-[#2B1F51] text-white text-[14px] font-bold py-3 px-6 rounded-lg hover:bg-[#1a1435] transition-all shadow-lg active:scale-95'>
                    {orderNow || 'اطلب الان'}
                  </button>
                )}
              </div>
            </div>
          ))}
        </motion.div>
      </div>
    </section>
  );
};

export default CloudPricing;
