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: "Pronoun\u2013Antecedent Agreement",
    segments: [
      { text: "Each student", type: "subject", label: "Antecedent: singular" },
      { text: " must submit", type: "verb", label: "Verb" },
      {
        text: " their",
        type: "conjunction",
        label: "\u26a0 Incorrect: \u2018their\u2019 is plural",
      },
      { text: " own assignment", type: "ic", label: "" },
      { text: ".", type: "punct" },
    ],
  },
  {
    title: "Who vs. Whom",
    segments: [
      {
        text: "The scientist",
        type: "subject",
        label: "Subject of main clause",
      },
      {
        text: " who",
        type: "conjunction",
        label: "Subject pronoun: \u2018who\u2019 replaces \u2018he/she\u2019",
      },
      { text: " won the award", type: "ic", label: "Relative clause" },
      { text: " is my mentor", type: "verb", label: "Main verb" },
      { text: ".", type: "punct" },
    ],
  },
  {
    title: "Pronoun Case: Subject vs. Object",
    segments: [
      { text: "The award was given to", type: "ic", label: "" },
      {
        text: " her",
        type: "conjunction",
        label:
          "Object pronoun: \u2018her\u2019 after preposition \u2018to\u2019",
      },
      { text: " and", type: "conjunction", label: "" },
      {
        text: " me",
        type: "conjunction",
        label:
          "Object pronoun: \u2018me\u2019 not \u2018I\u2019 \u2014 object position",
      },
      { text: ".", type: "punct" },
    ],
  },
];

// ── Decision Tree data ─────────────────────────────────────────────────────
const PRONOUN_TREE: TreeNode = {
  id: "root",
  question: "What type of pronoun problem is this?",
  hint: "Is the question about: (1) who/whom, (2) they/it agreement with antecedent, or (3) subject vs. object case (I/me, he/him, she/her)?",
  yesLabel: "who / whom choice",
  noLabel: "Agreement or case question",
  yes: {
    id: "who-whom",
    question:
      "Is the pronoun acting as the SUBJECT of its clause (performing the action)?",
    hint: "Substitute: if 'he/she' fits \u2192 use 'who'. If 'him/her' fits \u2192 use 'whom'. Try: 'The scientist who/whom won\u2026' \u2192 'he won' \u2192 'who'.",
    yesLabel: "Yes \u2014 it's the subject (he/she would fit)",
    noLabel: "No \u2014 it's the object (him/her would fit)",
    yes: {
      id: "use-who",
      result:
        "\u2713 Use 'who' \u2014 it's in subject position (replaces he/she).",
      resultType: "correct",
      ruleRef: "who = he/she | whom = him/her",
    },
    no: {
      id: "use-whom",
      result:
        "\u2713 Use 'whom' \u2014 it's in object position (replaces him/her).",
      resultType: "correct",
      ruleRef: "whom = him/her | 'To whom' = 'To him'",
    },
  },
  no: {
    id: "agreement-or-case",
    question:
      "Does the pronoun need to AGREE with its antecedent (match in number)?",
    hint: "Antecedent = the noun the pronoun refers back to. Example: 'Each student submitted their assignment' \u2014 'their' must match 'each student' (singular).",
    yesLabel: "Yes \u2014 agreement question",
    noLabel: "No \u2014 subject/object case question (I vs. me)",
    yes: {
      id: "agreement",
      question:
        "Is the antecedent singular or an indefinite pronoun (each, every, anyone, someone)?",
      yesLabel: "Yes \u2014 singular or indefinite",
      noLabel: "No \u2014 clearly plural",
      yes: {
        id: "singular-antecedent",
        result:
          "\u26a0 Use a SINGULAR pronoun: 'he or she' / 'his or her'. Indefinite pronouns (each, someone, anyone) require singular pronouns. 'Their' is plural and incorrect here.",
        resultType: "warning",
        ruleRef: "Each student \u2192 his or her (not their)",
      },
      no: {
        id: "plural-antecedent",
        result:
          "\u2713 Use a plural pronoun: they, their, them \u2014 matches the plural antecedent.",
        resultType: "correct",
        ruleRef: "[Plural noun] \u2192 they/their/them",
      },
    },
    no: {
      id: "case",
      question:
        "Is the pronoun in SUBJECT position (before the verb, doing the action)?",
      hint: "Try covering the other person: 'He and I went' \u2192 'I went' \u2713. 'Between he and I' \u2192 'Between I' \u2717 \u2192 use 'me'.",
      yesLabel: "Yes \u2014 it's the subject",
      noLabel: "No \u2014 it's after a preposition or is an object",
      yes: {
        id: "subject-case",
        result: "\u2713 Use subject pronouns: I, he, she, we, they, who.",
        resultType: "correct",
        ruleRef: "Subject: I/he/she/we/they performed the action",
      },
      no: {
        id: "object-case",
        result:
          "\u2713 Use object pronouns: me, him, her, us, them, whom \u2014 after prepositions or as direct/indirect objects.",
        resultType: "correct",
        ruleRef: "Object: gave it to me/him/her/them",
      },
    },
  },
};

const TREE_SCENARIOS: TreeScenario[] = [
  {
    label: "Sentence 1",
    sentence:
      "Each of the students submitted their assignments before the deadline.",
    tree: PRONOUN_TREE,
  },
  {
    label: "Sentence 2",
    sentence:
      "The award was presented to Sarah and I at the end of the ceremony.",
    tree: PRONOUN_TREE,
  },
  {
    label: "Sentence 3",
    sentence:
      "The professor, who students found inspiring, won the teaching award.",
    tree: PRONOUN_TREE,
  },
];

// ── Lesson component ───────────────────────────────────────────────────────
const EBRWPronounsLesson: 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="Pronoun 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">
            Pronouns &amp; Agreement
          </h2>
          <p className="text-lg text-slate-500 mb-8">
            See how pronoun choices work — then master the rules the SAT tests
            most.
          </p>

          {/* Rule summary grid */}
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-8">
            {[
              {
                num: 1,
                rule: "Who vs. Whom",
                desc: "who = subject (he/she). whom = object (him/her). Test by substituting.",
              },
              {
                num: 2,
                rule: "Pronoun\u2013Antecedent Agreement",
                desc: "Pronoun must match antecedent in number. Each/anyone/someone \u2192 singular.",
              },
              {
                num: 3,
                rule: "Subject Pronouns",
                desc: "I, he, she, we, they, who \u2014 before verbs as subjects.",
              },
              {
                num: 4,
                rule: "Object Pronouns",
                desc: "me, him, her, us, them, whom \u2014 after prepositions or as objects.",
              },
            ].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">
            Pronoun 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"
          />

          {/* Common Traps */}
          <div className="space-y-3 mt-8 mb-6">
            {[
              {
                label: "Each/Every/Anyone \u2192 Singular Pronoun",
                desc: "'Each student submitted their assignment' sounds natural but is wrong. Use 'his or her' for formal SAT writing.",
              },
              {
                label: "I vs. Me After Prepositions",
                desc: "'between you and I' is always wrong. After prepositions, always use object pronouns: me, him, her, us, them.",
              },
              {
                label: "Who vs. Whom \u2014 Substitute Test",
                desc: "Replace who/whom with he/him. If 'he' fits \u2192 who. If 'him' fits \u2192 whom.",
              },
            ].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">
              The SAT's most common pronoun traps are 'their' with singular
              antecedents, 'I' after prepositions, and 'who' where 'whom' is
              correct. Test with substitution: he/him for who/whom, 'it is' for
              'it's'.
            </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 pronoun 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(2, 4).map((q) => (
            <PracticeFromDataset key={q.id} question={q} color="purple" />
          ))}
          {FORM_STRUCTURE_MEDIUM.slice(1, 2).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 EBRWPronounsLesson;
