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: "Prepositional Phrase Trap",
    segments: [
      {
        text: "The results",
        type: "subject",
        label: "True Subject: 'results' (plural)",
      },
      {
        text: " of the study",
        type: "modifier",
        label: "Prepositional Phrase — ignore for agreement",
      },
      { text: " were", type: "verb", label: "Plural Verb ✓" },
      { text: " significant", type: "ic", label: "" },
      { text: ".", type: "punct" },
    ],
  },
  {
    title: "Collective Noun — Singular",
    segments: [
      {
        text: "The committee",
        type: "subject",
        label: "Collective Noun — singular",
      },
      { text: " has", type: "verb", label: "Singular Verb ✓" },
      { text: " reached", type: "verb", label: "" },
      { text: " a decision", type: "ic", label: "" },
      { text: ".", type: "punct" },
    ],
  },
  {
    title: "Inverted Sentence",
    segments: [
      {
        text: "Among the findings",
        type: "modifier",
        label: "Introductory Phrase — not the subject",
      },
      {
        text: " was",
        type: "verb",
        label: "Singular Verb — matches 'one key result'",
      },
      {
        text: " one key result",
        type: "subject",
        label: "True Subject: singular",
      },
      { text: ".", type: "punct" },
    ],
  },
];

// ── Decision Tree data ─────────────────────────────────────────────────────
const SUBJECT_VERB_TREE: TreeNode = {
  id: "root",
  question: "What is the TRUE grammatical subject of the sentence?",
  hint: "Strip away all prepositional phrases (of, in, with, by...), relative clauses, and modifiers. What's left is your true subject.",
  yesLabel: "I found it — it's singular",
  noLabel: "I found it — it's plural",
  yes: {
    id: "singular-subject",
    question:
      "Is the subject a collective noun (team, committee, group, class, family) acting as one unit?",
    yesLabel: "Yes — collective noun",
    noLabel: "No — regular singular noun",
    yes: {
      id: "collective-singular",
      result:
        '✓ Use a SINGULAR verb. Collective nouns acting as a single unit take singular verbs: "The committee has decided."',
      resultType: "correct",
      ruleRef: "[Collective noun] + singular verb (has, was, decides)",
    },
    no: {
      id: "regular-singular",
      question:
        "Is the subject an indefinite pronoun (everyone, each, either, neither, anyone, someone)?",
      yesLabel: "Yes — indefinite pronoun",
      noLabel: "No — regular noun",
      yes: {
        id: "indefinite-singular",
        result:
          "✓ Use a SINGULAR verb. Most indefinite pronouns (everyone, each, either, neither) are grammatically singular.",
        resultType: "correct",
        ruleRef: "Everyone/Each/Either → singular verb",
      },
      no: {
        id: "regular-noun-singular",
        result: "✓ Use a SINGULAR verb: is, was, has, does, -s ending verbs.",
        resultType: "correct",
        ruleRef: "[Singular noun] + singular verb",
      },
    },
  },
  no: {
    id: "plural-subject",
    question: "Is the subject a compound subject joined by 'and'?",
    yesLabel: "Yes — X and Y (compound)",
    noLabel: "No — regular plural",
    yes: {
      id: "compound-and",
      result:
        '✓ Use a PLURAL verb. "X and Y" joined with "and" is always plural: "The teacher and the student are ready."',
      resultType: "correct",
      ruleRef: "[Noun] and [noun] + plural verb",
    },
    no: {
      id: "regular-plural",
      question: "Is the subject joined by 'or' or 'nor'?",
      yesLabel: "Yes — X or/nor Y",
      noLabel: "No — just a regular plural noun",
      yes: {
        id: "or-nor",
        result:
          "⚠ 'Or/Nor' rule: the verb agrees with the CLOSER subject. \"Neither the students nor the teacher IS ready\" (IS agrees with 'teacher', the closer one).",
        resultType: "warning",
        ruleRef: "Neither A nor [B] + verb matching B (the closer subject)",
      },
      no: {
        id: "plain-plural",
        result: "✓ Use a PLURAL verb: are, were, have, do, -s removed.",
        resultType: "correct",
        ruleRef: "[Plural noun] + plural verb",
      },
    },
  },
};

const TREE_SCENARIOS: TreeScenario[] = [
  {
    label: "Sentence 1",
    sentence:
      "The committee of senior researchers have decided to delay the publication.",
    tree: SUBJECT_VERB_TREE,
  },
  {
    label: "Sentence 2",
    sentence:
      "Neither the students nor the professor were prepared for the final exam.",
    tree: SUBJECT_VERB_TREE,
  },
  {
    label: "Sentence 3",
    sentence:
      "Among the most important discoveries of the decade was two breakthrough treatments.",
    tree: SUBJECT_VERB_TREE,
  },
];

// ── Lesson component ───────────────────────────────────────────────────────
const EBRWSubjectVerbLesson: 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.ComponentType<React.SVGProps<SVGSVGElement>>;
  }) => {
    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 z-0 hidden lg:block">
        <nav className="space-y-2 pt-6">
          <SectionMarker index={0} title="Agreement 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">
            Subject-Verb Agreement
          </h2>
          <p className="text-lg text-slate-500 mb-8">
            See how sentences are built — then learn how to match the verb to
            the true subject.
          </p>

          {/* Rule summary grid */}
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-8">
            {[
              {
                num: 1,
                rule: "Strip Prep Phrases",
                desc: 'Ignore "of the X, in the Y" — they hide the true subject. Find the core noun.',
              },
              {
                num: 2,
                rule: "Collective Nouns",
                desc: "team, group, committee, class → singular verb when acting as one unit.",
              },
              {
                num: 3,
                rule: "Indefinite Pronouns",
                desc: "each, every, either, neither, anyone, someone → always singular.",
              },
              {
                num: 4,
                rule: "Or / Nor Rule",
                desc: 'Verb matches the CLOSER subject: "Neither the students nor the teacher IS."',
              },
            ].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"
          />

          <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">
              The SAT hides subjects behind prepositional phrases and inverted
              sentences. Always identify the true subject first — strip
              modifiers, then match the verb.
            </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 agreement logic one question at a time. Click your
            answer at each step.
          </p>

          {/* Trap callouts */}
          <div className="space-y-3 mb-8">
            {[
              {
                label: "Prepositional Phrase Trap",
                desc: 'The results of the study [was/were]? Strip "of the study"; true subject is "results" (plural) → "were".',
              },
              {
                label: "Or/Nor Proximity Rule",
                desc: '"Neither A nor B" → verb matches B (the noun closer to the verb).',
              },
              {
                label: "Inverted Sentence",
                desc: '"There is/are..." and "Among X was/were Y" — find the true subject after the verb.',
              },
            ].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>
          {FORM_STRUCTURE_EASY.slice(0, 2).map((q) => (
            <PracticeFromDataset key={q.id} question={q} color="purple" />
          ))}
          {FORM_STRUCTURE_MEDIUM.slice(0, 1).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 EBRWSubjectVerbLesson;
