"use client";

import { useState, useEffect } from "react";
import { Globe, Server, Wrench, Shield, Gamepad2, Code, Activity, Lightbulb, User, BookOpen } from "lucide-react";
import { useLocale, useTranslations } from "next-intl";
import type { JSONContent } from "@tiptap/react";
import RichTextClient from "./RichTextClient";
import CurrentlyReading from "./CurrentlyReading";
import ReadBooks from "./ReadBooks";
import { motion } from "framer-motion";
import { TechStackCategory, Hobby } from "@/lib/directus";

const iconMap: Record<string, any> = {
  Globe, Server, Code, Wrench, Shield, Activity, Lightbulb, Gamepad2
};

const About = () => {
  const locale = useLocale();
  const t = useTranslations("home.about");
  const [cmsDoc, setCmsDoc] = useState<JSONContent | null>(null);
  const [techStack, setTechStack] = useState<TechStackCategory[]>([]);
  const [hobbies, setHobbies] = useState<Hobby[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchData = async () => {
      try {
        const [cmsRes, techRes, hobbiesRes] = await Promise.all([
          fetch(`/api/content/page?key=home-about&locale=${locale}`),
          fetch(`/api/tech-stack?locale=${locale}`),
          fetch(`/api/hobbies?locale=${locale}`)
        ]);
        const cmsData = await cmsRes.json();
        if (cmsData?.content?.content) setCmsDoc(cmsData.content.content as JSONContent);
        const techData = await techRes.json();
        if (techData?.techStack) setTechStack(techData.techStack);
        const hobbiesData = await hobbiesRes.json();
        if (hobbiesData?.hobbies) setHobbies(hobbiesData.hobbies);
      } catch (error) {
        console.error(error);
      } finally {
        setLoading(false);
      }
    };
    fetchData();
  }, [locale]);

  return (
    <section id="about" className="py-32 px-6 bg-[#fdfcf8] dark:bg-stone-950 transition-colors duration-500 overflow-hidden">
      <div className="max-w-7xl mx-auto">
        
        <div className="grid grid-cols-1 md:grid-cols-12 gap-8">
          
          {/* 1. Large Bio Text */}
          <motion.div 
            initial={{ opacity: 0, y: 30 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: true }}
            className="md:col-span-8 bg-white dark:bg-stone-900 rounded-[3rem] p-12 border border-stone-200/60 dark:border-stone-800/60 shadow-sm"
          >
            <div className="space-y-8">
              <h2 className="text-4xl md:text-6xl font-black text-stone-900 dark:text-stone-50 tracking-tighter">
                {t("title")}
              </h2>
              <div className="prose prose-stone dark:prose-invert max-w-none text-xl font-light leading-relaxed text-stone-600 dark:text-stone-400">
                {cmsDoc ? <RichTextClient doc={cmsDoc} /> : <p>{t("p1")} {t("p2")}</p>}
              </div>
              <div className="pt-6 flex flex-wrap gap-4">
                <div className="bg-liquid-mint/10 px-6 py-3 rounded-2xl border border-liquid-mint/20">
                   <p className="text-[10px] font-black uppercase tracking-widest text-liquid-mint mb-1">{t("funFactTitle")}</p>
                   <p className="text-sm font-bold opacity-80">{t("funFactBody")}</p>
                </div>
              </div>
            </div>
          </motion.div>

          {/* 2. Reading Log */}
          <motion.div 
            initial={{ opacity: 0, y: 30 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: true }}
            transition={{ delay: 0.1 }}
            className="md:col-span-4 bg-white dark:bg-stone-900 rounded-[3rem] p-12 border border-stone-200/60 dark:border-stone-800/60 shadow-sm flex flex-col"
          >
            <h3 className="text-2xl font-black text-stone-900 dark:text-stone-50 mb-8 flex items-center gap-3 uppercase tracking-tighter">
              <BookOpen className="text-liquid-purple" size={24} /> Reading
            </h3>
            <div className="space-y-10 overflow-y-auto pr-2 scrollbar-hide">
              <CurrentlyReading />
              <div className="pt-10 border-t border-stone-100 dark:border-stone-800">
                <ReadBooks />
              </div>
            </div>
          </motion.div>

          {/* 3. Tech Stack */}
          <motion.div 
            initial={{ opacity: 0, y: 30 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: true }}
            transition={{ delay: 0.2 }}
            className="md:col-span-12 bg-white dark:bg-stone-900 rounded-[3rem] p-12 border border-stone-200/60 dark:border-stone-800/60 shadow-sm"
          >
            <div className="grid grid-cols-1 md:grid-cols-4 gap-12">
              {techStack.map((cat) => (
                <div key={cat.id} className="space-y-6">
                  <h4 className="text-[10px] font-black uppercase tracking-[0.2em] text-stone-400">{cat.name}</h4>
                  <div className="flex flex-wrap gap-2">
                    {cat.items?.map((item: any) => (
                      <span key={item.id} className="px-4 py-2 bg-stone-50 dark:bg-stone-800/50 rounded-xl text-xs font-bold border border-stone-100 dark:border-stone-700/50 hover:border-liquid-mint transition-colors">
                        {item.name}
                      </span>
                    ))}
                  </div>
                </div>
              ))}
            </div>
          </motion.div>

          {/* 4. Hobbies */}
          <motion.div 
            initial={{ opacity: 0, y: 30 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: true }}
            transition={{ delay: 0.3 }}
            className="md:col-span-12 bg-liquid-purple/5 dark:bg-stone-900 rounded-[3rem] p-12 border border-liquid-purple/20 dark:border-stone-800/60 flex flex-col md:flex-row justify-between items-center gap-8"
          >
            <h3 className="text-2xl font-black text-stone-900 dark:text-stone-50 flex items-center gap-3">
              <Gamepad2 className="text-liquid-purple" size={28} /> {t("hobbiesTitle")}
            </h3>
            <div className="flex flex-wrap justify-center gap-4">
              {hobbies.map((hobby) => {
                const Icon = iconMap[hobby.icon] || Lightbulb;
                return (
                  <div key={hobby.id} className="flex items-center gap-3 px-6 py-3 bg-white dark:bg-stone-800 rounded-2xl shadow-sm border border-stone-100 dark:border-stone-700">
                    <Icon size={18} className="text-liquid-purple" />
                    <span className="text-xs font-bold uppercase tracking-wider">{hobby.title}</span>
                  </div>
                )
              })}
            </div>
          </motion.div>

        </div>
      </div>
    </section>
  );
};

export default About;
