import React, { useRef, useState, useEffect } from "react";
import { ArrowDown, Check, BookOpen, AlertTriangle, Zap } from "lucide-react";
import { PracticeFromDataset } from "../../../components/lessons/LessonShell";
import {
  BOUNDARIES_EASY,
  BOUNDARIES_MEDIUM,
} from "../../../data/rw/boundaries";
import ClauseBreakdownWidget, {
  type ClauseExample,
} from "../../../components/lessons/ClauseBreakdownWidget";
import DecisionTreeWidget, {
  type TreeScenario,
  type TreeNode,
} from "../../../components/lessons/DecisionTreeWidget";

interface LessonProps {
  onFinish?: () => void;
}

// ── Clause Breakdown data ──────────────────────────────────────────────────
const CLAUSE_EXAMPLES: ClauseExample[] = [
  {
    title: "Em Dash — Nonessential Phrase",
    segments: [
      { text: "The results", type: "subject", label: "Subject" },
      { text: " —", type: "punct" },
      {
        text: " gathered over three years",
        type: "modifier",
        label: "Nonessential Info (must be paired)",
      },
      { text: " —", type: "punct" },
      { text: " confirmed the hypothesis", type: "verb", label: "Predicate" },
      { text: ".", type: "punct" },
    ],
  },
  {
    title: "Possession: team's",
    segments: [
      { text: "The", type: "ic", label: "" },
      { text: " team's", type: "subject", label: "Add 's for possession" },
      { text: " report", type: "ic", label: "" },
      { text: " was submitted on time", type: "verb", label: "Predicate" },
      { text: ".", type: "punct" },
    ],
  },
  {
    title: "Contraction: they're / it's",
    segments: [
      {
        text: "They're",
        type: "conjunction",
        label: "Contraction: they + are",
      },
      { text: " reviewing", type: "verb", label: "" },
      { text: " the data", type: "ic", label: "" },
      { text: " because", type: "dc", label: "" },
      { text: " it's", type: "conjunction", label: "Contraction: it + is" },
      { text: " inconclusive", type: "ic", label: "" },
      { text: ".", type: "punct" },
    ],
  },
];

// ── Decision Tree data ─────────────────────────────────────────────────────
const DASH_SUBTREE: TreeNode = {
  id: "dash",
  question: "Is there already an em dash somewhere else in the sentence?",
  hint: "Em dashes must come in PAIRS when setting off a nonessential phrase in the middle of a sentence.",
  yesLabel: "Yes — there's a dash elsewhere in the sentence",
  noLabel: "No — this is the only dash",
  yes: {
    id: "dash-pair",
    result:
      "✓ Match the punctuation. If an em dash opens a nonessential phrase, another em dash must close it.",
    resultType: "correct",
    ruleRef: "[IC] — [nonessential phrase] — [rest of IC]",
  },
  no: {
    id: "dash-single",
    question:
      "Is the dash at the END of the sentence introducing an explanation or list?",
    yesLabel: "Yes — introducing what follows",
    noLabel: "No — it's in the middle of a sentence",
    yes: {
      id: "dash-end",
      result:
        "✓ A single em dash at the end correctly introduces an explanation, list, or dramatic pause.",
      resultType: "correct",
      ruleRef: "[Complete sentence] — [explanation or list]",
    },
    no: {
      id: "dash-mid",
      result:
        "⚠ A single em dash in the middle of a sentence is incorrect — it must be paired with another em dash, or converted to commas.",
      resultType: "warning",
      ruleRef: "Fix: use matching dashes or commas",
    },
  },
};

const APOSTROPHE_SUBTREE: TreeNode = {
  id: "apostrophe",
  question: "Can you substitute 'it is' or 'they are' in place of the word?",
  hint: "Try substituting: 'its / it is' — if 'it is' fits, use 'it\u2019s'. If not, use 'its' (possessive).",
  yesLabel: "Yes — contraction fits (it is / they are)",
  noLabel: "No — it's showing ownership",
  yes: {
    id: "contraction",
    result:
      "✓ Use the contraction form: it's (it is), they're (they are), who's (who is).",
    resultType: "correct",
    ruleRef: "Contraction: it's = it + is | they're = they + are",
  },
  no: {
    id: "possession",
    question: "Does the noun END in -s already (plural)?",
    yesLabel: "Yes — plural noun ending in -s",
    noLabel: "No — singular noun",
    yes: {
      id: "plural-poss",
      result:
        "✓ Add only an apostrophe after the -s: students' (not students's).",
      resultType: "correct",
      ruleRef: "Plural possession: [noun]s' [thing]",
    },
    no: {
      id: "singular-poss",
      result: "✓ Add 's to the singular noun: scientist's, team's.",
      resultType: "correct",
      ruleRef: "Singular possession: [noun]'s [thing]",
    },
  },
};

const DASH_APOSTROPHE_TREE: TreeNode = {
  id: "root",
  question: "Is this an apostrophe or dash/punctuation question?",
  yesLabel: "Apostrophe (') question",
  noLabel: "Em dash (—) or other punctuation",
  yes: APOSTROPHE_SUBTREE,
  no: DASH_SUBTREE,
};

const TREE_SCENARIOS: TreeScenario[] = [
  {
    label: "Sentence 1",
    sentence:
      "The researchers findings, which had been collected over a decade, were finally published.",
    tree: DASH_APOSTROPHE_TREE,
  },
  {
    label: "Sentence 2",
    sentence: "Each student submitted there essay before the deadline.",
    tree: DASH_APOSTROPHE_TREE,
  },
  {
    label: "Sentence 3",
    sentence:
      "The committee — composed of three senior researchers agreed to delay the vote.",
    tree: DASH_APOSTROPHE_TREE,
  },
];

// ── Lesson component ───────────────────────────────────────────────────────
const EBRWDashesApostrophesLesson: React.FC<LessonProps> = ({ onFinish }) => {
  const [activeSection, setActiveSection] = useState(0);
  const sectionsRef = useRef<(HTMLElement | null)[]>([]);

  useEffect(() => {
    const observers: IntersectionObserver[] = [];
    sectionsRef.current.forEach((el, idx) => {
      if (!el) return;
      const obs = new IntersectionObserver(
        ([entry]) => {
          if (entry.isIntersecting) setActiveSection(idx);
        },
        { threshold: 0.3 },
      );
      obs.observe(el);
      observers.push(obs);
    });
    return () => observers.forEach((o) => o.disconnect());
  }, []);

  const scrollToSection = (index: number) => {
    setActiveSection(index);
    sectionsRef.current[index]?.scrollIntoView({ behavior: "smooth" });
  };

  const SectionMarker = ({
    index,
    title,
    icon: Icon,
  }: {
    index: number;
    title: string;
    icon: React.ElementType;
  }) => {
    const isActive = activeSection === index;
    const isPast = activeSection > index;
    return (
      <button
        onClick={() => scrollToSection(index)}
        className={`flex items-center gap-3 p-3 w-full rounded-lg text-left transition-all ${isActive ? "bg-purple-50" : "hover:bg-slate-50"}`}
      >
        <div
          className={`w-8 h-8 rounded-full flex items-center justify-center shrink-0
          ${isActive ? "bg-purple-600 text-white" : isPast ? "bg-purple-400 text-white" : "bg-slate-200 text-slate-500"}`}
        >
          {isPast ? (
            <Check className="w-4 h-4" />
          ) : (
            <Icon className="w-4 h-4" />
          )}
        </div>
        <p
          className={`text-sm font-bold ${isActive ? "text-purple-900" : "text-slate-600"}`}
        >
          {title}
        </p>
      </button>
    );
  };

  return (
    <div className="flex flex-col lg:flex-row min-h-screen">
      <aside className="w-full lg:w-64 lg:fixed lg:top-14 lg:bottom-0 lg:overflow-y-auto p-4 border-r border-slate-200 bg-slate-50 lg:bg-transparent z-0 hidden lg:block">
        <nav className="space-y-2 pt-6">
          <SectionMarker
            index={0}
            title="Punctuation Anatomy"
            icon={BookOpen}
          />
          <SectionMarker
            index={1}
            title="Decision Tree Lab"
            icon={AlertTriangle}
          />
          <SectionMarker index={2} title="Practice Questions" icon={Zap} />
        </nav>
      </aside>

      <div className="flex-1 lg:ml-64 p-6 md:p-12 max-w-4xl mx-auto">
        {/* Section 0 — Concept + Clause Breakdown */}
        <section
          ref={(el) => {
            sectionsRef.current[0] = el;
          }}
          className="min-h-screen flex flex-col justify-center mb-24 pt-20 lg:pt-0"
        >
          <div className="inline-flex items-center gap-2 bg-purple-100 text-purple-700 px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider mb-4 w-fit">
            Standard English Conventions
          </div>
          <h2 className="text-4xl font-extrabold text-slate-900 mb-2">
            Dashes, Apostrophes &amp; More
          </h2>
          <p className="text-lg text-slate-500 mb-8">
            See how punctuation structures meaning — then master every SAT trap.
          </p>

          {/* Rule summary grid */}
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-8">
            {[
              {
                num: 1,
                rule: "Em Dash Pairs",
                desc: "Use two em dashes to set off nonessential info in the middle of a sentence — one to open, one to close.",
              },
              {
                num: 2,
                rule: "Apostrophe: Possession",
                desc: "Add 's to singular nouns; add ' only to plurals ending in -s (students').",
              },
              {
                num: 3,
                rule: "It's vs. Its",
                desc: "it's = it is (contraction). its = possessive. Test by substituting 'it is.'",
              },
              {
                num: 4,
                rule: "Confused Words",
                desc: "their (possessive), there (place), they're (contraction). who's (who is), whose (possessive).",
              },
            ].map((r) => (
              <div
                key={r.num}
                className="bg-purple-50 border border-purple-200 rounded-xl p-4"
              >
                <div className="flex items-center gap-2 mb-1">
                  <span className="w-6 h-6 rounded-full bg-purple-600 text-white flex items-center justify-center text-xs font-bold shrink-0">
                    {r.num}
                  </span>
                  <span className="font-bold text-purple-900 text-sm">
                    {r.rule}
                  </span>
                </div>
                <p className="text-xs text-slate-600 ml-8">{r.desc}</p>
              </div>
            ))}
          </div>

          {/* Common Traps */}
          <div className="space-y-3 mb-8">
            {[
              {
                label: "Its vs. It's",
                desc: "'its' is possessive (like 'his/her'). 'it's' = it is. Test: substitute 'it is' — if it works, use it's.",
              },
              {
                label: "Unpaired Em Dash",
                desc: "Em dashes around a nonessential phrase MUST be paired. One dash in the middle of a sentence is always wrong.",
              },
              {
                label: "Their / There / They're",
                desc: "'their' = possessive, 'there' = place or existence, 'they're' = they are. The SAT uses all three as traps.",
              },
            ].map((t) => (
              <div
                key={t.label}
                className="flex gap-3 bg-red-50 border border-red-200 rounded-xl px-4 py-3"
              >
                <AlertTriangle className="w-4 h-4 text-red-500 shrink-0 mt-0.5" />
                <div>
                  <p className="text-sm font-bold text-red-800">{t.label}</p>
                  <p className="text-xs text-slate-600 mt-0.5">{t.desc}</p>
                </div>
              </div>
            ))}
          </div>

          {/* Clause Breakdown */}
          <h3 className="text-xl font-bold text-slate-800 mb-3">
            Sentence Anatomy
          </h3>
          <p className="text-sm text-slate-500 mb-4">
            Hover over any colored span to see its label. Use the tabs to switch
            between examples.
          </p>
          <ClauseBreakdownWidget
            examples={CLAUSE_EXAMPLES}
            accentColor="purple"
          />

          <div className="mt-6 bg-purple-900 text-white rounded-2xl p-5">
            <p className="font-bold mb-1">Golden Rule</p>
            <p className="text-sm text-purple-100">
              On the SAT, every apostrophe is either a contraction or possession
              — never both. Test contractions by substitution. Test possessives
              by checking what owns what.
            </p>
          </div>

          <button
            onClick={() => scrollToSection(1)}
            className="mt-12 group flex items-center text-purple-600 font-bold hover:text-purple-800 transition-colors"
          >
            Next: Decision Tree Lab{" "}
            <ArrowDown className="ml-2 w-5 h-5 group-hover:translate-y-1 transition-transform" />
          </button>
        </section>

        {/* Section 1 — Decision Tree */}
        <section
          ref={(el) => {
            sectionsRef.current[1] = el;
          }}
          className="min-h-screen flex flex-col justify-center mb-24"
        >
          <h2 className="text-4xl font-extrabold text-slate-900 mb-2">
            Decision Tree Lab
          </h2>
          <p className="text-lg text-slate-500 mb-8">
            Work through the grammar logic one question at a time. Click your
            answer at each step.
          </p>

          {/* Trap callouts */}
          <div className="space-y-3 mb-8">
            {[
              {
                label: "Mismatched Dash & Comma",
                desc: "Opening with a dash and closing with a comma (or vice versa) is always wrong. Em dashes must pair with em dashes.",
              },
              {
                label: "it's / its Confusion",
                desc: "Test: can you replace it with 'it is'? If yes → it's. If no → its. This is the #1 apostrophe trap on the SAT.",
              },
              {
                label: "Confused Homophones",
                desc: "their/there/they're and whose/who's are the most common wrong-word traps. Always ask: possession, place, or contraction?",
              },
            ].map((t) => (
              <div
                key={t.label}
                className="flex gap-3 bg-red-50 border border-red-200 rounded-xl px-4 py-3"
              >
                <AlertTriangle className="w-4 h-4 text-red-500 shrink-0 mt-0.5" />
                <div>
                  <p className="text-sm font-bold text-red-800">{t.label}</p>
                  <p className="text-xs text-slate-600 mt-0.5">{t.desc}</p>
                </div>
              </div>
            ))}
          </div>

          <DecisionTreeWidget scenarios={TREE_SCENARIOS} accentColor="purple" />

          <button
            onClick={() => scrollToSection(2)}
            className="mt-12 group flex items-center text-purple-600 font-bold hover:text-purple-800 transition-colors"
          >
            Next: Practice Questions{" "}
            <ArrowDown className="ml-2 w-5 h-5 group-hover:translate-y-1 transition-transform" />
          </button>
        </section>

        {/* Section 2 — Quiz */}
        <section
          ref={(el) => {
            sectionsRef.current[2] = el;
          }}
          className="min-h-screen flex flex-col justify-center mb-24"
        >
          <h2 className="text-4xl font-extrabold text-slate-900 mb-6">
            Practice Questions
          </h2>
          {BOUNDARIES_EASY.slice(4, 6).map((q) => (
            <PracticeFromDataset key={q.id} question={q} color="purple" />
          ))}
          {BOUNDARIES_MEDIUM.slice(2, 3).map((q) => (
            <PracticeFromDataset key={q.id} question={q} color="purple" />
          ))}
          <div className="mt-8 text-center">
            <button
              onClick={onFinish}
              className="px-6 py-3 bg-purple-900 text-white font-bold rounded-full hover:bg-purple-700 transition-colors"
            >
              Finish Lesson ✓
            </button>
          </div>
        </section>
      </div>
    </div>
  );
};

export default EBRWDashesApostrophesLesson;
