import React, { useRef, useState, useEffect } from "react";
import { ArrowDown, Check, BookOpen, AlertTriangle, Zap } from "lucide-react";
import ContextEliminationWidget, {
  type VocabExercise,
} from "../../../components/lessons/ContextEliminationWidget";
import { PracticeFromDataset } from "../../../components/lessons/LessonShell";
import {
  WORDS_CONTEXT_EASY,
  WORDS_CONTEXT_MEDIUM,
} from "../../../data/rw/words-in-context";

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

const EBRWVocabMeaningLesson: 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 = (i: number) => {
    setActiveSection(i);
    sectionsRef.current[i]?.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-rose-50" : "hover:bg-slate-50"}`}
      >
        <div
          className={`w-8 h-8 rounded-full flex items-center justify-center shrink-0 ${isActive ? "bg-rose-600 text-white" : isPast ? "bg-rose-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-rose-900" : "text-slate-600"}`}
        >
          {title}
        </p>
      </button>
    );
  };

  const VOCAB_EXERCISES: VocabExercise[] = [
    {
      sentence:
        "The critic's review was pointed, cutting directly to the weaknesses of the performance without any diplomatic softening.",
      word: "pointed",
      question: "As used in this sentence, 'pointed' most nearly means:",
      options: [
        {
          id: "A",
          definition: "having a sharp physical tip",
          isCorrect: false,
          elimReason:
            "This is the literal, physical meaning. In a review context, 'pointed' describes the directness of criticism, not a physical shape.",
        },
        {
          id: "B",
          definition: "deliberately directed and sharply critical",
          isCorrect: true,
          elimReason:
            "Correct — 'pointed' here means incisively critical and direct. The phrase 'cutting directly to the weaknesses' confirms the metaphorical sharpness of the critique.",
        },
        {
          id: "C",
          definition: "polite and professionally worded",
          isCorrect: false,
          elimReason:
            "Opposite — the sentence says 'without diplomatic softening,' meaning the review was NOT polite. Eliminate on opposite-meaning grounds.",
        },
        {
          id: "D",
          definition: "carefully structured and well-organized",
          isCorrect: false,
          elimReason:
            "The sentence emphasizes directness and impact, not organization. 'Pointed' is about force, not structure.",
        },
      ],
    },
    {
      sentence:
        "The administration's new policy represented a marked departure from the approach taken by its predecessor.",
      word: "marked",
      question: "As used in this sentence, 'marked' most nearly means:",
      options: [
        {
          id: "A",
          definition: "labeled or indicated with a visible sign",
          isCorrect: false,
          elimReason:
            "Physical marking meaning. Here 'marked departure' is an idiom meaning notable/significant departure — no literal label is involved.",
        },
        {
          id: "B",
          definition: "slight and barely noticeable",
          isCorrect: false,
          elimReason:
            "Opposite connotation — 'marked' as an adjective before a noun means striking or significant, the opposite of slight.",
        },
        {
          id: "C",
          definition: "clearly noticeable and significant",
          isCorrect: true,
          elimReason:
            "Correct — 'a marked departure' means a clearly noticeable, significant change. This is the standard idiomatic meaning of 'marked' as an adjective.",
        },
        {
          id: "D",
          definition: "controversial and widely debated",
          isCorrect: false,
          elimReason:
            "The sentence says nothing about controversy — it only describes how different the policy is. 'Marked' means notable, not controversial.",
        },
      ],
    },
    {
      sentence:
        "Despite the economic pressures, the nonprofit remained committed to its founding mission, refusing to compromise its principles.",
      word: "compromise",
      question: "As used in this sentence, 'compromise' most nearly means:",
      options: [
        {
          id: "A",
          definition: "reach a mutual agreement through negotiation",
          isCorrect: false,
          elimReason:
            "This is the common meaning of 'compromise' as a noun/verb in negotiations. Here it's used differently — to weaken or undermine.",
        },
        {
          id: "B",
          definition: "weaken or undermine",
          isCorrect: true,
          elimReason:
            "Correct — 'compromise its principles' means to weaken or betray them. This is the secondary meaning of 'compromise' as a verb: to expose to risk or damage.",
        },
        {
          id: "C",
          definition: "publicly disclose or reveal",
          isCorrect: false,
          elimReason:
            "This meaning applies to 'compromise' in security contexts (e.g., 'compromised data'). Here the context is about integrity, not disclosure.",
        },
        {
          id: "D",
          definition: "renegotiate or redefine",
          isCorrect: false,
          elimReason:
            "'Refusing to compromise' means refusing to weaken principles, not refusing to redefine them. Too specific and misses the core meaning.",
        },
      ],
    },
  ];

  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="Word Meaning Strategy"
            icon={BookOpen}
          />
          <SectionMarker
            index={1}
            title="Context Elimination"
            icon={AlertTriangle}
          />
          <SectionMarker index={2} title="Practice Quiz" icon={Zap} />
        </nav>
      </aside>

      <div className="flex-1 lg:ml-64 p-6 md:p-12 max-w-4xl mx-auto">
        {/* Section 0: Word Meaning Strategy */}
        <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-rose-100 text-rose-700 px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider mb-4">
            Vocabulary in Context
          </div>
          <h2 className="text-4xl font-extrabold text-slate-900 mb-2">
            Word Meaning Strategy
          </h2>
          <p className="text-lg text-slate-500 mb-8">
            A word can have many meanings. "Most nearly means" asks which
            meaning fits THIS sentence. Context, not the dictionary, is the
            judge.
          </p>

          <div className="bg-rose-50 border border-rose-200 rounded-2xl p-6 mb-6 space-y-4">
            <h3 className="text-lg font-bold text-rose-900">
              Four Essential Rules
            </h3>
            <div className="space-y-3">
              <div className="bg-white rounded-xl p-3 border border-rose-100">
                <p className="text-xs font-bold text-rose-700 mb-1">
                  1. 'Most Nearly Means'
                </p>
                <p className="text-xs text-slate-600">
                  This question type asks for the word's meaning IN THIS
                  CONTEXT, not its general definition. The same word can have
                  different meanings in different sentences.
                </p>
              </div>
              <div className="bg-white rounded-xl p-3 border border-rose-100">
                <p className="text-xs font-bold text-rose-700 mb-1">
                  2. Substitute Test
                </p>
                <p className="text-xs text-slate-600">
                  Insert each answer choice into the sentence in place of the
                  word. The correct answer will sound natural and preserve the
                  sentence's meaning.
                </p>
              </div>
              <div className="bg-white rounded-xl p-3 border border-rose-100">
                <p className="text-xs font-bold text-rose-700 mb-1">
                  3. Watch for Secondary Meanings
                </p>
                <p className="text-xs text-slate-600">
                  The SAT deliberately picks words that have a common meaning
                  AND a rarer contextual meaning. The correct answer is usually
                  the less obvious one.
                </p>
              </div>
              <div className="bg-white rounded-xl p-3 border border-rose-100">
                <p className="text-xs font-bold text-rose-700 mb-1">
                  4. Tone and Connotation
                </p>
                <p className="text-xs text-slate-600">
                  Even if a word is technically correct, wrong connotation =
                  wrong answer. 'Famous' and 'notorious' both mean well-known,
                  but 'notorious' has negative connotation.
                </p>
              </div>
            </div>
          </div>

          <div className="space-y-3 mb-6">
            <div className="bg-rose-100 rounded-xl p-4 border border-rose-200">
              <p className="text-sm text-slate-800 italic">
                "The scientist's findings were met with considerable reservation
                by her peers."
              </p>
            </div>
            <div className="bg-green-100 rounded-xl p-4 border border-green-200">
              <p className="text-sm text-slate-800">
                <span className="font-bold text-green-800">
                  'Reservation' here = doubt/skepticism.
                </span>{" "}
                Not a place to stay — context overrides the common meaning.
              </p>
            </div>
            <div className="bg-orange-100 rounded-xl p-4 border border-orange-200">
              <p className="text-sm text-slate-800">
                <span className="font-bold text-orange-800">
                  Wrong choice trap:
                </span>{" "}
                'hesitation' — close but 'reservation' implies more sustained
                doubt, not momentary pause.
              </p>
            </div>
          </div>

          <div className="bg-red-50 border border-red-200 rounded-xl p-4 mb-6">
            <p className="text-sm font-bold text-red-800 mb-1">
              The 'most nearly means' trap
            </p>
            <p className="text-sm text-slate-700">
              The SAT picks the most common meaning of the word as a wrong
              answer choice. Always check if the common meaning fits the
              sentence — if it feels wrong in context, look for the rarer
              contextual meaning.
            </p>
          </div>

          <div className="bg-rose-900 rounded-2xl p-5 mb-8">
            <p className="text-sm font-bold text-rose-100 mb-1">Golden Rule</p>
            <p className="text-sm text-white">
              The correct answer substitutes naturally into the sentence without
              changing the author's intended meaning. Test each choice by
              reading the full sentence aloud.
            </p>
          </div>

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

        {/* Section 1: Context Elimination */}
        <section
          ref={(el) => {
            sectionsRef.current[1] = el;
          }}
          className="min-h-screen flex flex-col justify-center mb-24"
        >
          <div className="inline-flex items-center gap-2 bg-rose-100 text-rose-700 px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider mb-4">
            Interactive Practice
          </div>
          <h2 className="text-4xl font-extrabold text-slate-900 mb-2">
            Context Elimination
          </h2>
          <p className="text-lg text-slate-500 mb-8">
            For each question below, read the sentence and use context clues to
            eliminate wrong choices — then select the best meaning.
          </p>

          <ContextEliminationWidget
            exercises={VOCAB_EXERCISES}
            accentColor="rose"
          />

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

        {/* Section 2: Practice 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 Quiz
          </h2>
          {WORDS_CONTEXT_EASY.slice(2, 4).map((q) => (
            <PracticeFromDataset key={q.id} question={q} color="fuchsia" />
          ))}
          {WORDS_CONTEXT_MEDIUM.slice(1, 2).map((q) => (
            <PracticeFromDataset key={q.id} question={q} color="fuchsia" />
          ))}
          <div className="mt-8 text-center">
            <button
              onClick={onFinish}
              className="px-6 py-3 bg-rose-900 text-white font-bold rounded-full hover:bg-rose-700 transition-colors"
            >
              Finish Lesson
            </button>
          </div>
        </section>
      </div>
    </div>
  );
};

export default EBRWVocabMeaningLesson;
