'use client'

import { motion } from 'framer-motion'
import { useState } from 'react'
import { X } from 'lucide-react'
import { ProjectShowcase } from '@/components/ProjectShowcase'
import { HeroBlobFlow } from '@/components/HeroBlobFlow'
import { InteractiveTerminal } from '@/components/InteractiveTerminal'
import { ExperienceTimeline } from '@/components/ExperienceTimeline'
import { useLanguage } from '@/lib/LanguageContext'

export default function Home() {
  const [bioModalOpen, setBioModalOpen] = useState(false)
  const { t } = useLanguage()

  const projects = t.projects.items.map((item, index) => ({
    ...item,
    id: index === 0
      ? 'project-homelab'
      : index === 1 
        ? 'project-rbc' 
        : index === 2 
          ? 'project-aws' 
          : undefined,
    status: index === 1 
      ? 'completed' as const 
      : index === 3 
        ? 'building' as const 
        : 'live' as const,
    media: index === 1 
      ? { type: 'image' as const, url: '/rbc-images.jpeg' }
      : index === 2
        ? { type: 'video' as const, url: 'https://www.youtube.com/watch?v=wdJ5DN15jus' }
        : undefined,
    videoUrl: index === 3 ? 'https://github.com/rockliang/go-flink' : '#',
    articleUrl: index === 2 ? 'https://lnkd.in/p/gm2PZkWw' : '#',
  }))

  return (
    <main className="min-h-screen">
      {/* Hero */}
      <section className="bg-gradient-to-br from-blue-50 to-purple-50 dark:from-gray-950 dark:to-gray-900 py-24 px-6">
        <div className="max-w-4xl mx-auto">
          {/* Avatar with flowing outcomes */}
          <HeroBlobFlow />

          {/* Description */}
          <motion.div
            initial={{ opacity: 0, y: 20 }}
            animate={{ opacity: 1, y: 0 }}
            transition={{ duration: 0.8, delay: 1 }}
            className="text-center mt-16"
          >
            <h1 className="text-4xl md:text-5xl font-bold text-gray-900 dark:text-white mb-2">
              {t.hero.title}
            </h1>
            <p className="text-xl text-gray-700 dark:text-gray-300 mb-6 font-medium">
              {t.hero.subtitle}
            </p>
            <p className="text-lg text-gray-600 dark:text-gray-400 max-w-2xl mx-auto mb-10">
              {t.hero.description}
            </p>

            <div className="flex gap-4 justify-center flex-wrap mb-8">
              <button
                onClick={() => setBioModalOpen(true)}
                className="bg-blue-600 hover:bg-blue-700 text-white px-8 py-3 rounded-lg font-semibold transition"
              >
                {t.hero.exploreMore}
              </button>
            </div>

            <motion.div
              initial={{ opacity: 0 }}
              animate={{ opacity: 1 }}
              transition={{ duration: 0.8, delay: 1.2 }}
              className="text-sm text-gray-600 dark:text-gray-400"
            >
              💡 {t.hero.terminalHint} <kbd className="bg-gray-200 dark:bg-gray-800 px-2 py-1 rounded">Cmd+K</kbd> {t.hero.terminalHintSuffix}
            </motion.div>
          </motion.div>
        </div>
      </section>

      {/* Experience Timeline */}
      <ExperienceTimeline />

      {/* Features Grid */}
      <section id="features" className="max-w-6xl mx-auto px-6 py-20">
        <motion.div
          initial={{ opacity: 0 }}
          whileInView={{ opacity: 1 }}
          viewport={{ once: true }}
          transition={{ duration: 0.6 }}
          className="mb-16"
        >
          <h2 className="text-4xl font-bold mb-4">{t.projects.title}</h2>
          <p className="text-lg text-gray-600 dark:text-gray-400">
            {t.projects.subtitle}
          </p>
        </motion.div>

        <motion.div
          initial="hidden"
          whileInView="visible"
          viewport={{ once: true }}
          variants={{
            hidden: { opacity: 0 },
            visible: {
              opacity: 1,
              transition: {
                staggerChildren: 0.15,
              },
            },
          }}
          className="grid grid-cols-1 gap-8 max-w-3xl mx-auto"
        >
          {projects.map((project) => (
            <ProjectShowcase 
              key={project.title}
              {...project} 
              watchVideoText={t.projects.watchVideo}
              readArticleText={t.projects.readArticle}
              askPoimenText={t.projects.askPoimen}
            />
          ))}
        </motion.div>
      </section>

      {/* Footer */}
      <footer className="border-t border-gray-200 dark:border-gray-800 bg-gray-50 dark:bg-gray-900 py-12 px-6">
        <div className="max-w-6xl mx-auto text-center text-sm text-gray-600 dark:text-gray-400">
          <p>{t.footer.copyright}</p>
          <p className="mt-2">
            <a href="https://github.com/rockliang" target="_blank" rel="noopener noreferrer" className="hover:text-blue-600 dark:hover:text-blue-400">
              {t.footer.github}
            </a>
            {' • '}
            <a href="mailto:locartrock@gmail.com" className="hover:text-blue-600 dark:hover:text-blue-400">
              {t.footer.email}
            </a>
          </p>
        </div>
      </footer>

      {/* Interactive Terminal */}
      <InteractiveTerminal />

      {/* Bio Modal */}
      {bioModalOpen && (
        <motion.div
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          exit={{ opacity: 0 }}
          className="fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4"
          onClick={() => setBioModalOpen(false)}
        >
          <motion.div
            initial={{ scale: 0.95, opacity: 0 }}
            animate={{ scale: 1, opacity: 1 }}
            exit={{ scale: 0.95, opacity: 0 }}
            onClick={(e) => e.stopPropagation()}
            className="bg-white dark:bg-gray-900 rounded-lg shadow-2xl max-w-2xl w-full max-h-96 overflow-y-auto"
          >
            {/* Header */}
            <div className="sticky top-0 bg-gradient-to-r from-blue-600 to-purple-600 px-6 py-6 flex justify-between items-center">
              <h2 className="text-2xl font-bold text-white">{t.bio.title}</h2>
              <button
                onClick={() => setBioModalOpen(false)}
                className="text-white hover:bg-white/20 rounded-lg p-2 transition"
              >
                <X size={24} />
              </button>
            </div>

            {/* Content */}
            <div className="p-6 space-y-4 text-gray-900 dark:text-white">
              <p className="text-lg">
                {t.bio.intro}
              </p>

              <div>
                <h3 className="font-semibold text-blue-600 dark:text-blue-400 mb-2">{t.bio.expertise.title}</h3>
                <p className="text-sm text-gray-700 dark:text-gray-300">
                  {t.bio.expertise.content}
                </p>
              </div>

              <div>
                <h3 className="font-semibold text-blue-600 dark:text-blue-400 mb-2">{t.bio.highlights.title}</h3>
                <ul className="text-sm text-gray-700 dark:text-gray-300 space-y-1">
                  {t.bio.highlights.items.map((item, i) => (
                    <li key={i}>• {item}</li>
                  ))}
                </ul>
              </div>

              <div>
                <h3 className="font-semibold text-blue-600 dark:text-blue-400 mb-2">{t.bio.educationTitle}</h3>
                <p className="text-sm text-gray-700 dark:text-gray-300 whitespace-pre-line">
                  {t.bio.educationContent}
                </p>
              </div>

              <p className="text-sm text-gray-600 dark:text-gray-400 pt-4 border-t border-gray-200 dark:border-gray-700">
                {t.bio.currentFocus}
              </p>
            </div>
          </motion.div>
        </motion.div>
      )}
    </main>
  )
}
