import React, { useState } from 'react';
import { motion } from 'framer-motion';
import { projectsData } from '../data/projectsData';
import { ProjectCard } from './ProjectCard';
import AnimatedSection from '../../../shared/components/AnimatedSection';
import type { Project } from '../../../shared/types';

type Category = 'Alle' | 'Webanwendungen' | 'UI-Komponenten' | 'Fullstack';

export const ProjectsSection: React.FC = () => {
  const [selectedCategory, setSelectedCategory] = useState<Category>('Alle');

  const categories: Category[] = ['Alle', 'Webanwendungen', 'UI-Komponenten', 'Fullstack'];

  const filteredProjects = projectsData.filter(
    (project: Project) =>
      selectedCategory === 'Alle' || project.category === selectedCategory
  );

  return (
    <AnimatedSection id="projects" className="py-20 px-4 sm:px-6 lg:px-8">
      <div className="max-w-7xl mx-auto">
        {/* Section Header */}
        <div className="text-center mb-16">
          <motion.div
            initial={{ opacity: 0, y: -20 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: true }}
            transition={{ duration: 0.5 }}
            className="inline-block px-4 py-2 rounded-full border border-green-500/50 bg-green-500/10 mb-6"
          >
            <span className="text-green-400 text-sm font-medium">Meine Projekte</span>
          </motion.div>

          <h2 className="text-4xl sm:text-5xl font-bold text-white mb-4">
            Ausgewählte Projekte
          </h2>
          <p className="text-gray-400 text-lg max-w-2xl mx-auto">
            Eine Auswahl meiner besten Lösungen und Erfolge
          </p>
        </div>

        {/* Category Filter */}
        <motion.div
          initial={{ opacity: 0, y: 20 }}
          whileInView={{ opacity: 1, y: 0 }}
          viewport={{ once: true }}
          className="flex flex-wrap justify-center gap-3 mb-12"
        >
          {categories.map((category) => (
            <motion.button
              key={category}
              onClick={() => setSelectedCategory(category)}
              whileHover={{ scale: 1.05 }}
              whileTap={{ scale: 0.95 }}
              animate={{
                backgroundColor:
                  selectedCategory === category ? 'rgba(34,197,94,1)' : 'transparent',
                borderColor:
                  selectedCategory === category
                    ? 'rgba(34,197,94,1)'
                    : 'rgba(34,197,94,0.3)',
              }}
              transition={{ duration: 0.2 }}
              className={`px-6 py-2 rounded-full font-medium ${
                selectedCategory === category
                  ? 'text-black'
                  : 'border border-green-500/30 text-gray-300'
              }`}
            >
              {category}
            </motion.button>
          ))}
        </motion.div>

        {/* Projects Grid */}
        <motion.div
          layout
          className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"
        >
          {filteredProjects.map((project: Project, index: number) => (
            <ProjectCard key={project.id} project={project} index={index} />
          ))}
        </motion.div>
      </div>
    </AnimatedSection>
  );
};
