'use client';

import { useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { useTranslator } from '@/contexts/TranslatorContext';
import { REGION_KEYS, REGIONS, COUNTRY_FALLBACK } from '@/constants/dataCenterRegions';

const Tabs = () => {
  const { translations, dir } = useTranslator();
  const t = translations.tabs_section || {};
  const regionLabels = t.regions || {};

  const [selected, setSelected] = useState('africa');

  const countries = REGIONS[selected] || [];

  // نص الدولة من الترجمات، وإلا الاحتياطي العربي
  const textOf = key => {
    const fromT = t.countries?.[key];
    return {
      city: fromT?.city || COUNTRY_FALLBACK[key]?.city || key,
      country: fromT?.country || COUNTRY_FALLBACK[key]?.country || '',
    };
  };

  return (
    <div className='w-full p-4' dir={dir}>
      {/* أزرار التبويبات */}
      <motion.div
        initial={{ opacity: 0, y: 100 }}
        whileInView={{
          opacity: 1,
          y: 0,
          transition: { duration: 0.4, ease: 'easeOut' },
        }}
        viewport={{ once: true }}
        className='w-full max-w-3xl mx-auto mb-8'>
        <div
          role='tablist'
          className='flex flex-wrap rounded-lg bg-gray-200 p-1.5'>
          {REGION_KEYS.map(key => (
            <button
              key={key}
              role='tab'
              aria-selected={selected === key}
              className={`flex-1 rounded-lg px-3 sm:px-4 py-2.5 text-center transition-colors font-medium text-sm sm:text-base ${
                selected === key
                  ? 'bg-white text-gray-900 shadow-sm'
                  : 'bg-transparent text-gray-600 hover:text-gray-900'
              }`}
              onClick={() => setSelected(key)}>
              {regionLabels[key] || key}
              <span className='ms-1.5 text-[11px] text-gray-400'>
                ({REGIONS[key]?.length || 0})
              </span>
            </button>
          ))}
        </div>
      </motion.div>

      {/* محتوى التبويب */}
      <div className='w-full'>
        <AnimatePresence mode='wait'>
          <motion.div
            key={selected}
            initial={{ opacity: 0, y: 20 }}
            animate={{ opacity: 1, y: 0 }}
            exit={{ opacity: 0, y: -10 }}
            transition={{ duration: 0.25, ease: 'easeOut' }}
            className='flex flex-wrap justify-center gap-6 w-full'>
            {countries.map(loc => {
              const { city, country } = textOf(loc.key);
              return (
                <div
                  key={loc.key}
                  className='bg-white rounded-2xl border border-gray-200 shadow-sm hover:shadow-md transition-shadow p-5 flex flex-col w-full sm:w-[calc(50%-12px)] lg:w-[calc(33.333%-16px)]'>
                  {/* صورة المدينة */}
                  <div className='w-full mb-5'>
                    <img
                      src={loc.skyline}
                      alt=''
                      aria-hidden
                      className='w-full h-36 object-contain'
                      loading='lazy'
                    />
                  </div>

                  {/* العلم والموقع */}
                  <div className='flex items-center gap-3 mb-5' dir='rtl'>
                    <img
                      src={loc.flag}
                      alt={country}
                      className='w-12 h-12 rounded-full object-cover flex-shrink-0'
                      loading='lazy'
                    />
                    <div className='flex flex-col items-start text-right'>
                      <p className='text-xl font-bold text-gray-900 mb-1'>{city}</p>
                      <p className='text-sm text-gray-500'>{country}</p>
                    </div>
                    <span className='ms-auto text-sm font-bold text-green-600 tabular-nums'>
                      {loc.ping}
                    </span>
                  </div>

                  {/* الـ IP وزر الاختبار */}
                  <div className='flex flex-col gap-3 mt-auto' dir='rtl'>
                    <p className='text-sm text-gray-600 text-left'>
                      <span className='font-semibold'>IP:</span> {loc.ip}
                    </p>
                    <button
                      type='button'
                      className='w-full bg-[#2B1F51] text-white text-sm font-semibold py-2.5 rounded-lg hover:bg-[#1f1640] transition-colors'>
                      {t.testSpeed || 'إختبار السرعة'}
                    </button>
                  </div>
                </div>
              );
            })}
          </motion.div>
        </AnimatePresence>
      </div>
    </div>
  );
};

export default Tabs;
