"use client";

import Link from "next/link";
import { useEffect, useState } from "react";
import { motion, useReducedMotion } from "framer-motion";

export type LandingFeature = {
  href: string;
  title: string;
  blurb: string;
  cta: string;
  icon: "services" | "classifieds" | "jobs" | "explore";
};

const icons = {
  services: (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
      <path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z" />
    </svg>
  ),
  classifieds: (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
      <path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4Z" />
      <path d="M3 6h18" />
      <path d="M16 10a4 4 0 0 1-8 0" />
    </svg>
  ),
  jobs: (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
      <rect width="20" height="14" x="2" y="7" rx="2" />
      <path d="M16 7V5a2 2 0 0 0-2-2h-4a2 2 0 0 0-2 2v2" />
    </svg>
  ),
  explore: (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
      <path d="M12 21s7-4.5 7-11a7 7 0 1 0-14 0c0 6.5 7 11 7 11Z" />
      <circle cx="12" cy="10" r="2.5" />
    </svg>
  ),
};

function FeatureCard({
  feature,
  index,
  ready,
  reduce,
}: {
  feature: LandingFeature;
  index: number;
  ready: boolean;
  reduce: boolean | null;
}) {
  const card = (
    <Link href={feature.href} className="landing-feature-card group">
      <span className="landing-feature-card__glow" aria-hidden />
      <span className="landing-feature-card__icon">{icons[feature.icon]}</span>
      <h3 className="landing-feature-card__title">{feature.title}</h3>
      <p className="landing-feature-card__blurb">{feature.blurb}</p>
      <span className="landing-feature-card__cta">
        {feature.cta}
        <span className="landing-feature-card__arrow" aria-hidden>
          →
        </span>
      </span>
    </Link>
  );

  if (!ready || reduce) return card;

  return (
    <motion.div
      initial={{ opacity: 0, y: 28 }}
      whileInView={{ opacity: 1, y: 0 }}
      viewport={{ once: true, margin: "-8%" }}
      transition={{
        delay: index * 0.08,
        duration: 0.5,
        ease: [0.22, 1, 0.36, 1],
      }}
      whileHover={{ y: -6 }}
    >
      {card}
    </motion.div>
  );
}

export function LandingFeatures({
  features,
  title,
  subtitle,
}: {
  features: LandingFeature[];
  title: string;
  subtitle: string;
}) {
  const [ready, setReady] = useState(false);
  const reduce = useReducedMotion();

  useEffect(() => {
    setReady(true);
  }, []);

  return (
    <section id="modulos" className="landing-features" aria-labelledby="landing-features-title">
      <div className="landing-features__head">
        <p className="landing-features__kicker">La plataforma</p>
        <h2 id="landing-features-title" className="landing-features__title">
          {title}
        </h2>
        <p className="landing-features__sub">{subtitle}</p>
      </div>
      <div className="landing-features__grid">
        {features.map((f, i) => (
          <FeatureCard
            key={f.href}
            feature={f}
            index={i}
            ready={ready}
            reduce={reduce}
          />
        ))}
      </div>
    </section>
  );
}
