import React from 'react';
import { motion } from 'framer-motion';
import { ExternalLink, ArrowUpRight, MousePointerClick } from 'lucide-react';

// Optimized constants to prevent object recreation on renders
const HARDWARE_ACCELERATION_STYLE = {
  transform: 'translateZ(0)',
  willChange: 'transform, opacity'
} as const;

const HARDWARE_ACCELERATION_TRANSFORM_STYLE = {
  transform: 'translateZ(0)',
  willChange: 'transform'
} as const;

const projects = [
  {
    title: 'Play Or Draw',
    category: 'Web Development',
    year: '2023',
    description: 'The Marketplace for Players. A fully-fledged custom-made marketplace designed with a smoother user experience at its core and lower fees to support the trading card game community.',
    technologies: ['Next.js', 'Backend (Express)', 'Shopify API'],
    metrics: ['Advanced Stripe integrations', 'Custom Built Marketplace', 'Crystal Commerce Partnership'],
    image: 'https://images.pexels.com/photos/3184291/pexels-photo-3184291.jpeg?auto=compress&cs=tinysrgb&w=800',
    ratio: 'aspect-[1.618/1]' // Golden ratio
  },
  {
    title: 'Website Portfolio Showcase',
    category: 'Web Development & Design',
    year: '2024',
    description: 'Creative Showcase. Our comprehensive portfolio featuring innovative web designs, interactive experiences, and modern development approaches that demonstrate our expertise across various industries.',
    technologies: ['React', 'Framer Motion', 'Modern CSS', 'Responsive Design'],
    metrics: ['Custom design', 'Mobile friendly', 'Fits any budget'],
    image: '/assets/sitesportfolioscreengrab.png',
    ratio: 'aspect-[1.618/1]' // Golden ratio - same as live previews
  },
  {
    title: 'DesignRF',
    category: 'Web Development, RF Design',
    year: '2021',
    description: 'Streamlined RF Design. A cost-effective solution for RF engineers to create heatmap-overlayed floor plans for bidding projects, estimating costs, and more.',
    technologies: ['Frontend (Svelte)', 'Backend (Flask)'],
    metrics: ['7x faster than competitors', '10x cheaper than competitors', '$1M+ in SAAS costs saved to date'],
    image: 'https://images.pexels.com/photos/3184339/pexels-photo-3184339.jpeg?auto=compress&cs=tinysrgb&w=800',
    ratio: 'aspect-[1.618/1]'
  }
];

const Portfolio = () => {

  return (
    <section id="portfolio" className="py-16 relative min-h-screen" role="region" aria-label="Portfolio and work showcase">

      {/* Asymmetric background elements */}
      <div className="absolute top-1/4 left-0 w-1/4 h-1/2 bg-gradient-to-r from-[#2D80FF]/5 to-transparent transform -skew-y-12 z-10" />
      
      <div className="max-w-7xl mx-auto px-6 lg:px-32 relative z-10">
        <motion.div
          initial={{ opacity: 0, y: 20 }}
          whileInView={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.8 }}
          className="mb-20"
          style={HARDWARE_ACCELERATION_STYLE}
        >
          <div className="flex flex-col lg:flex-row items-start justify-between gap-6 lg:gap-12">
            <div className="lg:w-1/2">
              <h3 className="text-4xl sm:text-5xl md:text-6xl lg:text-7xl font-extralight text-white leading-tight">
                Our
                <br />
                <span className="text-[#2D80FF] font-light">Work</span>
              </h3>
            </div>
            <div className="lg:w-1/2">
              <p className="text-lg sm:text-xl font-light text-white/60 leading-relaxed">
                Amazing projects we've worked on. Each represents a unique challenge
                solved through <em>innovative thinking</em> and <strong>meticulous execution</strong>.
              </p>
            </div>
          </div>
        </motion.div>

        <div className="space-y-16 sm:space-y-20 lg:space-y-24">
          {projects.map((project, index) => (
            <motion.div
              key={index}
              initial={{ opacity: 0, y: 30 }}
              whileInView={{ opacity: 1, y: 0 }}
              transition={{ duration: 0.6, delay: index * 0.15, ease: "easeOut" }}
              className={`grid grid-cols-1 lg:grid-cols-12 gap-8 sm:gap-10 lg:gap-12 items-center ${
                index % 2 === 1 ? 'lg:grid-flow-col-dense' : ''
              }`}
              style={{
                transform: 'translateZ(0)', // Hardware acceleration
                willChange: 'transform, opacity'
              }}
            >
              <motion.div
                initial={{ opacity: 0 }}
                whileInView={{ opacity: 1 }}
                transition={{ duration: 0.5, delay: index * 0.15 + 0.2, ease: "easeOut" }}
                whileHover={{ scale: 1.01, rotate: index % 2 === 0 ? 0.5 : -0.5 }}
                className={`lg:col-span-7 ${index % 2 === 1 ? 'lg:col-start-6' : ''}`}
                style={HARDWARE_ACCELERATION_TRANSFORM_STYLE}
              >
                <a
                  href={
                    project.title === 'Play Or Draw' ? 'https://playordraw.com/' :
                    project.title === 'DesignRF' ? 'https://www.kmswireless.com/prop' :
                    project.title === 'Website Portfolio Showcase' ? 'https://sites.daedastream.com' :
                    '#portfolio'
                  }
                  target="_blank"
                  rel="noopener noreferrer"
                  className="relative group overflow-hidden rounded-3xl block"
                >
                  <div
                    className={`aspect-[4/3] sm:aspect-[3/2] lg:${project.ratio.split(' ')[0]} bg-gradient-to-br from-white/10 to-white/5 relative overflow-hidden`}
                    style={{
                      transform: 'translateZ(0)', // Hardware acceleration
                      willChange: 'transform'
                    }}
                  >
                    {project.title === 'Play Or Draw' ? (
                      // Play Or Draw - iframe preview
                      <div className="absolute inset-0 w-full h-full">
                        <iframe
                          src="https://playordraw.com/"
                          title="Play Or Draw Website Preview"
                          className="absolute inset-0 w-full h-full border-0 opacity-80 group-hover:opacity-100 transition-all duration-700 z-10 pointer-events-none"
                          sandbox="allow-scripts allow-same-origin"
                          loading="lazy"
                          scrolling="no"
                          style={{ overflow: 'hidden' }}
                        />
                      </div>
                    ) : project.title === 'Website Portfolio Showcase' ? (
                      // Website Portfolio Showcase - Screenshot preview
                      <img
                        src={project.image}
                        alt={`${project.title} - ${project.category} project`}
                        className="absolute inset-0 w-full h-full object-cover transition-all duration-500"
                        loading="lazy"
                        decoding="async"
                      />
                    ) : (
                      project.title === 'DesignRF' ? (
                        // DesignRF - Live preview of KMS Wireless property page
                        <iframe
                          src="https://www.kmswireless.com/prop"
                          title="DesignRF - KMS Wireless Property Tool Preview"
                          className="absolute inset-0 w-full h-full border-0 opacity-80 group-hover:opacity-100 transition-all duration-700 z-10 scale-75 origin-top-left pointer-events-none"
                          style={{
                            width: '133.33%',
                            height: '133.33%',
                            transform: 'scale(0.75)',
                            transformOrigin: 'top left',
                            overflow: 'hidden'
                          }}
                          sandbox="allow-scripts allow-same-origin"
                          loading="lazy"
                          scrolling="no"
                        />
                      ) : (
                        // Other projects - Use stock images
                        <img
                          src={project.image}
                          alt={`${project.title} - ${project.category} project showcasing ${project.technologies.join(', ')} development`}
                          className={`absolute inset-0 w-full h-full object-cover opacity-80 group-hover:opacity-100 group-hover:scale-105 transition-all duration-700 ${
                            project.title === 'Arrow Plumbing CRM' ? 'object-center' : ''
                          }`}
                          loading="lazy"
                          decoding="async"
                        />
                      )
                    )}

                    {/* Hover overlay */}
                    <div className="absolute inset-0 bg-black/60 opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-center justify-center z-20">
                      <div className="flex flex-col items-center space-y-3 text-white">
                        <MousePointerClick size={32} className="animate-pulse" />
                        <span className="text-lg font-light">Click to View</span>
                      </div>
                    </div>
                  </div>
                </a>
              </motion.div>

              <div className={`lg:col-span-5 ${index % 2 === 1 ? 'lg:col-start-1' : ''}`}>
                <motion.div
                  initial={{ opacity: 0, y: 20 }}
                  whileInView={{ opacity: 1, y: 0 }}
                  transition={{ duration: 0.5, delay: index * 0.15 + 0.3, ease: "easeOut" }}
                  className="space-y-6"
                  style={{
                    transform: 'translateZ(0)', // Hardware acceleration
                    willChange: 'transform, opacity'
                  }}
                >
                  <div className="text-white/40 text-xs font-light tracking-wider uppercase mb-2">
                    {project.year}
                  </div>
                  
                  <div className="text-[#2D80FF] text-sm font-light tracking-wider uppercase">
                    {project.category}
                  </div>
                  
                  <h3 className="text-2xl sm:text-3xl md:text-4xl lg:text-5xl font-extralight text-white leading-tight">
                    {project.title}
                  </h3>
                  
                  <p className="text-lg font-light text-white/70 leading-relaxed">
                    {project.description}
                  </p>

                  {project.technologies && (
                    <div className="flex flex-wrap gap-2 mt-4">
                      {project.technologies.map((tech, techIndex) => (
                        <span key={techIndex} className="text-xs bg-white/10 text-white/80 px-3 py-1 rounded-full">
                          {tech}
                        </span>
                      ))}
                    </div>
                  )}
                  
                  {project.metrics && (
                    <div className="mt-4 space-y-1">
                      {project.metrics.map((metric, metricIndex) => (
                        <div key={metricIndex} className="text-sm text-[#2D80FF] font-light">
                          • {metric}
                        </div>
                      ))}
                    </div>
                  )}

                  <motion.a
                    href={
                      project.title === 'Play Or Draw' ? 'https://playordraw.com/' :
                      project.title === 'DesignRF' ? 'https://www.kmswireless.com/prop' :
                      project.title === 'Website Portfolio Showcase' ? 'https://sites.daedastream.com' : '#'
                    }
                    target="_blank"
                    rel="noopener noreferrer"
                    whileHover={{ x: 5 }}
                    whileTap={{ scale: 0.98 }}
                    className="flex items-center space-x-2 text-white/80 hover:text-[#2D80FF] transition-colors duration-300 min-h-[44px] touch-manipulation active:text-[#2D80FF] p-2 -m-2"
                  >
                    <span className="font-light">
                      {project.title === 'Website Portfolio Showcase' ? 'View Portfolio' : 'View Project'}
                    </span>
                    <ArrowUpRight size={20} />
                  </motion.a>
                </motion.div>
              </div>
            </motion.div>
          ))}
        </div>

        {/* Client Logos Section */}
        <motion.div
          initial={{ opacity: 0, y: 10 }}
          whileInView={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.8, delay: 0.6, ease: "easeOut" }}
          className="mt-32 bg-gradient-to-br from-[#2D80FF]/10 to-white/5 backdrop-blur-xl border border-white/10 rounded-3xl p-12 pb-16 max-w-4xl mx-auto"
          style={{
            transform: 'translateZ(0)', // Hardware acceleration
            willChange: 'transform, opacity'
          }}
        >
          <h3 className="text-3xl font-light text-white mb-8 text-center">
            Other Amazing People We've Worked With
          </h3>
          <div className="grid grid-cols-2 gap-12 items-center justify-items-center">
            {[
              { src: "/assets/DriveWize_full.png", alt: "DriveWize" },
              { src: "/assets/ap.f90723cc.png", alt: "Arrow Plumbing & Drain Services" },
              { src: "/assets/CSAlliance2.png", alt: "CSAlliance" },
              { src: "/assets/perlman.ee243bed.png", alt: "The Perlman Clinic" }
            ].map((logo, index) => (
              <motion.div
                key={index}
                initial={{ opacity: 0 }}
                whileInView={{ opacity: 1 }}
                transition={{ duration: 0.4, delay: 0.8 + index * 0.1, ease: "easeOut" }}
                whileHover={{ scale: 1.05 }}
                className="flex items-center justify-center h-24 w-48"
                style={{
                  transform: 'translateZ(0)', // Hardware acceleration
                  willChange: 'transform, opacity'
                }}
              >
                <img
                  src={logo.src}
                  alt={logo.alt}
                  className="max-h-20 max-w-44 w-auto h-auto object-contain filter brightness-0 invert opacity-70 hover:opacity-100 transition-opacity duration-300"
                  style={{
                    maxWidth: '176px',
                    maxHeight: '80px'
                  }}
                  loading="lazy"
                />
              </motion.div>
            ))}
          </div>
        </motion.div>
      </div>
    </section>
  );
};

export default Portfolio;