'use client';

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

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

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

  const planName = translations?.['vps.plan_name'] || translations?.vps?.plan1?.name || translations?.pricing?.plan_name || 'أوبيليفيو نايت';
  const period = translations?.['vps.period'] || 'د.ل/ربع سنوي';
  const orderNow = translations?.['vps.pricing_order_now'] || translations?.pricing?.order_now || 'اطلب الآن';
  const pricingTitle = translations?.['vps.pricing_title'] || translations?.pricing?.title || 'اختر الخطة المناسبة';
  const pricingSubtitle = translations?.['vps.pricing_subtitle'] || 'استضافة الخوادم الافتراضية (VPS) توفر لك المرونة والتحكم والأداء العالي للموارد المخصصة بأسعار مناسبة.';

  const livePlans = useLivePrices(vpsPlans);
  const plans = livePlans.map((p) => ({
    ...p,
    name: planName,
    period,
    features: featuresConfig.map((f) => ({
      ...f,
      label: translations?.[f.labelKey] || f.labelFallback,
    })),
  }));

  return (
    <section className='vps-pricing-section pt-0 pb-20 bg-white relative overflow-hidden -mt-20 z-[1] border-0 border-b-0' dir={dir} style={{ border: 'none', outline: 'none', borderWidth: 0 }}>
      <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
      />

      <div className='absolute inset-x-0 top-[-10%] h-[800px] pointer-events-none z-0'>
        <img
          src='/images/right-black-graphic1.svg'
          alt=''
          className='absolute left-0 top-0 h-full w-auto opacity-40 transform scale-x-[-1]'
          aria-hidden
        />
        <img
          src='/images/right-black-graphic1.svg'
          alt=''
          className='absolute right-0 top-0 h-full w-auto opacity-40'
          aria-hidden
        />
      </div>

      <div className='container mx-auto px-4 relative z-10 border-0' style={{ border: 'none' }}>
        {/* الجزء فوق العنوان: أيقونتان دائريتان + عنوان + فقرة */}
        <div className='text-center mb-16 pt-32'>
          <div className='flex justify-center gap-4 mb-6'>
            <div className='w-14 h-14 rounded-full bg-blue-50 border border-gray-200 flex items-center justify-center p-2 shadow-sm overflow-hidden'>
              <Monitor className='w-8 h-8 text-blue-600' aria-hidden />
            </div>
            <div className='w-14 h-14 rounded-full bg-blue-50 border border-gray-200 flex items-center justify-center p-2 shadow-sm overflow-hidden'>
              <Settings2 className='w-8 h-8 text-blue-600' aria-hidden />
            </div>
          </div>
          <h2 className='text-[20px] md:text-[24px] font-bold text-[#092346] mb-4 leading-tight'>
            {pricingTitle}
          </h2>
          <p className='text-[#64748b] text-base max-w-2xl mx-auto leading-relaxed'>
            {pricingSubtitle}
          </p>
        </div>

        <h2 className='text-center text-[20px] md:text-[24px] font-bold text-[#092346] mb-10 leading-tight'>
          {pricingTitle}
        </h2>

        <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 pb-4 border-0'
          style={{ border: 'none' }}
        >
          {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-0 border-b-0'
            >
              <div
                className='h-2.5 w-full'
                style={{ backgroundColor: plan.color }}
              />
              <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>
                <div className='flex justify-center items-start gap-6 mb-6'>
                  {plan.features.map((feature, idx) => (
                    <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 border border-gray-200 bg-blue-50 overflow-hidden'>
                        <feature.Icon className='w-5 h-5 text-blue-600' aria-hidden />
                      </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>
                {plan.link ? (
                  <a
                    href={plan.link}
                    target='_blank'
                    rel='noopener noreferrer'
                    className='block w-36 text-center mx-auto mt-2 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 mt-2 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 VpsPricing;
