import React, { useRef, useState, useEffect } from "react";
import { ArrowDown, Check, BookOpen, AlertTriangle, Zap } from "lucide-react";
import { PracticeFromDataset } from "../../../components/lessons/LessonShell";
import {
  FORM_STRUCTURE_EASY,
  FORM_STRUCTURE_MEDIUM,
} from "../../../data/rw/form-structure-sense";
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: "Parallel Structure — List",
    segments: [
      {
        text: "The program requires",
        type: "subject",
        label: "Subject + Verb",
      },
      { text: " attending lectures", type: "conjunction", label: "Gerund: ✓" },
      { text: ",", type: "punct" },
      {
        text: " completing assignments",
        type: "conjunction",
        label: "Gerund: ✓",
      },
      { text: ",", type: "punct" },
      { text: " and", type: "conjunction", label: "FANBOYS" },
      {
        text: " passing the final exam",
        type: "conjunction",
        label: "Gerund: ✓ — all match",
      },
      { text: ".", type: "punct" },
    ],
  },
  {
    title: "Misplaced Modifier",
    segments: [
      {
        text: "Running through the park",
        type: "modifier",
        label: "Modifier: describes who is running",
      },
      { text: ",", type: "punct" },
      {
        text: " the sunset",
        type: "subject",
        label: "⚠ 'The sunset' can't run — modifier is misplaced!",
      },
      { text: " was beautiful", type: "verb", label: "" },
      { text: ".", type: "punct" },
    ],
  },
  {
    title: "Dangling Modifier — Fixed",
    segments: [
      {
        text: "Running through the park",
        type: "modifier",
        label: "Modifier: describes who is running",
      },
      { text: ",", type: "punct" },
      {
        text: " she",
        type: "subject",
        label: "✓ 'She' is the one running — modifier now matches",
      },
      { text: " watched the sunset", type: "verb", label: "" },
      { text: ".", type: "punct" },
    ],
  },
];

// ── Decision Tree data ─────────────────────────────────────────────────────
const STRUCTURE_TREE: TreeNode = {
  id: "root",
  question: "Is this a PARALLELISM or MODIFIER problem?",
  hint: "Parallelism: a list where elements don't match in form. Modifier: a descriptive phrase that's attached to the wrong noun.",
  yesLabel: "Parallelism — list items don't match",
  noLabel: "Modifier — descriptive phrase in wrong place",
  yes: {
    id: "parallelism",
    question: "What grammatical form do MOST items in the list use?",
    hint: "Identify the form of the majority: nouns? gerunds (-ing)? infinitives (to-)? adjectives?",
    yesLabel: "Gerunds (-ing forms) dominate",
    noLabel: "Nouns, infinitives, or adjectives dominate",
    yes: {
      id: "parallel-gerunds",
      result:
        "✓ Convert ALL list items to gerunds (-ing). 'The program requires attending, completing, and passing' — all match.",
      resultType: "correct",
      ruleRef: "[verb]-ing, [verb]-ing, and [verb]-ing",
    },
    no: {
      id: "parallel-other",
      question: "Are the items a mix of infinitives (to-verb) and other forms?",
      yesLabel: "Yes — some 'to-verb', some don't match",
      noLabel: "No — nouns or adjectives are mixed",
      yes: {
        id: "parallel-infinitives",
        result:
          "✓ Convert ALL list items to infinitives (to + verb): 'to attend, to complete, and to pass.'",
        resultType: "correct",
        ruleRef: "to [verb], to [verb], and to [verb]",
      },
      no: {
        id: "parallel-nouns",
        result:
          "✓ Make all items the same part of speech — all nouns, all adjectives, or all the same structure. The parallelism rule: match the form of the first item.",
        resultType: "correct",
        ruleRef: "[noun], [noun], and [noun] — all same form",
      },
    },
  },
  no: {
    id: "modifier",
    question:
      "Is the modifier at the BEGINNING of the sentence (before the comma)?",
    hint: "Opening modifiers like 'Running through the park,' must be immediately followed by the noun/pronoun they describe.",
    yesLabel: "Yes — opens the sentence before a comma",
    noLabel: "No — modifier is elsewhere in the sentence",
    yes: {
      id: "opening-modifier",
      question:
        "Does the word immediately AFTER the comma refer to who/what is doing the action in the modifier?",
      yesLabel: "Yes — it matches",
      noLabel: "No — it doesn't make sense (dangling modifier)",
      yes: {
        id: "modifier-correct",
        result:
          "✓ The modifier is correctly placed. It immediately precedes the noun it describes.",
        resultType: "correct",
        ruleRef: "[Modifier phrase], [the noun it describes] [verb]...",
      },
      no: {
        id: "dangling-modifier",
        result:
          "⚠ Dangling modifier! The noun after the comma must be the one performing the action in the phrase. Fix: rewrite so the correct subject follows the comma.",
        resultType: "warning",
        ruleRef: "Fix: '[Modifier], [WHO is doing it] [verb]'",
      },
    },
    no: {
      id: "mid-sentence-modifier",
      result:
        "⚠ Misplaced modifier! The modifier should be placed immediately next to what it describes. Move it closer to the word it modifies.",
      resultType: "warning",
      ruleRef: "Place modifier directly next to the word it describes",
    },
  },
};

const TREE_SCENARIOS: TreeScenario[] = [
  {
    label: "Sentence 1",
    sentence:
      "The program requires attending lectures, to complete assignments, and passing the final.",
    tree: STRUCTURE_TREE,
  },
  {
    label: "Sentence 2",
    sentence:
      "Having reviewed all the evidence, a verdict was reached by the jury.",
    tree: STRUCTURE_TREE,
  },
  {
    label: "Sentence 3",
    sentence: "She enjoys hiking, to read, and cooking on weekends.",
    tree: STRUCTURE_TREE,
  },
];

// ── Lesson component ───────────────────────────────────────────────────────
const EBRWSentenceStructureLesson: 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="Structure 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">
            Sentence Structure
          </h2>
          <p className="text-lg text-slate-500 mb-8">
            See how sentences are built — then learn exactly where parallelism
            breaks and modifiers go wrong.
          </p>

          {/* Rule summary grid */}
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-8">
            {[
              {
                num: 1,
                rule: "Parallelism in Lists",
                desc: "All list items must match: all gerunds, all infinitives, all nouns, etc.",
              },
              {
                num: 2,
                rule: "Parallel Comparisons",
                desc: "'more X than Y' — X and Y must be the same type (comparing like to like).",
              },
              {
                num: 3,
                rule: "Opening Modifier Rule",
                desc: "The noun immediately after a comma must be what the modifier describes.",
              },
              {
                num: 4,
                rule: "Squinting Modifiers",
                desc: "A modifier must be placed unambiguously next to what it modifies.",
              },
            ].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>

          {/* 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"
          />

          {/* SAT Traps */}
          <h3 className="text-xl font-bold text-slate-800 mt-8 mb-4">
            Common SAT Traps
          </h3>
          <div className="space-y-3 mb-8">
            {[
              {
                label: "Subtle Parallelism Break",
                desc: "'researching, to analyze, and write' — the break ('to analyze') looks acceptable but breaks the gerund pattern.",
              },
              {
                label: "Dangling Modifier",
                desc: "'Having reviewed the evidence, a verdict was reached' — the verdict didn't review anything. The jury did.",
              },
              {
                label: "Squinting Modifier",
                desc: "A modifier placed between two clauses so it's unclear which one it modifies.",
              },
            ].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>

          <div className="mt-2 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">
              Parallel structure and modifiers both follow one principle: every
              part of a sentence must connect clearly and consistently to what
              it describes or lists. Mismatch in form or placement = SAT error.
            </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>

          <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>
          {FORM_STRUCTURE_EASY.slice(6, 8).map((q) => (
            <PracticeFromDataset key={q.id} question={q} color="purple" />
          ))}
          {FORM_STRUCTURE_MEDIUM.slice(3, 4).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 EBRWSentenceStructureLesson;
