'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()

  // Order: 0=Homelab, 1=Poimen Memory, 2=Poimen Workflow, 3=RBC, 4=AWS
  const projects = t.projects.items.map((item, index) => ({
    ...item,
    id: ['project-homelab', 'project-poimen-memory', 'project-poimen-workflow', 'project-rbc', 'project-aws'][index],
    status: index === 3
      ? 'completed' as const
      : index <= 2
        ? 'building' as const
        : 'live' as const,
    media: index === 3
      ? { type: 'image' as const, url: '/rbc-images.jpeg' }
      : index === 4
        ? { type: 'video' as const, url: 'https://www.youtube.com/watch?v=wdJ5DN15jus' }
        : undefined,
    videoUrl: '#',
    articleUrl: index === 4 ? 'https://lnkd.in/p/gm2PZkWw' : '#',
    ciRepos: index === 0 ? [
      { label: 'portfolio', repo: 'rock/riotpiao.com', forgejoBase: 'https://forgejo.riotpiao.com/rock/riotpiao.com' },
      { label: 'homelab-frontend', repo: 'rock/homelab-frontend', forgejoBase: 'https://forgejo.riotpiao.com/rock/homelab-frontend' },
      { label: 'kmsvc', repo: 'rock/kmsvc-manage', forgejoBase: 'https://forgejo.riotpiao.com/rock/kmsvc-manage' },
    ] : index === 1 ? [
      { label: 'poimen-mem', repo: 'rock/poimen-memory', forgejoBase: 'https://forgejo.riotpiao.com/rock/poimen-memory' },
    ] : index === 2 ? [
      { label: 'poimen-wf', repo: 'rock/poimen-workflows', forgejoBase: 'https://forgejo.riotpiao.com/rock/poimen-workflows' },
    ] : undefined,
  }))

  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>
  )
}
