'use client';
import React from 'react';
import { useTranslator } from '@/contexts/TranslatorContext';
import { useCurrency } from '@/contexts/CurrencyContext';
import { useLivePrices } from '@/hooks/useLivePrices';
import { windowsPlans } from '@/constants/servicePlansConsts';
import { motion } from 'framer-motion';

const PricingCardItem = ({ plan, translations, dir, index }) => (
  <motion.div
    className={`bg-white rounded-sm shadow-[0_4px_20px_rgba(0,0,0,0.05)] hover:shadow-xl transition-all duration-300 w-full border-t-[5px]`}
    style={{ borderColor: plan.color }}
    initial={{ opacity: 0, y: 60 }}
    whileInView={{ opacity: 1, y: 0 }}
    viewport={{ once: true, amount: 0.1, margin: '0px 0px -60px 0px' }}
    transition={{ duration: 0.5, delay: index * 0.15, ease: 'easeOut' }}
    whileHover={{ y: -8, scale: 1.02 }}
  >
    <div className='p-6 pt-6 text-center flex flex-col items-center'>
      <h3 className='text-xl sm:text-2xl font-bold text-[#2B1F51] mb-1 font-sans'>
        {plan.name}
      </h3>

      <div className='flex flex-col items-center justify-center my-1'>
        <div className='flex items-start justify-center gap-1 text-[#2B1F51]' dir='ltr'>
          <span className='text-2xl font-light mt-2'>{plan.currency}</span>
          <span className='text-[3.5rem] leading-none font-light tracking-tighter'>
            {plan.price}
          </span>
        </div>

        {plan.originalPrice && (
          <div className='flex items-center gap-4 mt-2 text-sm justify-center w-full'>
            <span className='bg-[#FFEDD5] text-[#F97316] px-2 py-0.5 rounded text-xs font-medium'>
              {plan.discount}
            </span>
            <span className='text-gray-300 line-through decoration-gray-300 decoration-1 text-base'>
              {plan.originalPrice} {plan.currency}
            </span>
          </div>
        )}
        {!plan.originalPrice && (
          <p className='text-gray-500 text-sm mt-1' dir={dir}>
            {plan.period}
          </p>
        )}
      </div>

      {plan.link ? (
        <a
          href={plan.link}
          target='_blank'
          rel='noopener noreferrer'
          className='mt-3 w-[140px] bg-white border border-[#2B1F51] text-[#2B1F51] hover:bg-[#2B1F51] hover:text-white font-medium py-2.5 px-6 rounded text-sm transition-colors duration-300 text-center'
        >
          {translations['pricing.order_now'] || 'اطلب الان'}
        </a>
      ) : (
        <button className='mt-3 w-[140px] bg-white border border-[#2B1F51] text-[#2B1F51] hover:bg-[#2B1F51] hover:text-white font-medium py-2.5 px-6 rounded text-sm transition-colors duration-300'>
          {translations['pricing.order_now'] || 'اطلب الان'}
        </button>
      )}

      <div className='w-full border-t border-gray-100 mt-4 mb-3'></div>
    </div>

    <div className='px-6 pb-3'>
      <ul className='space-y-1.5' dir={dir === 'rtl' ? 'rtl' : 'ltr'}>
        {plan.features.map((feature, idx) => {
          const isValueKey = feature.valueKey;
          const valueText = isValueKey ? translations[feature.valueKey] : feature.value;
          const labelText = translations[feature.labelKey] || feature.labelKey;

          return (
            <li
              key={idx}
              className='text-sm text-gray-700 flex items-start gap-2 font-medium'>
              <span className='text-[#374151]'>
                {valueText && (
                  <span className='font-bold text-black mx-1'>{valueText}</span>
                )}
                {labelText}
              </span>
            </li>
          );
        })}
      </ul>
    </div>

    <div className='px-6 pb-5 text-center'>
      <span className='text-gray-400 text-sm inline-flex items-center gap-2'>
        powered by
        <img
          src='/images/logos/litespeed.png'
          alt='LiteSpeed'
          className='h-5 w-auto'
        />
      </span>
    </div>
  </motion.div>
);

const featureList = [
  { labelKey: 'pricing.nvme_storage', value: '60GB' },
  { labelKey: 'pricing.websites', value: '15' },
  { labelKey: 'pricing.ram', value: '4' },
  { labelKey: 'pricing.cpu', value: '3' },
  { labelKey: 'pricing.cpanel' },
  { labelKey: 'pricing.subdomains', valueKey: 'pricing.unlimited' },
  { labelKey: 'pricing.databases', valueKey: 'pricing.unlimited' },
  { labelKey: 'pricing.email_account', valueKey: 'pricing.unlimited' },
  { labelKey: 'pricing.ftp_accounts', valueKey: 'pricing.unlimited' },
  { labelKey: 'pricing.dedicated_ns', value: '(ns1,ns2)' },
];

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

  const planName = translations['pricing.plan_name'] || 'أوبيليونز ناتيف';
  const currencyPeriod = translations['pricing.currency_period'] || '/ ربع سنوي';

  const livePlans = useLivePrices(windowsPlans);
  const plans = livePlans.map((p) => ({
    name: planName,
    price: priceFor(p.prices),
    currency: symbol,
    period: currencyPeriod,
    color: p.color,
    features: featureList,
    link: p.link,
  }));

  return (
    <section className='w-full py-12 sm:py-16 md:py-20 lg:py-24 mt-8 sm:mt-4 md:mt-0 bg-white px-2 sm:px-4 md:px-6'>
      <div className='container mx-auto px-2 sm:px-4'>
        <motion.h2
          className='text-3xl sm:text-4xl font-bold text-center text-[#2B1F51] mb-12 sm:mb-16'
          dir={dir}
          initial={{ opacity: 0, y: 20 }}
          whileInView={{ opacity: 1, y: 0 }}
          viewport={{ once: true }}
          transition={{ duration: 0.5 }}>
          {translations['pricing.select_plan'] || 'اختر أحد الخطط'}
        </motion.h2>

        <motion.div
          className='grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4 sm:gap-4 md:gap-4 lg:gap-4 w-full'
          dir={dir}
          initial={{ opacity: 0 }}
          whileInView={{ opacity: 1 }}
          viewport={{ once: true, amount: 0.05, margin: '0px 0px -80px 0px' }}
          transition={{ duration: 0.3 }}>
          {plans.map((plan, idx) => (
            <PricingCardItem
              key={idx}
              plan={plan}
              translations={translations}
              dir={dir}
              index={idx}
            />
          ))}
        </motion.div>
      </div>
    </section>
  );
}
