import Link from 'next/link';
import { ArrowRight, BookOpen, ClipboardList, FileText } from 'lucide-react';

import { BUNDLE_ITEMS, FOUNDATION_ITEMS } from './hubData';
import { C, SERIF } from './hubTheme';
import DownloadGate from './DownloadGate';
import { trackHubEvent } from './hubAnalytics';

const ICONS = { alert: FileText, book: BookOpen, file: FileText, list: ClipboardList };

function GuideGrid({ items, unlocked, email, onUnlock }) {
  return (
    <div className="mt-7 grid gap-5 md:grid-cols-2">
      {items.map((item) => {
        const Icon = ICONS[item.icon] || BookOpen;
        return (
          <article
            key={item.key}
            className="flex flex-col rounded-3xl bg-white p-7 shadow-sm"
            style={{ border: '1px solid rgba(212,175,56,0.45)' }}
          >
            <div className="mb-4 flex h-11 w-11 flex-shrink-0 items-center justify-center rounded-xl" style={{ background: C.maroon }}>
              <Icon className="h-5 w-5" style={{ color: C.gold }} aria-hidden="true" />
            </div>
            <p className="mb-2 text-[10px] font-extrabold uppercase tracking-[0.18em]" style={{ color: C.goldDeep }}>{item.category}</p>
            <h3 className="text-lg font-extrabold leading-snug" style={{ color: C.maroon, fontFamily: SERIF }}>
              {item.title}
            </h3>
            <p className="mt-1 text-xs font-semibold leading-5" style={{ color: C.goldDeep }}>{item.sub}</p>
            <p className="mt-4 rounded-xl bg-[#FAF6EF] p-3 text-xs leading-5" style={{ color: C.ink }}>
              <strong>Learning objective:</strong> {item.objective}
            </p>
            <ul className="my-5 flex-1 space-y-2.5">
              {item.bullets.map((bullet) => (
                <li key={bullet} className="flex items-start gap-2.5 text-sm leading-6" style={{ color: C.ink }}>
                  <span className="mt-2 h-1.5 w-1.5 flex-shrink-0 rounded-full" style={{ background: C.gold }} />
                  {bullet}
                </li>
              ))}
            </ul>
            <p className="mb-4 text-xs font-semibold" style={{ color: C.ink }}><strong>Format:</strong> {item.format}</p>
            <div className="flex flex-col items-start gap-3">
              <Link
                href={item.tutorial.href}
                onClick={() => trackHubEvent('related_tutorial_click', {
                  source_resource: item.key,
                  destination_topic: item.tutorial.href,
                })}
                className="inline-flex items-center gap-1.5 text-xs font-extrabold underline decoration-[#D4AF37] decoration-2 underline-offset-4"
                style={{ color: C.wine }}
              >
                {item.tutorial.label} <ArrowRight className="h-3.5 w-3.5" aria-hidden="true" />
              </Link>
              <DownloadGate
                unlocked={unlocked}
                pdfUrl={item.pdf_url}
                email={email}
                onUnlock={onUnlock}
                resourceId={item.key}
                topic={item.category}
                format={item.format}
                lockedLabel="Unlock This Free Guide"
              />
            </div>
          </article>
        );
      })}
    </div>
  );
}

export default function HubFoundations({ unlocked, email }) {
  const jumpToForm = () => {
    trackHubEvent('resource_form_start', { form_id: 'algebra_pdf_pack', source: 'guide_card' });
    document.getElementById('hub-form')?.scrollIntoView({ behavior: 'smooth', block: 'center' });
  };

  return (
    <section id="study-guides" className="scroll-mt-16 px-5 py-16 lg:py-20" style={{ background: '#FFFFFF' }}>
      <div className="mx-auto max-w-6xl">
        <div className="max-w-3xl">
          <p className="mb-2 text-[10px] font-bold uppercase tracking-[0.22em]" style={{ color: C.goldDeep }}>
            Plan · practise · review
          </p>
          <h2 className="text-2xl font-extrabold sm:text-3xl" style={{ color: C.maroon, fontFamily: SERIF }}>
            Texas Algebra 1 Study and Planning Guides
          </h2>
          <p className="mt-3 text-sm leading-relaxed sm:text-base" style={{ color: C.ink }}>
            Use the Texas-focused guides to plan practice and understand current assessment categories. Always
            confirm official standards and assessment requirements with the Texas Education Agency.
          </p>
        </div>
        <GuideGrid items={BUNDLE_ITEMS} unlocked={unlocked} email={email} onUnlock={jumpToForm} />

        <div className="mt-14 max-w-3xl">
          <h2 className="text-2xl font-extrabold sm:text-3xl" style={{ color: C.maroon, fontFamily: SERIF }}>
            Additional Algebra 1 Practice and Parent Guides
          </h2>
          <p className="mt-3 text-sm leading-relaxed sm:text-base" style={{ color: C.ink }}>
            These supporting materials focus on error analysis, factoring and systems. They are useful beyond
            Texas because the underlying Algebra 1 methods are widely taught.
          </p>
        </div>
        <GuideGrid items={FOUNDATION_ITEMS} unlocked={unlocked} email={email} onUnlock={jumpToForm} />
      </div>
    </section>
  );
}
